标签:stream push efi -- main 顺序存储 def fine space
偷个懒,整合一下书上代码就得到了
顺序存储:
1 #include<iostream> 2 #include<cstdlib> 3 #define MaxSize 30 4 5 using namespace std; 6 7 struct SqStack { 8 int data[MaxSize]; 9 int top; 10 }; 11 12 void InitStack(SqStack *& s) { 13 s = (SqStack *)malloc(sizeof(SqStack)); 14 s->top = -1; 15 } 16 17 void DestroyStack(SqStack *& s) { 18 free(s); 19 } 20 21 bool StackEmpty(SqStack * s) { 22 return (s->top == -1); 23 } 24 25 bool Push(SqStack *& s,int e) { 26 if(s->top == MaxSize - 1) 27 return false; 28 s->top++; 29 s->data[s->top] = e; 30 return true; 31 } 32 33 bool Pop(SqStack *& s,int & e) { 34 if(s->top == -1) 35 return false; 36 e = s->data[s->top]; 37 s->top--; 38 return true; 39 } 40 41 bool GetTop(SqStack *& s,int & e) { 42 if(s->top == -1) 43 return false; 44 e = s->data[s->top]; 45 return true; 46 } 47 48 int main() 49 { 50 SqStack * s; 51 int e = 9; 52 InitStack(s); 53 cout<<StackEmpty(s)<<endl; 54 cout<<Push(s,5)<<endl; 55 cout<<GetTop(s,e)<<endl; 56 cout<<Pop(s,e)<<" "<<e<<endl; 57 DestroyStack(s); 58 return 0; 59 }
链式存储:
1 #include<iostream> 2 #include<cstdlib> 3 #define MaxSize 30 4 5 using namespace std; 6 7 struct LiStack { 8 int data; 9 struct LiStack * next; 10 }; 11 12 void InitStack(LiStack *& s) { 13 s = (LiStack *)malloc(sizeof(LiStack)); 14 s->next = NULL; 15 } 16 17 void DestroyStack(LiStack *& s) { 18 LiStack * p = s,* q = s->next; 19 while(q != NULL) { 20 free(p); 21 p = q; 22 q = p->next; 23 } 24 free(p); 25 } 26 27 bool StackEmpty(LiStack * s) { 28 return (s->next == NULL); 29 } 30 31 void Push(LiStack *& s,int e) { 32 LiStack * p; 33 p = (LiStack *)malloc(sizeof(LiStack)); 34 p->data = e; 35 p->next = s->next; 36 s->next = p; 37 } 38 39 bool Pop(LiStack *& s,int & e) { 40 LiStack * p; 41 if(s->next == NULL) 42 return false; 43 p = s->next; 44 e = p->data; 45 s->next = p->next; 46 free(p); 47 return true; 48 } 49 50 bool GetTop(LiStack *& s,int & e) { 51 if(s->next == NULL) 52 return false; 53 e = s->next->data; 54 return true; 55 } 56 57 int main() 58 { 59 LiStack * s; 60 int e = 9; 61 InitStack(s); 62 cout<<StackEmpty(s)<<endl; 63 Push(s,5); 64 cout<<GetTop(s,e)<<endl; 65 cout<<Pop(s,e)<<" "<<e<<endl; 66 DestroyStack(s); 67 return 0; 68 }
2020-05-25
标签:stream push efi -- main 顺序存储 def fine space
原文地址:https://www.cnblogs.com/2015-16/p/12957964.html