#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 50
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef int SElemType;
typedef struct node {
SElemType data;//数据域
struct node *next;//指针域
}LinkStackNode,*LinkStack;
void Menu()
{
printf("*********栈的基本操作*********\n\n");
printf("\t1.栈清空\n");
printf("\t2.栈数据输出\n");
printf("\t3.数据入栈\n");
printf("\t4.数据出栈\n");
printf("\t0.退出系统\n\n");
printf("*********栈的基本操作*********\n");
}
//初始化链栈
Status InitStack(LinkStack &S)
{
S = NULL;
return OK;
}
Status CrearStack(LinkStack &S)
{
S->next = NULL;
return OK;
}
Status Input(LinkStack &S)
{
LinkStack p;
p->next = S->next;
if (p == NULL)
return ERROR;
while(S != NULL)
{
printf("%d ", p->data);
p = p->next;
}
return OK;
}
Status Push(LinkStack &S, SElemType e) //入栈操作
{
LinkStackNode * p;
p = (LinkStackNode *)malloc(sizeof(LinkStackNode));
if (p == NULL)
return ERROR;
p->data = e;
p->next = S->next;
S->next = p;
return OK;
}
Status Pop(LinkStack &S, SElemType *e) //出栈操作
{
LinkStackNode * p;
p = S->next;
if (S == NULL)
return OVERFLOW;
S->next = p->next;
*e = p->data;
free(p);
return OK;
}
int main()
{
LinkStack S;
int i,choice = -1;
InitStack(S);
SElemType e ;
while(choice != 0)
{
system("cls");
fflush(stdin);
Menu();
printf("\n\t请选择功能:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
if(CrearStack(S) == OK)
printf("\n清空操作成功!\n");
else
printf("\n清空操作失败!\n");
system("pause");
break;
}
case 2:
{
if(!Input(S))
printf("栈内没有数据\n");
else
{
printf("栈的所有元素为:");
Input(S);
}
system("pause");
break;
}
case 3:
{
printf("插入的数据: ");
scanf("%d",&e);
Push(S,e);
printf("数据%d入栈成功\n",e);
system("pause");
break;
}
case 4:
{
if(Pop(S,&e) == 1)
printf("数据%d出栈成功\n",e);
else
printf("栈内没有数据,出栈失败\n",e);
system("pause");
break;
}
case 0:
{
printf("感谢使用,bye-bye!");
exit(0);
}
default:
{
printf("功能选择错误,请重新选择\n");
system("pause");
}
}
}
return 1;
}
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 50
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef int SElemType;
typedef struct node {
SElemType data;//数据域
struct node *next;//指针域
}LinkStackNode,*LinkStack;
void Menu()
{
printf(“栈的基本操作\n\n”);
printf(“\t1.栈清空\n”);
printf(“\t2.栈数据输出\n”);
printf(“\t3.数据入栈\n”);
printf(“\t4.数据出栈\n”);
printf(“\t0.退出系统\n\n”);
printf(“栈的基本操作\n”);
}
//初始化链栈
Status InitStack(LinkStack &S)
{
S = NULL;
return OK;
}
Status CrearStack(LinkStack &S)
{
S->next = NULL;
return OK;
}
Status Input(LinkStack &S)
{
LinkStack p;
p->next = S->next;
if (p == NULL)
return ERROR;
while(S != NULL)
{
printf("%d ", p->data);
p = p->next;
}
return OK;
}
Status Push(LinkStack &S, SElemType e) //入栈操作
{
LinkStackNode * p;
p = (LinkStackNode *)malloc(sizeof(LinkStackNode));
if (p == NULL)
return ERROR;
p->data = e;
p->next = S->next;
S->next = p;
return OK;
}
Status Pop(LinkStack &S, SElemType *e) //出栈操作
{
LinkStackNode * p;
p = S->next;
if (S == NULL)
return OVERFLOW;
S->next = p->next;
*e = p->data;
free§;
return OK;
}
int main()
{
LinkStack S;
int i,choice = -1;
InitStack(S);
SElemType e ;
while(choice != 0)
{
system(“cls”);
fflush(stdin);
Menu();
printf(“\n\t请选择功能:”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
{
if(CrearStack(S) == OK)
printf(“\n清空操作成功!\n”);
else
printf(“\n清空操作失败!\n”);
system(“pause”);
break;
}
case 2:
{
if(!Input(S))
printf(“栈内没有数据\n”);
else
{
printf(“栈的所有元素为:”);
Input(S);
}
system(“pause”);
break;
}
case 3:
{
printf(“插入的数据: “);
scanf(”%d”,&e);
Push(S,e);
printf(“数据%d入栈成功\n”,e);
system(“pause”);
break;
}
case 4:
{
if(Pop(S,&e) == 1)
printf(“数据%d出栈成功\n”,e);
else
printf(“栈内没有数据,出栈失败\n”,e);
system(“pause”);
break;
}
case 0:
{
printf(“感谢使用,bye-bye!”);
exit(0);
}
default:
{
printf(“功能选择错误,请重新选择\n”);
system(“pause”);
}
}
}
return 1;
}