#为什么我的打印栈顶和打印功能不好使啊
//Stack On Link Structure
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
typedef int StackElementType; //数据元素类型定义
typedef struct node{ //顺序栈的定义
StackElementType data;
struct node* next;
}LinkStackNode;
typedef LinkStackNode* LinkStack;
void InitStack(LinkStack top);//创建一个带头节点链栈
int IsEmpty(LinkStack top);//判空
int Push(LinkStack top, StackElementType x); //进栈
int Pop(LinkStack top, StackElementType *x); //出栈
int GetTop(LinkStack top, StackElementType *x); //取栈顶元素
void PrintStack(LinkStack top); //打印栈元素(从栈顶到栈底顺序)
void ClearStack(LinkStack top); //把一个带头节的链栈清空(带头节点的空栈)
int main()
{
int a,b,i;
StackElementType *x;
LinkStack *top = (LinkStack*)malloc(sizeof(LinkStackNode));
// LinkStack top; //top就是一个指针变量
//生成菜单
char sel=' ';
while(sel!='0')
{
printf("------栈(链式存储结构)演示系统-------\n");
printf(" 版本:1.0 作者:辛新坪 日期:XXXX-XX-XX\n");
printf("------------------------------------------\n");
printf(" 1.创建空栈\n");
printf(" 2.进栈操作\n");
printf(" 3.出栈操作\n");
printf(" 4.打印栈顶元素\n");
printf(" 5.打印栈\n");
printf(" 6.清空屏幕\n");
printf(" 7.清空栈\n");
printf(" 0.退出系统\n");
printf("请输入选项[0-7]:");
sel=getch();
switch(sel)
{
case '1':
printf("创建空栈操作.\n");
InitStack(*top);
system("pause"); //按任意键继续
break;
case '2':
printf("进栈操作.\n");
printf("请输入所需输入的数据个数:");
scanf("%d",&b);
for(i = 0;i <b;i++)
{
printf("请输入数据:");
scanf("%d",&a);
Push(*top, a);
}
system("pause"); //按任意键继续
break;
case '3':
printf("出栈操作.\n");
Pop(*top, x);
system("pause"); //按任意键继续
break;
case '4':
printf("打印栈顶元素操作.\n");
printf("%d",GetTop(*top, x));
system("pause"); //按任意键继续
break;
case '5':
printf("打印栈操作.\n");
PrintStack(*top);
system("pause"); //按任意键继续
break;
case '6':
system("cls");
break;
case '7':
printf("释放栈空间操作.\n");
system("cls");
break;
case '0':
printf("\n谢谢使用,再见!\n");
break;
default:
printf("您输入的选项不合法,请重新选择!\n");
}
}
return 0;
}
void InitStack(LinkStack top)
{
top->next = NULL ;
}
int IsEmpty(LinkStack top)
{
if(top->next = NULL)
{
printf("此栈为空!");
}
}
int Push(LinkStack top, StackElementType x)//进栈
{
LinkStackNode *temp;
temp = (LinkStackNode*)malloc(sizeof(LinkStackNode));
if(temp == NULL)
return(FALSE);
temp->data = x;
temp->next = top->next;
top->next = temp;
return(TRUE);
}
int Pop(LinkStack top, StackElementType *x)//出栈
{
LinkStack temp;
temp = top->next;
if(temp == NULL)
return(FALSE);
*x = temp->data;
top->next = temp->next;
free(temp);
return(TRUE);
}
int GetTop(LinkStack top, StackElementType *x) //打印栈顶
{
if(top->next == NULL)
return(FALSE);
else
{
*x = top->data;
// printf("%d\n",*x);
return(*x);
}
}
void PrintStack(LinkStack top)
{
LinkStackNode *temp;
temp = top;
if(top->next != NULL)
{
printf("%d",temp->data);
temp = top->next;
}
}