偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C++數(shù)據(jù)結構學習之棧和隊列

開發(fā) 后端
在C++中,棧和隊列是操作受限的線性表,好像每本講數(shù)據(jù)結構的書都是這么說的。有些書按照這個思路給出了定義和實現(xiàn);但是很遺憾,本文沒有這樣做。本文從鏈式結構角度給出棧和隊列的定義和實現(xiàn)。

  在C++數(shù)據(jù)結構學習中,順序表示的隊列,必須預先分配空間,并且空間大小受限,使用起來限制比較多。而且,由于限定存取位置,順序表示的隨機存取的優(yōu)點就沒有了,所以,鏈式結構應該是***。

  棧的定義和實現(xiàn)

  1. #ifndef Stack_H  
  2. #define Stack_H   
  3. #include "List.h"  
  4.  
  5. template <class Type> class Stack : List//棧類定義  
  6. {  
  7.  public:  
  8.   void Push(Type value)  
  9.   {  
  10.    Insert(value);  
  11.   }  
  12.  
  13.  Type Pop()  
  14.  {  
  15.   Type p = *GetNext();  
  16.   RemoveAfter();  
  17.   return p;  
  18.  }  
  19.  
  20.  Type GetTop()  
  21.  {  
  22.   return *GetNext();  
  23.  }  
  24.  
  25.  List ::MakeEmpty;  
  26.  List ::IsEmpty;  
  27.  
  28. };  
  29.  
  30. #endif 

  隊列的定義和實現(xiàn)

  1. #ifndef Queue_H  
  2. #define Queue_H  
  3. #include "List.h"  
  4.  
  5. template <class Type> class Queue : List//隊列定義  
  6. {  
  7.  public:  
  8.   void EnQueue(const Type &value)  
  9.   {  
  10.    LastInsert(value);  
  11.   }  
  12.  
  13.  Type DeQueue()  
  14.  {   
  15.   Type p = *GetNext();  
  16.   RemoveAfter();  
  17.   IsEmpty();  
  18.   return p;  
  19.  }  
  20.  
  21.  Type GetFront()  
  22.  {  
  23.   return *GetNext();  
  24.  }  
  25.  
  26.  List ::MakeEmpty;  
  27.  List ::IsEmpty;  
  28.  
  29. };  
  30. #endif 

  測試程序

  1. #ifndef StackTest_H  
  2. #define StackTest_H  
  3. #include "Stack.h"  
  4.  
  5. void StackTest_int()  
  6. {  
  7.  cout << endl << "整型棧測試" << endl;  
  8.  cout << endl << "構造一個空棧" << endl;  
  9.  Stack<int> a;  
  10.  cout << "將1~20入棧,然后再出棧" << endl;  
  11.  for (int i = 1; i <= 20; i++) a.Push(i);  
  12.   while (!a.IsEmpty()) cout << a.Pop() << ' ';  
  13.   cout << endl;  
  14. }  
  15. #endif  
  16.  
  17. #ifndef QueueTest_H  
  18. #define QueueTest_H  
  19. #include "Queue.h"  
  20.  
  21. void QueueTest_int()  
  22. {  
  23.  cout << endl << "整型隊列測試" << endl;  
  24.  cout << endl << "構造一個空隊列" << endl;  
  25.  Queue<int> a;  
  26.  cout << "將1~20入隊,然后再出隊" << endl;  
  27.  for (int i = 1; i <= 20; i++) a.EnQueue(i);  
  28.  while (!a.IsEmpty()) cout << a.DeQueue() << ' ';  
  29.  cout << endl;  
  30. }  
  31. #endif 

  沒什么好說的,你可以清楚的看到,在單鏈表的基礎上,棧和隊列的實現(xiàn)是如此的簡單。

  如讀者希望繼續(xù)閱讀棧和隊列的應用,請閱讀拓展文章C++數(shù)據(jù)結構學習之棧的應用C++數(shù)據(jù)結構學習之隊列的應用 。

【編輯推薦】

  1. 淺析C++棧使用方法
  2. 18.3.1 隊列的概念
  3. 數(shù)據(jù)庫使用C++數(shù)據(jù)結構
  4. 程序員必看 c++筆試題匯總
  5. c++編程常用工具
責任編輯:韓亞珊 來源: 天極網(wǎng)
相關推薦

2011-04-11 12:22:11

數(shù)據(jù)結構C++

2011-04-11 12:48:36

隊列數(shù)據(jù)結構C++

2011-04-11 17:09:37

稀疏矩陣矩陣C++

2012-02-02 10:21:05

單鏈表nexthead

2020-12-17 10:12:33

數(shù)據(jù)結構算法隊列

2022-03-31 11:17:58

JavaScript數(shù)組方法

2021-07-16 07:57:34

Python數(shù)據(jù)結構

2024-01-15 06:01:36

C++數(shù)組

2009-08-11 14:51:11

C#數(shù)據(jù)結構與算法

2009-08-11 14:43:42

C#數(shù)據(jù)結構與算法

2009-08-13 16:02:29

C#結構

2010-01-27 15:58:35

C++數(shù)據(jù)結構

2023-12-13 10:01:15

數(shù)據(jù)結構c++編程

2021-06-08 06:01:00

C++數(shù)據(jù)結構向量和數(shù)組

2011-07-20 17:10:54

C++

2022-09-01 16:27:19

JavaScriptWeb開發(fā)

2010-07-19 11:07:13

Perl控制結構

2012-05-16 17:05:33

Java數(shù)據(jù)結構

2020-10-28 10:10:03

Java單鏈表數(shù)據(jù)結構

2009-08-12 18:35:17

C#數(shù)據(jù)結構
點贊
收藏

51CTO技術棧公眾號