鏈式儲存的佇列的實現

2021-09-06 11:39:04 字數 3990 閱讀 9248

2013-08-18 20:07:25

鏈式儲存的佇列的實現,包括佇列的初始化、銷毀、入隊、出隊、測長、獲取隊首元素等基本操作。

注意幾點:

佇列結構,包含乙個頭指標、乙個尾指標,初始化為空佇列,空佇列的隊首指標與隊尾指標相同;鍊錶包含頭結點,否則在隊列為空以及佇列只有乙個元素時都是隊尾至真與隊首指標相等,無法區分有動態分配空間,就要釋放,本實現中用destoryqueue釋放動態記憶體;帶有頭結點,佇列的第乙個元素在頭指標指向的結點中,對非空佇列,佇列中第乙個元素為q.front->next->data; 而非q.front->data;在出隊以及獲取隊首元素時,要考慮佇列空否,本實現中用assert( !isqueueempty(q) ); 檢查。

1 #include 2 #include 3

using

namespace

std;

45 typedef int

datatype;

67 typedef struct

node

8lnode,*plnode;

1213

//佇列結構,包含乙個頭指標、乙個尾指標

14 typedef struct

queue

15queue;

1920

//初始化佇列,鍊錶包含頭結點,

21//

否則在隊列為空以及佇列只有乙個元素時都是隊尾至真與隊首指標相等,無法區分

22void initqueue(queue &q)

2328

29//

佇列銷毀,即兩個棧的銷毀

30void destoryqueue(queue &q)

3141}42

43//

判斷佇列空否

44bool isqueueempty(queue &q)

4548

49//

出隊,從隊首刪除

50 datatype dequeue(queue &q)

5161

62//

q.front = q.front->next->next;

//錯誤的更新,帶有頭結點,佇列的第乙個元素在頭指標指向的結點中

63 q.front->next = q.front->next->next; //

更新隊首

64 delete pnodetodelete; //

刪除舊的隊首結點

6566

return

frontdata;67}

6869

//入隊,從隊尾插入

70void enqueue(queue &q,datatype data)

7179

80//

獲取隊首元素

81 datatype getheadofqueue(queue &q)

8287

88//

佇列測長

89 size_t getlengthofqueue(queue &q)

9099

100return

lengthofqueue;

101}

102103

//從隊首到隊尾顯示佇列元素

104void displayqueue(queue &q)

105113

114 cout<115}

116117

//佇列測試

118void

testqueue()

119153

154case

(pop):

155166

167case

(getlength):

168173

174case

(gethead):

175180

case

(isempty):

181186

default

:187

break

;188

}189 cout<<"

please enter the command ,end with ctrl+z :

"<190}

191192 destoryqueue(q); //

銷毀佇列

193}

194195

intmain()

196

測試結果:

1 please enter the command ,end with ctrl+z :22

3 the length of queue is : 0

4 please enter the command ,end with ctrl+z :50

6please enter z data to push :71

8 enqueue 1

9the queue before enqueue :

1011

the queue after enqueue :121

13 please enter the command ,end with ctrl+z :140

15please enter z data to push :162

17 enqueue 2

18the queue before enqueue :191

20the queue after enqueue :211

222 please enter the command ,end with ctrl+z :230

24please enter z data to push :253

26 enqueue 3

27the queue before enqueue :281

229the queue after enqueue :301

2331 please enter the command ,end with ctrl+z :322

33 the length of queue is : 3

34 please enter the command ,end with ctrl+z :353

36 the top element of stack is : 1

37 please enter the command ,end with ctrl+z :381

39the queue before dequeue :401

2341the queue after dequeue :422

343 please enter the command ,end with ctrl+z :443

45 the top element of stack is : 2

46 please enter the command ,end with ctrl+z :471

48the queue before dequeue :492

350the queue after dequeue :513

52 please enter the command ,end with ctrl+z :531

54the queue before dequeue :553

56the queue after dequeue :

5758 please enter the command ,end with ctrl+z :591

60the queue before dequeue :

6162 assertion failed: !isqueueempty(q), file e:\visual studio 2010_projects\link_que

63 ue_2013_08_18\link_queue_2013_08_18\link_queue.cpp, line 53

64 請按任意鍵繼續. . .

佇列的鏈式儲存實現

include include define true 1 define false 0 define ok 1 define error 0 define overflow 02 typedef int qelemtype typedef int status storage structure ...

佇列的順序 鏈式儲存實現

佇列 具有一定操作約束的線性表,只能在一端插入,在另一端刪除。特點 先來先服務,先進先出表 頭front,尾rear 順序儲存 1 define maxsize 儲存資料元素的最大個數 23 struct qnode 1213 typedef struct qnode queue 14 front ...

佇列的鏈式儲存

include include include typedef int elemtype typedef struct qnodequeuenode,queuenodeptr typedef struct qlistlistqueue,listqueueptr listqueueptr initqu...