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

📄 栈的顺序存储结构及实现.cpp

📁 栈的顺序存储结构及实现
💻 CPP
字号:
#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);
void main()
{SqStack q;
 int 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(0);
}
}while(cord<=5);
}/*main end*/
void InitStack(SqStack *p)
{p->top=-1;
}
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!=-1){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!=-1){x=p.elem[p.top];
             return(x);
}
else{printf ("\n    Underflow!");
return(-1);
}
}
void OutStack(SqStack p)
{int i;
if(p.top==-1)printf("\n   The Stack is NULL.");
for(i=p.top;i>=0;i--)printf("\n%2d %6d",i,p.elem[i]);
}

⌨️ 快捷键说明

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