#include<stdio.h>
#include<stdlib.h>
struct Stack_List
{
int data;
struct Stack_List *Next;
};
typedef struct Stack_List Stack ;
typedef Stack* Link ;
Link stack=NULL;
/********************************************/
/* check the stack ( empty or not ) */
/********************************************/
int check()
{
if(stack==NULL)
return 1;
else return 0;
}
/********************************************/
/* print the stack */
/********************************************/
void print(Link Head)
{
Link pointer;
pointer=Head;
if(check(pointer))
printf("The stack is empty\n");
else
{
for(pointer=stack;pointer!=NULL;pointer=pointer->Next)
printf("[%d]",pointer->data);
printf("\n");
}
}
/********************************************/
/* push a data to the satck */
/********************************************/
Link push()
{
int data;
Link New;
New=(Link)malloc(sizeof(Stack));
if(New==NULL)
printf("Memory allocate faulure!\n");
else
{
printf("please input a num you want to push :");
scanf("%d",&data);
New->data=data;
New->Next=stack;
stack=New;
}
return stack;
}
/********************************************/
/* pop a data from the satck */
/********************************************/
Link pop(int *data,int *flag)
{
Link pointer;
printf("Now,you will pop a data from the stack\n");
pointer=stack;
if(check(pointer))
{
*flag=0;
printf("The stack is empty\n");
}
else
{
*flag=1;
stack=stack->Next;
*data=pointer->data;
free(pointer);
}
return stack;
}
int main(void)
{
int choice;
int data; // 如果这里定义为 *data;
int flag;
while(1){
printf("\t**********************************\n");
printf("\t 1.push a data to the stack;\n");
printf("\t 2.pop a data to the stack;\n");
printf("\t 3.print the stack;\n");
printf("\t 4.Exit the stack system\n");
printf("please input your choice:");
scanf("%d",&choice);
switch(choice){
case 1:
stack=push();
break;
case 2:
stack=pop(&data,&flag); //这里为stack=pop(data,&flag);
if(flag!=0)
printf("the data you pop is %d\n",data); // 这里输出 printf("the data you pop is %d\n",*data);
break;
case 3:
print(stack);
break;
case 4:
exit(0);
default:
printf("Error input \n");
}
}
}
如果把上面的 部分语句用 红色部分代替,为什么执行的时候就会出错 ? 偶菜,高手给点帮助。
[此贴子已经被作者于2006-3-27 14:58:30编辑过]