http://blog.csdn.net/fisherwan/article/details/21479649
?棧和隊列都有兩種實現方式,一種在之前已經寫過了,是鏈式存儲形式,另一種是順序存儲形式。也就是這里所寫的用數組的形式實現,和鏈式存儲形式相比,有幾個不同的地方。
- 順序存儲的方式,必須確定棧和隊列的大小,也就是要確定數組的大小。而鏈式儲存是動態分配的,根據需要來增減。
- 順序存儲的方式有溢出的現象,由于是數組存儲,所以超出數組下標的時候就溢出了。
? ? ? ? 下面上代碼:
SequentialStack.h 順序存儲棧頭文件
[cpp]?view plain?copy
- #ifndef?_SEQUENTIALSTACK_H_H??
- #define?_SEQUENTIALSTACK_H_H??
- ??
- typedef?int?SStackEle;??
- const?int?MAXSTACK?=?20;??
- ??
- typedef?struct?SSTACK??
- {??
- ????SStackEle?arrele[MAXSTACK];??
- ????SStackEle?top;??
- }SStack;??
- ??
- //初始化順序棧??
- void?InitSStack(SStack?&s);??
- ??
- //壓入棧??
- void?PushSStack(SStack?&s,?SStackEle?ele);??
- ??
- //出棧??
- void?PopSStack(SStack?&s,?SStackEle?&ele);??
- ??
- //判斷棧是否為空??
- bool?IsemptySStack(SStack?s);??
- ??
- //得到棧頂元素??
- SStackEle?GetTopSStack(SStack?s);??
- ??
- #endif??
[cpp]?view plain?copy
- #ifndef?_SEQUENTIALQUEUE_H_H??
- #define?_SEQUENTIALQUEUE_H_H??
- ??
- typedef?int?SQueueEle;??
- const?int?MAXQUEUE?=?10;??
- ??
- typedef?struct?SQUEUE??
- {??
- ????SQueueEle?arrele[MAXQUEUE];??
- ????SQueueEle?front,?rear;??
- }SQueue;??
- ??
- //初始化順序隊列??
- void?InitSQueue(SQueue?&q);??
- ??
- //入隊??
- void?EnSQueue(SQueue?&q,?SQueueEle?ele);??
- ??
- //出隊??
- void?DeSQueue(SQueue?&q,?SQueueEle?&ele);??
- ??
- //判斷隊列是否為空??
- bool?IsemptySQueue(SQueue?q);??
- ??
- //獲得隊頭元素??
- SQueueEle?GetFrontSQueue(SQueue?q);??
- ??
- #endif??
SequentialStack.cpp 順序存儲棧源文件
[cpp]?view plain?copy
- #include?"SequentialStack.h"??
- #include?<stdio.h>??
- ??
- //初始化順序棧??
- void?InitSStack(SStack?&s)??
- {??
- ????s.top?=?-1;??
- }??
- ??
- //壓入棧??
- void?PushSStack(SStack?&s,?SStackEle?ele)??
- {??
- ????s.top++;??
- ????if?(s.top?<?MAXSTACK)??
- ????????s.arrele[s.top]?=?ele;??
- ????else??
- ????????printf("棧滿,不能進行壓入操作!\n");??
- }??
- ??
- //出棧??
- void?PopSStack(SStack?&s,?SStackEle?&ele)??
- {??
- ????if?(s.top?<?0)??
- ????????printf("棧空,不能進行出棧操作!\n");??
- ????else??
- ????{??
- ????????ele?=?s.arrele[s.top];??
- ????????s.top--;??
- ????}??
- }??
- ??
- //判斷順序棧是否為空??
- bool?IsemptySStack(SStack?s)??
- {??
- ????if?(s.top?=?-1)??
- ????????return?true;??
- ????else??
- ????????return?false;??
- }??
- ??
- //獲得棧頂元素值??
- SStackEle?GetTopSStack(SStack?s)??
- {??
- ????if?(s.top?<?0)??
- ????????printf("棧空,不能獲得棧頂元素值!\n");??
- ????else??
- ????????return?s.arrele[s.top];??
- }??
SequentialQueue.cpp 順序存儲隊列源文件
[cpp]?view plain?copy
- #include?"SequentialQueue.h"??
- #include?<stdio.h>??
- ??
- //初始化順序隊列??
- void?InitSQueue(SQueue?&q)??
- {??
- ????q.front?=?q.rear?=?-1;??
- }??
- ??
- //入隊列??
- void?EnSQueue(SQueue?&q,?SQueueEle?ele)??
- {??
- ????if?(q.rear?>=?MAXQUEUE)??
- ????????printf("隊列滿,不能進行入隊操作!\n");??
- ????else??
- ????????q.arrele[++q.rear]?=?ele;??
- }??
- ??
- //出隊列??
- void?DeSQueue(SQueue?&q,?SQueueEle?&ele)??
- {??
- ????if?(IsemptySQueue(q))??
- ????????printf("隊列空,不能進行出隊操作!\n");??
- ????else??
- ????????ele?=?q.arrele[++q.front];??
- }??
- ??
- //判斷隊列是否為空??
- bool?IsemptySQueue(SQueue?q)??
- {??
- ????if?(q.front?==?q.rear)??
- ????????return?true;??
- ????else??
- ????????return?false;??
- }??
- ??
- //獲得隊頭元素值??
- SQueueEle?GetFrontSQueue(SQueue?q)??
- {??
- ????if?(IsemptySQueue(q))??
- ????????printf("隊空,不能獲得隊頭元素值!\n");??
- ????else??
- ????????return?q.arrele[q.front?+?1];??
- }??
main.cpp 測試程序源文件
[cpp]?view plain?copy
- #include?<stdio.h>??
- #include?"SequentialStack.h"??
- #include?"SequentialQueue.h"??
- ??
- int?main()??
- {??
- ????/*順序棧測試代碼*/??
- ????int?ele;??
- ????SStack?s;??
- ????InitSStack(s);??
- ??
- ????PushSStack(s,?0);??
- ????PushSStack(s,?1);??
- ????PushSStack(s,?2);??
- ??
- ????printf("棧頂元素值為:%d\n",?GetTopSStack(s));??
- ??
- ????PopSStack(s,?ele);??
- ????printf("出棧第1個元素是:%d\n",?ele);??
- ??
- ????printf("棧頂元素值為:%d\n",?GetTopSStack(s));??
- ??
- ????PopSStack(s,?ele);??
- ????printf("出棧第2個元素是:%d\n",?ele);??
- ????PopSStack(s,?ele);??
- ????printf("出棧第3個元素是:%d\n",?ele);??
- ??
- ????PopSStack(s,?ele);??
- ??
- ????if?(IsemptySStack(s))??
- ????????printf("棧為空!\n");??
- ????putchar('\n');??
- ????/*順序隊列測試代碼*/??
- ????SQueue?q;??
- ????InitSQueue(q);??
- ??
- ????EnSQueue(q,?0);??
- ????EnSQueue(q,?1);??
- ????EnSQueue(q,?2);??
- ??
- ????printf("隊頭元素值為:%d\n",?GetFrontSQueue(q));??
- ??
- ????DeSQueue(q,?ele);??
- ????printf("出隊第1個元素是:%d\n",?ele);??
- ??
- ????printf("隊頭元素值為:%d\n",?GetFrontSQueue(q));??
- ??
- ????DeSQueue(q,?ele);??
- ????printf("出隊第2個元素是:%d\n",?ele);??
- ????DeSQueue(q,?ele);??
- ????printf("出隊第3個元素是:%d\n",?ele);??
- ??
- ????DeSQueue(q,?ele);??
- ??
- ????if?(IsemptySQueue(q))??
- ????????printf("隊列為空!\n");??
- ??
- ????return?0;??
- }??
下面附測試結果圖一張:
PS:個人測試沒有問題,如果大家發現問題請及時聯系,不勝感激!