以陣列作為儲存結構的棧

2021-06-26 12:23:04 字數 1930 閱讀 1351

參考《資料結構》

注:1.        純虛函式必須要定義,否則會出現error lnk2001: unresolvedexternal symbol "public: virtual bool __thiscallseqstack::gettop(int &)const

.h

const int size = 50;

template class stack

//元素x入棧

virtual void push(const t& x) = 0;

//棧頂元素出棧,由x返回

virtual bool pop(t& x) = 0;

//讀取棧頂元素,由x返回

virtual bool gettop(t& x) const = 0;

//判斷棧空

virtual bool isempty() const = 0;

//判斷棧滿

virtual bool isfull() const = 0;

//計算棧中元素個數

virtual int getsize() const = 0;

};

.cpp

#include "stack.h"

#include #include #include const int stackincreatement = 20;

template class seqstack:public stack

void push(const t& x);

//if(isboolfull()),則溢位處理,否則push(x)

bool pop(t& x);

//if(isempty()),則返回false, 否則pop(x)

bool gettop(t& x) const;

//if(isempty()),則返回false

bool isempty() const ;

//return getsize();

bool isfull() const ;

//return maxsize - getsize();

int getsize() const

void makeempty()

friend ostream& operator <<(ostream& out, seqstack& s);

private:

t* elements;

//top 表示最後儲存的元素的位置

int top;

int maxsize;

void overflowprocess();

};template seqstack::seqstack(int sz):maxsize(sz), top(-1) ;

template void seqstack::overflowprocess()

elements[++top] = x;

};template bool seqstack::pop(t& x)

x = elements[top--];

return true;

};template bool seqstack::gettop(t& x)

x = element[top];

return true;

};

main。cpp
#include #include #include "seqstack.h"

const int maxlength = 100;

bool printmatchedpairs(char* expression)

else if(expression[i - 1] == ')') {

//j用來存放pop出的i值

if(s.pop(j) == true) {

cout<<"("<

棧的鏈式儲存結構

棧的鏈式表示,即鏈棧。由於棧的操作是線性表操作的特例,因此鏈棧可以看成運算受限的單鏈表。其特點如下 鏈棧無棧滿問題,空間可擴充 插入和刪除僅在棧頂處執行 鏈式棧的棧頂在鏈頭 適合於多棧操作。stacklist.h include class listint int data listint link...

棧的順序儲存結構

標頭檔案 函式的宣告 include include include define stacksize 100 typedef int elemtype typedef struct seqstack void initstack seqstack s 初始化棧 int stackempty seq...

棧的鏈式儲存結構

棧的鏈式儲存結構稱為鏈棧,對於鏈棧來說,基本不存在棧滿的情況,除非記憶體已經沒用可以使用的空間,如果真的存在這種情況的話,那麼就說明此時的計算機作業系統已經快要宕機了,那麼我們就沒必要再去關注溢位的問題了 關於鏈棧的結構如下所示 關於鏈棧的程式定義如下所示 typedef struct stackn...