棧的簡單實現(順序棧 鏈棧)

2021-10-02 14:33:59 字數 3283 閱讀 6694

#include

#define maxsize 100

#define true 1

#define false 0

#define ok 1

#define error 0

#define infeasible -1

#define overflow -2

using

namespace std;

typedef

int status;

typedef

struct

sqstack;

status initstack

(sqstack &s)

status push

(sqstack &s,

int e)

status pop

(sqstack &s,

int&e)

status gettop

(sqstack s)

status stackempty

(sqstack s)

status stacklength

(sqstack s)

void

clearstack

(sqstack &s)

void

destroystack

(sqstack &s)

}int

main()

cout <

cout<<

"此時順序棧內元素個數為:"

<<

stacklength

(s)<

cout<<

"刪除棧頂元素:"

<

pop(s,e)

) cout<<

"刪除成功!"

<<

"此元素為:"

<

else

cout<<

"刪除失敗!"

<

!stackempty

(s))

cout<<

"棧非空!"

<

else

cout<<

"棧空!"

<

cout<<

"此時棧內元素個數為:"

<<

stacklength

(s)<

cout<<

"取棧頂元素:"

<<

gettop

(s)<

cout<<

"清空順序棧:"

<

clearstack

(s);

cout<<

"此時順序棧內元素個數為:"

<<

stacklength

(s)<

cout<<

"銷毀順序棧:"

<

destroystack

(s);

cout<<

"順序棧已銷毀"

"此時棧內元素個數為:"

<<

lenthstack

(s)<

cout<<

"刪除棧頂元素:"

<

pop(s,e)

) cout<<

"刪除成功!"

<

"此元素為:"

<

else

cout<<

"刪除失敗!"

<

!stackempty

(s))

cout <<

"棧非空!"

<

else

cout<<

"棧空!"

<

cout<<

"此時棧內元素個數為:"

<<

lenthstack

(s)<

cout<<

"取棧頂元素:"

<<

gettop

(s)<

cout<<

"清空鏈棧:"

<

clearstack

(s);

cout<<

"已清除!"

<

cout<<

"此時棧內元素個數為:"

; cout<<

lenthstack

(s)<

cout<<

"銷毀鏈棧:"

<

destroystack

(s);

cout<<

"已銷毀!"

棧 實現鏈棧和順序棧)

按不同的儲存結構,可以將棧分為順序棧和鏈棧。順序棧的實現 typedef int datatype const int maxnum 1000 struct sqstack 判斷棧空 bool isempty else return false 判斷棧滿 bool isfull else retur...

棧的實現 順序棧和鏈棧

本文主要給出我對棧的實現,包括順序棧和鏈棧兩種實現方式。common.h ifndef common h define common h 函式結果狀態碼 define true 1 define false 0 define ok 1 define error 0 define infeasible...

棧,順序棧,鏈棧

棧作為一種限定性線性表,是將表的插入刪除限制為僅在表的一端進行,通常將表中允許插入刪除的一端叫做棧頂 top 因此棧頂的當前位置是動態變化的。棧的另一端叫做棧底 bottom 當棧中沒有元素時稱為空棧。插入操作稱為進棧或入棧,刪除操作稱為出棧或退棧。棧是先進後出的線性表,簡稱為lifo表。棧主要有兩...