資料結構 實現棧的系列操作

2021-10-14 06:48:30 字數 1898 閱讀 2893

1.棧的概念

棧:一種特殊的線性表,其只允許在固定的一端進行插入和刪除元素操作。進行資料插入和刪除操作的一端稱為棧頂,另一端稱為棧底,棧中的資料元素遵守後進先出原則。

壓棧:棧的插入操作叫做進棧/壓棧/入棧,入資料在棧頂。

出棧:棧的刪除操作叫做出棧/彈棧,出資料也在棧頂。

2.示意圖

棧中資料

出棧

入棧

//初始化

void

stackinit

(stack* pst)

//銷毀

void

stackdestroy

(stack* pst)

//入棧

void

stackpush

(stack* pst, stdatatype x)

else

} pst->_a[pst->_top]

= x;

pst->_top++;}

//彈棧

void

stackpop

(stack* pst)

//獲取資料個數

intstacksize

(stack* pst)

//返回1是空,返回0是非空

intstackempty

(stack* pst)

//獲取棧頂的資料

stdatatype stacktop

(stack* pst)

stack.h

typedef

int stdatatype;

//靜態

//struct stack

//;//動態

typedef

struct stack

stack;

//初始化

void

stackinit

(stack* st)

;//銷毀

void

stackdestroy

(stack* pst)

;//入棧

void

stackpush

(stack* pst, stdatatype x)

;//彈棧

void

stackpop

(stack* pst)

;//獲取資料個數

intstacksize

(stack* pst)

;//返回1是空,返回0是非空

intstackempty

(stack* pst)

;//獲取棧頂的資料

stdatatype stacktop

(stack* pst)

;

資料結構 棧的操作

include include define maxsize 100 設順序表的最大長度為100,可依具體情況分配空間 define null 1 typedef int datatype typedef struct datatype stack maxsize int top 棧頂指標 seqs...

資料結構 實現棧

include include include define node len sizeof node 1 pstack ptop pstack pbottom都指向節點 typedef struct node pnode,node typedef struct stack pstack,stack...

資料結構 棧實現

棧和佇列不一樣,棧是後進先出。實現時用了陣列儲存棧,陣列大小根據內容自動擴充。廢話不多說,上 c mystack.h pragma once templateclass mystack templateint mystack getcount templatet mystack top templa...