Tag: Kong storage space des out EFI space structure language data structures
Stack: Stack is a special kind of linear table. The peculiarity is that the manipulation of inserting and deleting data elements can only be done at one end of the linear table.
/**** 2017.11.3** ahthor:799** Stack is implemented *****/#include <bits/stdc++.h>using namespace std; #define STACK_INIT_SIZE 1 00///Storage space Initial allocation # Stack_increment 10///Storage Allocation Increment # define Elemtype int#define Status int#define OK 1#define error-1#d Efine overflow-2typedef struct{elemtype *base; Elemtype *top; int stacksize;} Sqstack; Status Initstack (Sqstack &s) {s.base = (Elemtype *) malloc (stack_init_size * sizeof (elemtype)); if (! s.base) exit (OVERFLOW); S.top = S.base; S.stacksize = stack_init_size; return OK;} Status Destroystack (Sqstack &s) {for (Elemtype *index = s.base; Index! = s.top; index + +) {free (index); } free (&s); return OK;} Status Clearstack (Sqstack &s) {for (Elemtype *index = s.base+1; Index! = s.top; index + +) {free (index); } s.top = S.base; return OK;} Status Isemptystack (Sqstack &s) {if (s.base = = S.top) return true; else return false;} Status Lengthstack (Sqstack &s) {RetuRN S.top-s.base;} Elemtype GetTop (Sqstack &s) {if (s.top = = s.base) return ERROR; Elemtype e = * (s.top-1); return e;} Status Push (sqstack &s,elemtype &e) {if (s.top-s.base >= s.stacksize) {s.base = (Elemtype *) re Alloc (S.base, (s.stacksize + stack_increment) * sizeof (elemtype)); if (! s.base) exit (OVERFLOW); S.top = S.base + s.stacksize; S.stacksize + = stack_increment; } *s.top = e; S.top + +; return OK;} Status Pop (Sqstack &s, Elemtype &e) {if (s.top = = s.base) return ERROR; e = *--S.TOP; return OK;} Status Trversestack (Sqstack &s) {for (Elemtype *index = s.base; Index! = s.top; index + +) {cout<<* index<< ""; } cout<<endl; return OK;} int main () {Sqstack * St = (Sqstack *) malloc (sizeof (sqstack)); Initstack (*ST); int n; cin>>n; while (n--) {int tmp; cin>>tmp; Push (*ST,TMP); } int num; Pop (*st,nUM); cout<< "num" <<num<<endl; Trversestack (*ST); cout<< "Top" <<gettop (*st) <<endl; cout<< "Len:" <<lengthstack (*st) <<endl; Clearstack (*ST); if (Isemptystack (*st)) cout<< "Kong" <<endl; else cout<< "No" <<endl; return 0;}
Data structure (c language) stack--sequential implementation