数据结构:顺序栈(含完整代码,可复制)

要编写一个顺序栈的代码,首先要了解栈的特点。它是先进后出(或后进先出)的顺序进出元素。

1.初始化栈函数

这个函数比较简单,首先要先建立一个结构体,包含最大空间和栈顶位置。而初始化就是让让栈顶的位置为0。

 void InitStack(SqStack *p)//初始化栈 
          { p->top=0;//使栈顶元素为0 
          }

2.在栈中插入一个元素

如果在栈中插入一个元素,因为它先进后出的特点,所以插入的元素在栈顶,成为新的栈顶,并且栈顶的位置要往后移。

 void Push(SqStack *p,ElemType x)//插入一个元素 
          { if (p->top<MAXSIZE-1) {p->top=p->top+1;p->elem[p->top]=x;}
              else printf("\n Overflow!");
          
          }

3.删除栈顶元素

首先要确定是否含有栈顶元素,若含有,则将栈顶位置减一并输出被删除的栈顶元素。

ElemType Pop(SqStack *p)//删除栈顶元素 
            {ElemType x;
              if(p->top!=0){x=p->elem[p->top];
                            p->top=p->top-1;
                            return(x);
                            }
              else{printf("\n Underflow!");
                    return(-1);
                    }
              }

4.输出顺序栈

根据栈先进后出的原则,元素从栈顶开始输出,并将栈顶给下一个元素,接着输出。

 void OutStack(SqStack p)//输出顺序栈 
         { int i,j;
           if(p.top==0) printf("\n The Stack is NULL.");
           for(i=p.top;i>0;i--)
           printf("\n %2d %6d",i,p.elem[i]);
           }

5.完整代码

#include"stdio.h"
#include"stdlib.h"
#define MAXSIZE 100
typedef int ElemType;
typedef struct { ElemType elem[MAXSIZE];//建立一个结构体 
                   int top;
                   }SqStack;
void OutStack(SqStack p);
void InitStack(SqStack *p);
void Push(SqStack *p,ElemType x);
ElemType Pop(SqStack *p);
ElemType GetTop(SqStack p);
main()
{ SqStack q;
   int i,y,cord;ElemType a;

  do { printf("\n");
       printf("\n   主菜单\n");
       printf("\n 1 初始化顺序栈\n");
       printf("\n 2 插入一个元素\n");
       printf("\n 3 删除栈顶元素\n");
       printf("\n 4 取栈顶元素\n");
       printf("\n 5 结束程序运行\n");
       printf("\n---------------\n");
      
       printf("请输入您的选择(1.2.3.4.5)"); scanf("%d",&cord);

       switch(cord)
           {case 1:InitStack(&q);OutStack(q);
                    break;
            case 2:{printf("\n请输入要插入的数据a=");scanf("%d",&a);
                   Push(&q,a);OutStack(q);
                   }break;
            case 3:{a=Pop(&q);printf("\n a=%d",a);
                   OutStack(q);
                   }break;
            case 4:{y=GetTop(q);printf("\n y=%d",y);
                   OutStack(q);
                   }break;
            case 5:exit(1);
            }

            }while(cord<=5);
        }/*main end */

        void InitStack(SqStack *p)//初始化栈 
          { p->top=0;//使栈顶元素为0 
          }

        void Push(SqStack *p,ElemType x)//插入一个元素 
          { if (p->top<MAXSIZE-1) {p->top=p->top+1;p->elem[p->top]=x;}
              else printf("\n Overflow!");
          
          }

          ElemType Pop(SqStack *p)//删除栈顶元素 
            {ElemType x;
              if(p->top!=0){x=p->elem[p->top];
                            p->top=p->top-1;
                            return(x);
                            }
              else{printf("\n Underflow!");
                    return(-1);
                    }
              }

        ElemType GetTop(SqStack p)//取栈顶元素 
        { ElemType x;
          if (p.top!=0){x=p.elem[p.top];
                        return(x);
                        }
            else { printf("\n Underflow!");
                   return(-1);
                   }
            }

        void OutStack(SqStack p)//输出顺序栈 
         { int i,j;
           if(p.top==0) printf("\n The Stack is NULL.");
           for(i=p.top;i>0;i--)
           printf("\n %2d %6d",i,p.elem[i]);
           }

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值