(閒來無事)棧的實現

2021-08-08 17:21:53 字數 1410 閱讀 3956

對棧的理解記住「先進後出」就可以了,什麼意思呢?就是向桶裡放積木只能拿出最上層的積木,然後一層一層的拿出這樣理解的話就簡單了,只用乙個節點一直指向棧的最上層的節點就可以了。

1.首先想構造的資料結構是什麼樣子的?需要儲存的資料有哪些?儲存的資料可以根據自己的需求去新增, 在我設計的資料中只有乙個那就是int 型別的data資料如下所示:

typedef struct  stacknode

stacknode;

typedef struct stackhead

stackhead;

2.函式的實現

/*

** @brief 建立乙個高度為 height 的棧

** ** @param 無

**** @return 返回指向新建的棧的指標

*/stackhead *initstack()

pstack->top = pstack->base = null;

pstack->height = 0;

return pstack;}/*

** @brief 把資料壓入棧內

** ** @param ls 壓入棧的資料value

**** @return 成功ture 失敗false

*/bool push(stackhead *ls,int value)

stacknode *pnode = (stacknode *)malloc(sizeof(stacknode));

if(null == pnode)

pnode->pnext = ls->top;

pnode->data = value;

ls->top = pnode;

ls->height++;

return true;}/*

** @brief 把資料壓入棧內

**** @param ls

**** @return 成功ture 失敗false

*/bool pop(stackhead *ls)

stacknode *pnode = ls->top->pnext;

free(ls->top);

ls->top = pnode;

ls->height--;

return true;}/*

** @brief 列印棧內資料

** ** @param ls

**** @return 成功ture 失敗false

*/bool printsatck(stackhead *ls)

while(null != ls->top)

return true;

}

測試函式功能

int main()

閒來無事,學學python

字串 單雙三引號,三引號負責多行字串 轉義 拼接 複製 start,end,step 切片 format 格式化,可通過name指定值 len 字串長度 strip lstrip rstrip 去掉空格 split seq,num 字串轉列表 replace s1,s2,num 字串替換 encod...

雜談 之 閒來無事 二

在分析推背圖之前,首先要了解一下基礎知識,這個後續分析中才會知道去 找,以及思路是什麼。12 3456 78910 ji y b ng d ng w j g ng x nr n gu 甲乙丙 丁戊己庚 辛壬癸顏色 青青赤赤 黃黃白白 黑黑五行木 木火火土 土金金水 水 其中甲 丙 戊 庚 壬為陽幹,...

小程式兩款 閒來無事練練手

最近看指標,感覺理解的一般般,想用指標寫點東西吧,寫出來又是錯的.囧!無聊了就隨便寫了兩個程式回顧一下以前的東西.還好,沒忘多少.1 輸入10個數存入陣列a中,並將其倒序存放及輸出 include include int a 10 void input void swap int a,int n v...