資料結構 佇列

2021-10-24 01:57:29 字數 2473 閱讀 6025

佇列:

一種操作受限的線性表,只能在隊尾插入、隊頭刪除。

特點:先進先出 (fifo)

1.順序隊

方案一 (犧牲乙個儲存單元)

//順序隊的定義

typedef

struct

sqqueue;

----

----

----

----

----

----

----

----

----

-------

#define maxsize 10

intmain()

//起始時,兩個指標處於同一位置,因此當兩個指標重合時表示隊空

//判斷隊空

intisempty

( sqqueue q )

//入隊 (增)

intenqueue

( sqqueue &q, elemtype x )

return1;

}//通過%maxsize取餘運算將記憶體變成了邏輯上的環狀

//出隊 (刪)

intdequeue

( sqqueue &q, elemtype &x )

//刪除乙個元素,並用x返回該元素的值

//獲得隊頭元素的值 (查)

intgethead

(sqqueue q, elemtype &x )

//佇列中的元素個數

n =(rear+maxsize-front)

%maxsize;

方案二 (不浪費儲存空間-size)

typedef

struct

sqqueue;

//初始化時

front=rear=0;

size =0;

插入成功 size++

;出隊成功 size--

;隊空條件:szie ==

0隊滿條件:size == maxsize

方案三 (不浪費儲存空間-tag)

typedef

struct

sqstack;

//初始化時

front = rear =0;

tag =0;

//每次插入成功時,都令tag=1;

//每次刪除成功時,都令tag=0;

隊空條件:front==rear && tag==

0隊滿條件:front==rear && tag==

1//只有插入操作才可能使隊滿,只有刪除操作才可能使隊空

2.鏈隊

//鏈隊的定義

typedef

struct linknodelinknode;

//鏈隊結點的定義

typedef

struct

linkqueue;

//定義了乙個結點,裡面包含乙個隊頭指標和乙個隊尾指標

//鏈隊的初始化(帶頭結點)

void

initqueue

( linkqueue &q )

//判斷隊空

intisempty

(linkqueue q )

//初始化(不帶頭結點)

void

initqueue

( linkqueue &q )

帶頭結點的鏈隊示意圖

//鏈隊的入隊(帶頭結點)——增

void

enqueue

(linkqueue &q ,elemtype x )

//入隊(不帶頭結點)

void

enqueue

(linkqueue &q, elemtype x )

不帶頭結點的鏈隊示意圖

//鏈隊的出隊(帶頭結點)

intdequeue

( linkqueue &q, elemtype &x )

//(不帶頭結點)

intdequeue

( linkqueue &q, elemtype &x )

free

(p);

return1;

}

資料結構 佇列

一 佇列的迴圈陣列實現。1 初始化 空佇列。令rear front 0。2 入佇列 約定rear指向佇列尾元素的下乙個位置。入佇列時,先判斷佇列是否已滿,而後將array rear x 然後rear 3 出佇列 約定front指向佇列的首元素位置。出佇列時,先判斷佇列是否為空,而後返回隊首元素re ...

資料結構 佇列

資料參考自 資料結構c 語言描述 佇列是一種先進先出的資料結構,這與棧正好相反。下例是簡單的queue實現 queue.h檔案 ifndef queue h define queue h include include 資料元素結構 自定義 struct datatype 佇列元素最大數 const...

資料結構 佇列

code for fun created by dream whui 2015 1 25 include stdafx.h include include using namespace std define true 1 define false 0 define ok 1 define erro...