数据结构之链表实现栈

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
typedef struct Node
{
int data;
struct Node *next;
}Node,*Lnode;
typedef struct stack
{
Lnode bottom,top;
}stack,*Stack;

int Create_Stack(Stack S)
{
S->bottom =S->top =(Lnode)malloc(sizeof(Node));
if(!S->bottom )
exit(0);
S->bottom ->next =NULL;


return OK;
}
int push(Stack S,int item)
{
Lnode pew = (Lnode)malloc(sizeof(Node));
if(!pew)
exit(0);
pew->data = item;

pew->next=S->top ;
S->top = pew;

return OK;

}

int pop(Stack S,int *item)
{
Lnode p;
if(S->top ==S->bottom )
return 0;
p = S->top ->next ;
*item = p->data ;
if(p!=S->bottom )
{
printf("%d\n",*item);
free(S->top );
S->top = p ;
}


return OK;
}

int main()
{
stack S;
int item;
Create_Stack(&S);
for(int i=0;i<20;++i)
push(&S,i);
for(i=0;i<25;++i)
pop(&S,&item);

return 0;
}

猜你喜欢

转载自www.cnblogs.com/muzixiaofeng/p/10088674.html