/*用单链表对堆栈操作*/
#include"stdio.h"
#define maxsize 20
typedef struct node
{
char data;
struct node *next;
}lnode;
lnode *initializtion(lnode *top)
{
top=NULL;
return(top);
}
lnode *push(lnode *top,int x)
{
lnode *p;
p=(lnode*)malloc(sizeof(lnode));
p->data=x;
p->next=top;
top=p;
printf("[%p]",&top);
return(top);
}
lnode *pop(lnode *top)
{
lnode *tep;
tep=top;
top=top->next;
free(tep);
return(top);
}
void print(lnode *top)
{
if(top==NULL)
{
printf("the stack is NULL");
top->data=0;
}
else
{
while(top!=NULL)
{
printf("[%d]",top->data);
top=top->next;
}
}
}
void main()
{
int select;
int x;
int y;
lnode *top;
top=initializtion(top);
do
{
printf("\n(1) Input a stack data");
printf("\n(2) Output a stack data");
printf("\n(3) Exit");
printf("\nPlease select one:");
scanf("%d",&select);
switch(select)
{
case 1:printf("\nPlease input the data:");
scanf("%d",&x);
top=push(top,x);
printf("[%p]",&top);
printf("\nThis stack is: ");
print(top);
break;
case 2:top=pop(top);
printf("\nThis stack is: ");
print(top);
printf("\nThe get data is: %d ",top->data);
break;
case 3:break;
}
}
while(select<3);
getch();
}
阅读(412) | 评论(0) | 转发(0) |