栈的实现

来源:互联网 发布:46007小鱼儿最近域名 编辑:程序博客网 时间:2024/05/03 14:37
#include <stdio.h>#include <stdlib.h>#include<iostream>#include<cstdlib>#include <iomanip>#include<string>#include<ctime>#define MAX_SIZE 8using std::cout;using std::endl;using std::setw;using std::cin;using std::string;using std::time;using std::srand;struct Stack{int* top;int* bottom;int length = 0;int maxsize;};Stack Init(Stack* s,int m){s -> bottom = (int*)malloc(m * sizeof(int));s -> top = s -> bottom;s -> maxsize = m;return *s;}Stack push(Stack* s,int e){if(s ->length >= s ->maxsize){s -> bottom = (int*)realloc(s ->bottom,((s ->length)+1) * sizeof(int));s -> top = s -> bottom +s -> maxsize;s -> maxsize++;cout<<"我要加啦"<<endl;}*(s -> top) = e;(s -> top)++;(s -> length)++;}Stack output(Stack* s){int* count = s -> bottom;for(int i = 1;i <= s -> length;i++){cout<<*(count)<<endl;count++;}}int pop(Stack* s){if(s -> bottom == s -> top){cout<<"栈空了"<<endl;return -1;}(s -> top) -= 1;(s -> length)--;}int main(){Stack stack1;stack1 = Init(&stack1,3);push(&stack1,1);push(&stack1,2);push(&stack1,3);push(&stack1,4);pop(&stack1);push(&stack1,9);cout<<*(stack1.bottom)<<endl;output(&stack1);cout<<*(stack1.bottom)<<endl;    return 0;}

0 0
原创粉丝点击