• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

数据结构之 链栈的实现

#include "stdafx.h"
#include "malloc.h"
#define maxSize 100 

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode;
void push(LNode *&L,int x)//使用这个方法 使得栈的入栈和出栈都在表头了
{
	LNode *p;

	p=(LNode*)malloc(sizeof(LNode));
	p->next=L->next;
	p->data=x;

	L->next=p;
	
}
void push2(LNode *&L,int x)//表尾
{
	LNode *s;
	s=(LNode*)malloc(sizeof(LNode));
	s->data=x;
	s->next=NULL;
	L->next=s;
	L=s;
}
void pop(LNode *&L,int &x)
{
	LNode *p;
	if(L->next==NULL)
		return;
	p=L->next;
	x=p->data;
	L->next=p->next;
	free(p);
}
void show(LNode *&L)
{
	int x;
	while(L->next!=NULL)
	{
		pop(L,x);
		printf("%d ",x);
	}
}
void initSqStack(LNode *&L)
{
	L=(LNode*)malloc(sizeof(LNode));
	L->next=NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
	LNode *L;
	initSqStack(L);
	for(int i=1;i<=10;i++)
		push2(L,i);
	show(L);
}


 

 

posted @ 2013-09-27 19:52  Class Xman  阅读(256)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3