C 實現順序棧

2021-07-03 14:37:07 字數 1359 閱讀 5781

vs2013下實現

#include #include using namespace std;

const int stacksize = 20;

templateclass stack //建構函式,初始化乙個空棧

stack(t a, int n); //含參建構函式

stack(const stack&otherstack); //拷貝建構函式

stack& operator=(stack&otherstack); //賦值函式,目標棧必須是非靜態成員

~stack() //析構函式

void push(t x); //入棧

t pop(); //出棧

t gettop() //取棧頂元素

string isempty(); //是否為空棧

void print();

void clear();

private:

t data[stacksize]; //以陣列形式存放棧元素

int top; //棧頂指標,即陣列下標

};templatestack::stack(t a, int n)

templatestack::stack(const stack&otherstack) //拷貝建構函式定義 }}

templatestack& stack::operator=(stack&otherstack) //賦值函式定義

} return *this; //為了連續賦值,返回自身

}templatevoid stack::push(t x) //棧頂入棧操作,直接使用top

templatet stack::pop()

templatestring stack::isempty()

templatevoid stack::print() //遍歷輸出棧元素,此時不能用top進行遍歷,不然將top位置,影響後面的操作

templatevoid stack::clear() //清空棧

}int _tmain(int argc, _tchar* argv)

; stacka(a,6);

a.print();

a.push(7);

a.print();

coutb.print();

a.print();

a.push(9);

b = a;

b.print();

return 0;

}

C 順序棧實現

模板類檔案位置 宣告和定義均要在標頭檔案中進行,否則會出現鏈結錯誤,不可只在標頭檔案宣告,在cpp檔案中定義 環境vs2019 模板類中的友元函式宣告 在類內宣告友元函式時,需要在上面加上 模板頭 template,否則會報錯,具體原因 建構函式中使用return 不影響結果,貌似是這樣,在我這個例...

C 實現順序棧

版本一 int型別,只有建構函式和析構函式 class sstack sstack void push int x p size x int pop return p size inttop return p size 1 bool empty intsize private int p int s...

順序棧之C 實現

順序棧就是用順序表 陣列 實現的棧。其組織形式如下圖所示 下面介紹下我用c 實現的順序棧,在vc6下除錯通過。不足之處還請指正。1 檔案組織 2 ss.h棧類的宣告及巨集的定義 ifndef ss h define ss h typedef int datatype define maxsize 1...