赞
踩
目录
初始化栈:
- #define MAXSIZE 5
- typedef struct
- {
- int *top;
- int *base;
- int stacksize;
- }SqStack;
- void CreateStack(SqStack &S)
- {
- S.base=new int[MAXSIZE];
- if(S.base==NULL){
- cout<<"分配内存失败!";
- return ;
- }
- S.top=S.base;
- S.stacksize=MAXSIZE;
- }
入栈:
-
- void Push(SqStack &S,int e){
-
- if(S.top-S.base==maxsize) { cout << "栈满!"; return ; }
-
- *S.top=e;
-
- S.top++;
-
- }
出栈:
- void Pop(SqStack &S)
- {
- if(S.top==S.base){
- cout<<"栈空"<<endl;
- return;
- }
- S.top--;
- }
取栈顶元素:
- int GetTop(SqStack &S)
- {
- if(S.top==S.base)
- {
- cout<<"栈空!"<<endl;
- return ;
- }
- return *(S.top--);
- }
完整代码:
- #include<iostream>
- #include<iomanip>
- using namespace std;
-
-
-
- #define maxsize 5
- typedef struct
- {
- int* base;
- int* top;
- int stactsize;
- }sqstack;
-
- //顺序栈的初始化
- void initstack(sqstack& s)
- {
- s.base = new int[maxsize];
- if (!s.base) {
- cout << "分配内存失败!" << endl;
- system("pause");
- return;
- }
- s.top = s.base;
- s.stactsize = maxsize;
- }
- //入栈
- void push(sqstack& s, int e)
- {
- if ((s.top - s.base)==maxsize) {
- cout << "栈满!" << endl;
- system("pause");
- return;
- }
- *s.top = e;
- s.top++;
- system("pause");
- }
- //出栈
- void pop(sqstack& s)
- {
- if (s.top == s.base) {
- cout << "栈空!" << endl;
- system("pause");
- return;
- }
- s.top--;
- system("pause");
- }
- int gettop(sqstack s)
- {
- if (s.top == s.base) {
- cout << "栈空!" << endl;
- system("pause");
- }
- return *(s.top - 1);
- }
-
-
- void menu(sqstack& s)
- {
- cout << "************************" << endl;
- cout << "| 1.入栈 |" << endl;
- cout << "| 2.出栈 |" << endl;
- cout << "| 3.取栈顶元素 |" << endl;
- cout << "| 4.退出 |" << endl;
- cout << "************************" << endl;
- cout << "input your choice(1~4):";
-
- int i;
- cin >> i;
- int e;
- int e1;
- switch (i) {
- case 1:
- cout << "请输入你要入栈的元素:";
- cin >> e;
- push(s, e);
- break;
- case 2:
- pop(s);
- break;
- case 3:
- e1 = gettop(s);
- if (e1 < 0) break;
- cout << "栈顶元素为:" << e1;
- system("pause");
- break;
- case 4:
- exit(0);
- break;
- default:
- cout << "非法输入!";
- break;
- }
- }
- //实现顺序结构栈的基本操作(初始化栈,入栈,出栈,获取栈顶元素)
- int main()
- {
- sqstack s;
- initstack(s);
- while (1) {
- menu(s);
- system("cls");
- }
-
- }
初始化栈
- typedef struct StackNode
- {
- int data;
- struct StackNode *next;
- }StackNode,*LinkStack;
-
- void InitStack(LinkStack &S)
- {
- S=NULL;
- }
链栈的入栈
- void Push(LinkStack &S,int e)
- {
- LinkStack p=new StackNode;
- //或者 LinkStack p=(StackNode*)malloc(sizeof(StackNode));
- p->data=e;
- p->next=S;
- S=p;
- }
链栈的出栈
- void Pop(LinkStack &L,int &e)
- {
- if(S==NULL){
- cout<<"栈空";
- return ;
- }
- LinkStack p=S;
- e=p->data; //用e保存要出栈数据的信息
- S=S->next;
- delete p;
- //或者free(p);
- }
取链栈的栈顶元素
- int GetTop(LinkStack)
- {
- if(S==NULL){
- cout<<"栈空";
- }
- return S->data;
-
- }
完整代码
- //链栈的表示和实现
- typedef struct StackNode
- {
- int data;
- struct StackNode* next;
- }StackNode,*LinkStack;
- //链栈的初始化
- void InitStack(LinkStack& S)
- {
- S = NULL;
- }
- //链栈的入栈
- void Push(LinkStack& S, int e)
- {
- LinkStack p = new StackNode;
- p->data = e;
- p->next = S;
- S = p;
- }
- //链栈的出栈
- void Pop(LinkStack& S,int &e)
- {
- if (S == NULL) {
- cout << "栈空!" << endl;
- system("pause");
- return;
- }
- LinkStack p = S;
- e = p->data;
- S = S->next;
- delete p;
- }
- //取链栈的栈顶元素
- int GetTop(LinkStack S)
- {
- if (S == NULL) {
- cout << "栈空!" << endl;
-
- }
- return S->data;
- }
- //菜单
- void Mune(LinkStack& S)
- {
- int e;
- cout << "************************" << endl;
- cout << "| 1.入栈 |" << endl;
- cout << "| 2.出栈 |" << endl;
- cout << "| 3.取栈顶元素 |" << endl;
- cout << "| 4.退出 |" << endl;
- cout << "************************" << endl;
- cout << "input your choice(1~4):";
- int i;
- cin >> i;
- switch (i)
- {
- case 1:
- cout << "请输入你要入栈的元素:";
- cin >> e;
- Push(S, e);
- system("pause");
- break;
- case 2:
- Pop(S,e);
- if (e > 0) {
- cout << "你出栈的元素是:" << e << endl;
- system("pause");
- }
- break;
- case 3:
- cout << "栈顶元素为:" << GetTop(S) << endl;
- system("pause");
- break;
- case 4:
- exit(0);
- default:
- cout << "输入非法!";
- system("pause");
- break;
- }
-
- }
- //主函数
- int main()
- {
- LinkStack S = new StackNode;
- InitStack(S);
- while (1) {
- Mune(S);
- system("cls");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。