制作一个菜单 ,用栈实现一些基本操作

来源:互联网 发布:erp沙盘软件 编辑:程序博客网 时间:2024/04/28 04:11
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define  FALSE 0
#define  TRUE  1
typedef struct node
{
         int data;
         struct node *next;
}Stack;
int len=0;
int maxsize=0;
void Initstack();
void Createstack();
int Destroystack();
int  Push();
int  Pop();
int  IsEmpty();
int  IsFull();
int  Display();
int  Readfirst();
void Menu();
void Initstack(Stack *top)
{  
printf("请输入栈的空间大小:");
    scanf("%d",&maxsize);
}
void Createstack(Stack *top)
{
    int i;
Stack *p;
    p=(Stack *)malloc(sizeof(Stack));
   for(i=0;i<maxsize;i++)
{     
p->next=top->next;
top->next=p;
top->next=NULL;
}
printf("创建栈成功,栈的空间大小是:%d\n",maxsize);


}
int Destroystack(Stack *top)
{   
    char ch;
Stack *p;
    p=(Stack *)malloc(sizeof(Stack));
if(len==0)
{
printf("栈为空,请让数据进栈再销毁!\n");
return FALSE;
}
    printf("你真的要销毁栈吗?请输入(Y/y)确定,其他键为放弃:\n");
ch=getchar();
if(ch==89||ch==121)
{
while(len!=0)
{
   p=top->next;
top->next=p->next;
free(p);
len--;
}
printf("栈已经销毁!\n");
}
else
printf("已放弃摧毁该栈!\n");
return TRUE;
}
int Push(Stack *top,int data)
{        
         Stack *p;
         p=(Stack *)malloc(sizeof(Stack));
if(maxsize==0)
{
printf("未创建栈空间大小\n");
printf("数据%d进栈失败!\n",data);
         return FALSE;
}        
if(len>=maxsize)
{
printf("栈空间不足!\n");
return FALSE;
}
         p->data=data;
         p->next=top->next;
         top->next=p; 
         len++;
printf("数据%d已成功进栈!\n",p->data);
printf("栈的剩余空间大小为:%d\n",maxsize-len);
         return TRUE;
}
int Pop(Stack *top,int x)
{
       Stack *p;
    if(len==0)
    {
  printf("栈为空!\n");
  return FALSE;
    }
        p=top->next;
top->next=p->next;
        x=p->data;
        free(p);
        len--;
        printf("%d已出栈!\n",x);
    if(len==0)
{
printf("空间已用尽!\n");
}
return TRUE;
}
int IsEmpty(Stack *top)
{   
        if(len==0)
{
printf("栈为空!\n");
         return FALSE;
}
printf("栈不为空!\n");
         return TRUE;

int IsFull(Stack *top,int maxsize)
{
   if(len==0)
{
     printf("栈为空!\n");
     return FALSE;
}


if(len==maxsize)
      printf("此栈已满,请勿再增加数据!\n");
   if(len<maxsize)
 printf("栈未满,其空间大小为:%d\n",maxsize-len);
    return TRUE;          
}
int Display(Stack *top)
{  
       Stack *p;
  p=(Stack *)malloc(sizeof(Stack));
  p=top->next;
  if(len==0)
    {
  printf("栈为空!\n");
  return FALSE;
    }
 printf("栈中的内容是:");
    while(p!=NULL)
 {
       printf("%d ",p->data);
       p=p->next;
 }
printf("\n");
return TRUE;
}    
int Readfirst(Stack *top,int x)
{   
Stack *p;
    p=(Stack *)malloc(sizeof(Stack));
    if(len==0)
{
printf("栈为空!\n");
         return FALSE;
}
    p=top->next;
    x=p->data;
printf("栈顶数据是:%d\n",x);
return TRUE; 
}
void Menu()
{
system("cls");
printf("****************************\n");
printf("**    链栈基本功能演示   **\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("       8.判断是否是满栈     \n");
printf("       9.退出               \n");
return;
}
int main()
{
         int x,selection;
Stack *top ;
top=(Stack *)malloc(sizeof(Stack));
 
while(1)
{
   system("color F0");
Menu();
printf("请输入您的选择:\n");
scanf("%d",&selection);
            fflush(stdin);
switch(selection)
{
case 1:Initstack(top);
  Createstack(top);break;
case 2:Destroystack(top);break;
case 3:printf("向栈压入数据:");
  scanf("%d",&x);
  Push(top,x);break;
case 4:Pop(top,&x);break;
case 5:Display(top);break;
case 6:Readfirst(top,x);break;
case 7:IsEmpty(top);break;
case 8:IsFull(top,maxsize);break;
case 9:return 0;
default:printf("输入错误!\n");
}
        system("pause");
}
        return 0;
}
原创粉丝点击