新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++數(shù)據(jù)結(jié)構(gòu)學習之棧和隊列
在C++數(shù)據(jù)結(jié)構(gòu)學習中,順序表示的棧和隊列,必須預先分配空間,并且空間大小受限,使用起來限制比較多。而且,由于限定存取位置,順序表示的隨機存取的優(yōu)點就沒有了,所以,鏈式結(jié)構(gòu)應(yīng)該是***。

開平網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),開平網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為開平上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的開平做網(wǎng)站的公司定做!
棧的定義和實現(xiàn)
- #ifndef Stack_H
- #define Stack_H
- #include "List.h"
- template
class Stack : List //棧類定義 - {
- public:
- void Push(Type value)
- {
- Insert(value);
- }
- Type Pop()
- {
- Type p = *GetNext();
- RemoveAfter();
- return p;
- }
- Type GetTop()
- {
- return *GetNext();
- }
- List ::MakeEmpty;
- List ::IsEmpty;
- };
- #endif
隊列的定義和實現(xiàn)
- #ifndef Queue_H
- #define Queue_H
- #include "List.h"
- template
class Queue : List //隊列定義 - {
- public:
- void EnQueue(const Type &value)
- {
- LastInsert(value);
- }
- Type DeQueue()
- {
- Type p = *GetNext();
- RemoveAfter();
- IsEmpty();
- return p;
- }
- Type GetFront()
- {
- return *GetNext();
- }
- List ::MakeEmpty;
- List ::IsEmpty;
- };
- #endif
測試程序
- #ifndef StackTest_H
- #define StackTest_H
- #include "Stack.h"
- void StackTest_int()
- {
- cout << endl << "整型棧測試" << endl;
- cout << endl << "構(gòu)造一個空棧" << endl;
- Stack
a; - cout << "將1~20入棧,然后再出棧" << endl;
- for (int i = 1; i <= 20; i++) a.Push(i);
- while (!a.IsEmpty()) cout << a.Pop() << ' ';
- cout << endl;
- }
- #endif
- #ifndef QueueTest_H
- #define QueueTest_H
- #include "Queue.h"
- void QueueTest_int()
- {
- cout << endl << "整型隊列測試" << endl;
- cout << endl << "構(gòu)造一個空隊列" << endl;
- Queue
a; - cout << "將1~20入隊,然后再出隊" << endl;
- for (int i = 1; i <= 20; i++) a.EnQueue(i);
- while (!a.IsEmpty()) cout << a.DeQueue() << ' ';
- cout << endl;
- }
- #endif
沒什么好說的,你可以清楚的看到,在單鏈表的基礎(chǔ)上,棧和隊列的實現(xiàn)是如此的簡單。
如讀者希望繼續(xù)閱讀棧和隊列的應(yīng)用,請閱讀拓展文章C++數(shù)據(jù)結(jié)構(gòu)學習之棧的應(yīng)用和C++數(shù)據(jù)結(jié)構(gòu)學習之隊列的應(yīng)用 。
網(wǎng)站名稱:C++數(shù)據(jù)結(jié)構(gòu)學習之棧和隊列
URL標題:http://m.fisionsoft.com.cn/article/dhghgco.html


咨詢
建站咨詢
