可以認為具有一定約束的線性表,其插入和刪除都作用於棧頂(top)的端點位置。且最**棧的資料最先彈出。
壓入棧(push):插入資料
彈出棧(pop):取出(刪除)資料
型別名稱:堆疊(stack)
資料物件集:乙個有0個或多個元素的有窮線性表
操作集:對於乙個具體的長度為正整數的maxsize的堆疊s∈stack,記堆疊中的任一元素x∈elementtype,有:
(1)stack creatstack(int maxsize):生成空堆疊,其最大長度為maxsize;
(2)bool isfull(stack s):判斷堆疊s是否已滿。若s中元素個數等於maxsize時返回true;否則返回false;
(3)bool push(stack s,elementtype x):將元素壓入堆疊。若堆疊已滿,返回false;否則將資料元素x插入到堆疊s棧頂並返回true;
(4)bool isempty(stack s):判斷堆疊s是否為空。若是返回true;否則返回false;
(5)elementtype pop(stack s):刪除並返回棧頂元素。若堆疊為空,返回錯誤資訊;否則將棧頂元素從堆疊中刪除並返回。
#include
#include
#define error -1
typedef
int position;
typedef
struct node * ptrtonode;
struct node
;typedef ptrtonode stack;
stack creatstack
(int maxsize)
;bool isfull
(stack s)
;bool push
(stack s,
int x)
;//bool push(stack s,elementtype x);
bool isempty
(stack s)
;int
pop(stack s)
;//elementtype pop(stack s);
void
p(stack s)
;//列印棧中的資料
intmain()
stack creatstack
(int maxsize)
bool isfull
(stack s)
bool push
(stack s,
int x)
else
}bool isempty
(stack s)
intpop
(stack s)
else
}void
p(stack s)
for(i=
0;i<=s->top;i++
)printf
("列印完成\n");
}
#include
#include
typedef
struct node * ptrtonode;
//將node *重新命名為 ptrtonode
struct node
;typedef ptrtonode stack;
stack creatstack()
;//構建堆疊的頭結點,返回該結點指標
bool isempty
(stack s)
; bool push
(stack s,
int x)
;//bool push(stack s,elementtype x);
intpop
(stack s)
;//elementtype pop(stack s);
void
p(stack s)
;int
main()
stack creatstack()
bool isempty
(stack s)
bool push
(stack s,
int x)
intpop
(stack s)
else
}void
p(stack s)
else
}printf
("列印完成\n");
}
資料結構 堆疊
對於棧,想必大家都十分熟悉了,也能很快的答出棧是乙個先進後出的佇列。但是在平常程式設計的生活中應用的十分少。在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...
資料結構 堆疊
1 stack.h 2 ifndef stack h 3 define stack h45 struct nodestack 6 typedef struct nodestack ptrtonode 7typedef ptrtonode stack 8 typedef char elementtyp...