資料結構筆記 堆疊

2021-08-05 18:59:07 字數 1870 閱讀 7811

1.特點:

2.堆疊adt

3.堆疊的順序實現

//**未測試

#include

#include

using

namespace

std;

template

class stack;

template

stack ::stack(int n)

template

stack ::~stack()

template

bool isfull()

template

bool isempty()

template

bool pop(t &x)

}template

bool push(t x)

}

4.題目:用乙個陣列實現兩個堆疊,要求最大程度的利用陣列空間,使陣列只要有空間入棧操作就可以成功。

分析:可以使兩個棧分別從陣列的兩頭開始向中間增長;

當兩個堆疊指標相遇時就表示兩個棧都滿了;

在執行push、pop操作時增添乙個tag變數用於區分是對第乙個堆疊操作還是對第二個堆疊操作;

//**未測試

#include

#include

using

namespace

std;

template

class dstack();

template

dstack ::dstack(int n)

template

dstack ::~dstack()

template

bool dstack ::isfull()

template

bool dstack ::isempty()

template

bool dstack ::push(t x,int tag)

else

}}template

bool dstack ::pop(t &x,int tag)

if(top2==maxsize) return

false;

x = s[top2++]; return

true;

}

5.堆疊的鏈式儲存

棧的鏈式儲存實際上是乙個單鏈表,叫做鏈棧;

由於插入和刪除只能在鏈棧的棧頂進行,所以棧頂應當取鍊錶的頭部,方便插入刪除的進行。(尾部不方便刪除元素)

//**未測試

#include

#include

using

namespace

std;

//用帶頭節點的鍊錶實現堆疊

template

class tnode;

template

class linestack;

template

linestack ::linestack()

template

linestack ::~linestack()

}bool linestack ::isfull()

if(countreturn

false;

else

return

true;

}bool linestack ::pop(t &x)

return

false;

} bool linestack ::push(t x)

}

資料結構 堆疊

對於棧,想必大家都十分熟悉了,也能很快的答出棧是乙個先進後出的佇列。但是在平常程式設計的生活中應用的十分少。在acm中,棧是一種十分重要的資料結構 其他領域也一樣 我們可以用這種資料結構解決一些十分棘手的問題,大大提高了程式的效率。有這樣一道名為software bugs 的題。題目的意思簡要來說就...

資料結構 堆疊

引入 多項式計算問題 例如 62 3 42 62 6 2 3 33 3 3 0 042 4 2 8 僅計算最近的兩個數 08 0 8 8 結束 需要某種方式 順序儲存,倒序輸出 堆疊 堆疊 具有操作約束性的線性表 入棧void push stack s,elementtype x else 出棧 e...

資料結構 堆疊

可以認為具有一定約束的線性表,其插入和刪除都作用於棧頂 top 的端點位置。且最 棧的資料最先彈出。壓入棧 push 插入資料 彈出棧 pop 取出 刪除 資料 型別名稱 堆疊 stack 資料物件集 乙個有0個或多個元素的有窮線性表 操作集 對於乙個具體的長度為正整數的maxsize的堆疊s st...