⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sunxuzhan.txt

📁 顺序栈的出栈、入栈、求栈长等基本操作 #include <stdio.h> #include <stdlib.h> #define STACKSIZE 50 typed
💻 TXT
字号:
此程序完成对顺序栈的出栈、入栈、求栈长等基本操作.

#include <stdio.h>
#include <stdlib.h>
#define STACKSIZE 50
typedef char DateType;
typedef struct
{
 DateType s[STACKSIZE];
 int top;
}SeqStack;
int i;
DateType x;
void InitSt(SeqStack* st)
{
// st=(SeqStack*)malloc(STACKSIZE*sizeof(SeqStack));   这里不必为它分配内存
 st->top=0;
 printf("\n\t\t\t创建成功!");
}
void push(SeqStack* st,DateType x)
{
 if(st->top==STACKSIZE)
 {
  printf("\n\t\t\t栈已满!");
 }
 else
 {
  st->s[st->top]=x;
  st->top++;
 }
}
void pop(SeqStack* st)
{
 i=st->top;
 x=st->s[i-1];
 if(st->top==0)
 {
  printf("\n\t\t\t栈为空!");
 }
 else
 {
  st->top--;
  printf("\n\t\t\t出栈成功!");
  printf("\n\t\t\t出栈元素为:%c",x);
 }
}
void readTop(SeqStack* st)
{
 i=st->top;
 if(st->top==0)
 {
  printf("\n\t\t\t栈为空!");
 }
 else
 {
  i--;                                        //注意这里不能是st->top--,这样会删除掉栈顶元素
     printf("\n\t\t\t栈顶元素为:%c",st->s[i]);
 }
}
void lenSt(SeqStack* st)
{
 if(st->top==0)
 {
  printf("\n\t\t\t栈为空!");
 }
 else
 {
     printf("\n\t\t\t栈中有%d个元素",st->top);
 }
}
void showSt(SeqStack* st)
{
    if(st->top==0)
 {
  printf("\n\t\t\t栈为空!");
 }
 else
 {
  printf("\n\t\t\t栈中元素为:\n");
  printf("\n\t\t\t");
  for(i=0;i<st->top;i++)
  {
   printf("%c\t",st->s[i]);
  }
 }
}
void main()
{
 int i,j;
 DateType x;
 SeqStack st;
 while(j)
 {
  printf("\n\n\n\n");
  printf("\n\t\t\t**********************************");
        printf("\n\t\t\t***     1---创建栈             ***");
  printf("\n\t\t\t***     2---入栈               ***");
  printf("\n\t\t\t***     3---出栈               ***");
  printf("\n\t\t\t***     4---读栈顶元素         ***");
  printf("\n\t\t\t***     5---求栈长             ***");
  printf("\n\t\t\t***     6---显示栈元素         ***");
  printf("\n\t\t\t***     0---退出               ***");
        printf("\n\t\t\t**********************************\n");
  printf("\n\t\t\t请选择菜单号(0---6):");
  scanf("%d",&i);getchar();
  if(i==1)
  {
   InitSt(&st);
  }
  else if(i==2)
  {
            printf("\n\t\t\t请输入入栈元素:");
   scanf("%c",&x);
   push(&st,x);
  }
  else if(i==3)
  {
   pop(&st);
  }
  else if(i==4)
  {
   readTop(&st);
  }
  else if(i==5)
  {
   lenSt(&st);
  }
  else if(i==6)
  {
   showSt(&st);
  }
  else if(i==0)
  {
   j=0;
   printf("\n\t\t\t程序结束!\n");
  }
 }
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -