/*****************************************
C实现链式栈
*****************************************/
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int StackData;
typedef struct node
{
StackData data; //结点数据
struct node * next; //结点链指针
} StackNode;
StackNode * InitStack(void)
{
StackNode *top;
top=NULL;
return (top);
}
int Push ( StackNode **top, StackData x )
{
StackNode *p =(StackNode*)malloc(sizeof(StackNode));
if(top==NULL)
{
*top=p;
p->data = x;
p->next=NULL;
}
else
{
p->data = x;
p->next = *top;
*top = p;
}
return 1;
}
int StackEmpty (StackNode *top)
{
return (top==NULL);
}
int Pop(StackNode **top,StackData * x)
{
if ( StackEmpty (*top) ) return 0;
StackNode * p = (*top);
*top = p->next;
(*x) = p->data;
free (p);
return 1;
}
int GetTop (StackNode *top, StackData *x )
{
if ( StackEmpty (top) ) return 0;
(*x) = top->data;
return 1;
}
int main()
{
int i=0;
StackData x=0;
StackNode *top;
top=InitStack();
for(i=0;i<10;i++)
{
Push(&top,i);
}
GetTop(top,&x);
printf("%d\n",x);
for(i=0;i<10;i++)
{
Pop(&top,&x);
printf("%d\n",x);
}
return 0;
}
C实现链式栈
最新推荐文章于 2024-01-10 21:44:16 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)