資料結構與演算法 (棧的基本操作)

2021-10-10 17:21:40 字數 1844 閱讀 7595

//定義節點結構

#define maxsize 《陣列元素個數》

typedef

struct snodestack;

//建立棧

stack*

createstack()

//壓棧操作

void push (stack* ptrs,elemtype item)

else

}

//出棧

elemtype pop

(stack* ptrs)

else

}

//判斷棧是否為空

bool

isempty

(stack* ptrs)

//判斷棧是否已滿

bool

isfull

(stack* ptrs)

//求棧的深度

intsize

(stack* ptrs)

//獲取棧頂元素

elemtype top

(stack* ptrs)

return ptrs-

>data[top]

;}

用陣列實現棧時由於要預先分配後好儲存空間,故在大多數時候會造成空間資源的浪費,因此為了提高空間利用率,採用兩個棧各自向中間延伸的方法。

#define maxsize 《資料最大儲存量》

typedef

struct snode

stack;

void

initstack

(stack* ptrs)

void

push

(stack* ptrs,elemtype item,

int tag)

else

if(tag==1)

else

if(tag==2)

}

elemtype pop

(stack* ptrs,

int tag)

else

return ptrs-

>data[ptrs-

>top1--];

}else

else

return ptrs-

>data[ptrs-

>top2--];

}}

棧的鏈式儲存結構實際上是乙個單鏈表,棧的入棧與出棧只能在鍊錶的頭部進行。

//定義鍊錶的棧節點

typedef

struct snode

stack;

/*構建乙個煉表頭指標*/

stack*

createstack()

void

push

(stack* ptrs,elemtype item)

elemtype pop

(stack* ptrs)

bool

isempty

(stack* ptrs)

資料結構與演算法 棧 定義與操作

include include 棧 複習 adt stack is operations 1.stack createemptystack void 建立空棧 2.int isemptystack void 判斷是否為空棧 3.void push stack st,datatype x 向棧st的棧...

資料結構與演算法(佇列的基本操作)

佇列的結構定義 define size 佇列的最大長度 typedef struct qnode queue 建立乙個空佇列 queue createqueue int size 我們讓佇列中頭部位置與尾部位置相等時代表為空 頭部與尾部相差為1 時代表佇列已滿 判斷隊列為空 bool isempty...

資料結構與演算法 佇列的基本操作

佇列 順序佇列 鏈式佇列 一般常用順序迴圈佇列 空隊標誌 front rear 頭指標和尾指標相等 迴圈佇列解決隊滿時判斷方法 少用乙個元素空間 rear 1 maxqsize front 迴圈佇列的型別定義 define maxqsize 100 最大佇列長度 typedef struct sqq...