数据结构---栈的链表实现
来源:互联网 发布:宣传图片制作软件 编辑:程序博客网 时间:2024/05/17 01:28
栈的链表实现C代码如下:
#include <stdio.h>typedef int ElemType;typedef struct node{ ElemType Data; struct node *next;}Node;typedef struct stack{ Node *top;}Stack;//初始化栈void InitStack(Stack *S){ S->top=NULL;}//入栈int PushStackValue(Stack *S){ printf("Input the Value of stack member:\n(0-exit)\n"); int value; int i=1; Node *NewNode=(Node *)malloc(sizeof(Node)); printf("Please input the %dst value of stack:\n",i); scanf("%d",&value); NewNode->Data=value; NewNode->next=S->top; S->top=NewNode; i++; while(value) { Node *NewNode=(Node *)malloc(sizeof(Node)); printf("Please input the %dst value of stack:\n",i); scanf("%d",&value); if(value!=0) i++; NewNode->Data=value; NewNode->next=S->top; S->top=NewNode; } S->top=S->top->next; return i-1;}//出栈int PopStackValue(Stack *S,int len){ if(len>0) { printf("the stack %dst value pop out: %d\n",len,S->top->Data); } else { printf("The Stack is empty\n"); } len--; S->top=S->top->next; return len;}//判断栈空void IsEmpty(Stack *S){ if(S->top==NULL) { printf("The Stack is empty.\n"); } else { printf("The stack is not empty.\n"); }}//清空栈void ClearStack(Stack *S){ S->top=NULL;}//遍历栈Stack * ScanStack(Stack *S){ Stack *Temp=S; printf("The all stack member(from top to bottom) is:\n"); while(S->top!=NULL) { printf("%d ",S->top->Data); S->top=S->top->next; } printf("\n"); return Temp;}void main(){ Stack *S=(Stack *)malloc(sizeof(Stack)); int len; InitStack(&S); len=PushStackValue(&S); len=PopStackValue(&S,len); len=PopStackValue(&S,len); S=ScanStack(&S); IsEmpty(&S);}
运行结果:
转载请注明作者:小刘
2 0
- 数据结构 栈的链表实现法
- 【数据结构】栈的链表实现
- 数据结构-----栈的链表实现
- 数据结构---栈的链表实现
- 【数据结构】栈-链表的实现
- 【数据结构】栈的链表实现
- 数据结构之栈的链表实现
- 【数据结构】栈的链表实现
- 链表数据结构的实现
- 数据结构-链表的实现
- 数据结构 栈 (链表实现)
- 数据结构 --栈的实现
- 数据结构:栈的实现
- 数据结构-----栈的实现
- 数据结构-----栈的实现
- 【数据结构】栈的实现
- 数据结构-栈的实现
- 经典数据结构之链表实现的栈
- js声明数组、对象在jsp页面中(获得ajax得到json数据)
- Spring IoC(控制反转)【三】
- 男人,改变了航空历史
- Stanford Machine Learning: (1). Linear Regression
- c语言经典,高手必备,找出卖耶稣的叛徒问题!
- 数据结构---栈的链表实现
- Android Toast.makeText
- 班加洛尔大部分地区即将兴建别墅
- 为什么在winPE系统下不能显示原来系统的硬盘
- uva 10209 Is This Integration ? (计算几何)
- 随机产生20个[10 , 50]的正整数存到数组中,并求数组中的所有元素最大值、最小值、平均值以及各元素之和,及第二大值
- HDU 1045 Fire Net
- 珀斯家居装修这是所有关于获得适当的主构造函数
- js秒表计时器