链栈的定义及相关操作
2017-05-20 18:31
204 查看
#include <stdio.h> #include <malloc.h> typedef int ElemType; typedef struct linknode { ElemType data; linknode *next; } Node,*LinkStack; void PrintStack(LinkStack top);//打印栈中的元素 void DestroyStack(LinkStack top);//销毁栈 bool Push(LinkStack top,ElemType x);//压栈 bool Pop(LinkStack top,ElemType *x);//出栈 bool IsEmpty(LinkStack top);//判断栈空 int StackTop(LinkStack top);//返回栈顶元素 int StackLen(LinkStack top);//求栈中元素的个数 int main(void) { int x; int *a=&x; LinkStack S1=(LinkStack)malloc(sizeof(Node)); S1->next=NULL; for(int i=1; i<=10; i++) Push(S1,i); printf("%d\n",StackLen(S1)); PrintStack(S1); Pop(S1,a); printf("%d\n",x); if(!IsEmpty(S1)) printf("NO\n"); printf("%d\n",StackTop(S1)); printf("%d\n",StackLen(S1)); DestroyStack(S1); return 0; } void PrintStack(LinkStack top)//打印栈中的元素 { if(top->next==NULL) printf("the stack is empty\n"); else { while(top->next!=NULL) { printf("%d ",top->next->data); top=top->next; } printf("\n"); } } void DestroyStack(LinkStack top)//销毁栈 { LinkStack q; while(top) { q=top->next; delete top; top=q; } printf("销毁成功"); } bool Push(LinkStack top,ElemType x)//压栈 { Node *p; p=(LinkStack)malloc(sizeof(Node)); if(p==NULL) return false; p->data=x; p->next=top->next; top->next=p; return true; } bool Pop(LinkStack top,ElemType *x)//出栈 { Node *p; p=top->next; if(p==NULL) return false; top->next=p->next; *x=p->data; free(p); return true; } bool IsEmpty(LinkStack top)//判断栈空 { if(top->next==NULL) return true; else return false; } int StackTop(LinkStack top)//返回栈顶元素 { if(top->next==NULL) printf("the Stack is empty\n"); return top->next->data; } int StackLen(LinkStack top)//求栈中元素的个数 { int len=0; while(top->next!=NULL) { len++; top=top->next; } return len; }
相关文章推荐
- 链栈的定义、初始化、出栈、入栈等操作
- js定义数组及相关操作
- 顺序栈的定义及相关操作
- 数据库视图定义及其相关操作
- 线性表数据结构类型定义及相关操作总结
- 数据结构(9)--链队列的定义以及相关操作的实现
- EBS年底定义sequence以及assign的相关操作
- bo3-1-1.c 链栈(存储结构由c2-2.h定义)的基本操作(4个) 及验证
- 定义一个过滤器Filter及Filter提供的相关操作
- 数据库视图定义及其相关操作
- vector的定义和相关操作
- js定义数组及相关操作
- 链栈的相关操作
- 栈定义及其基本操作,顺序栈和链栈
- 顺序栈,共享栈以及链栈的相关操作
- 数据结构(5)--栈的定义以及相关操作的实现
- 链队列的定义及相关操作
- js定义数组及相关操作
- 数据结构_链栈的建立与相关操作_C语言源代码
- PHP数组的定义及相关的操作函数