資料結構 棧 鏈棧

2021-08-31 03:54:50 字數 2020 閱讀 9264

棧的插入和刪除只在棧頂進行操作,在單鏈表中,頭指標是單鏈表的必須元素;而在棧中,棧頂指標也是鏈棧的必須元素,且一般將棧頂放在單鏈表的頭部。 

線性表有順序儲存結構和鏈式儲存結構,棧屬於線性表的一種,也具有順序儲存結構和鏈式儲存結構。對於棧的鏈式儲存結構,一般稱之為鏈棧。

棧的特點:先進後出

棧函式實現:

1.初始化

2.判滿

3.壓棧

4.判空

5.出棧

6.帶元素出棧

7.清棧

#include#include#define maxsize 10

typedef int elem_type;

typedef struct stack

stack;

//pst沒有判斷null情況

void init(stack* pst)

bool isfull(stack* pst)

bool push(stack* pst,elem_type val)

return rt;

}bool isempty(stack* pst)

bool pop(stack* pst)

return rt;

}//帶出元素

//bool pop(stack* pst, elem_type* prt)

//// *prt = pst->data[pst->top - 1];

// pst->top--;

// return true;

//}elem_type top(stack* pst)

return pst->data[pst->top - 1];

}void clear(stack* pst)

void destroyed(stack* pst)

鏈棧函式實現:

1.初始化

2.壓棧

3.判空

4.出棧

5.帶元素出棧

6.清棧

#include#includetypedef int elem_type;

typedef struct node

node;

typedef struct stack

stack;

void init(stack* pst)

node* buynode(elem_type val)

return pnewnode;

}bool push(stack* pst, elem_type val)

pnewnode->pnext = (pst->head).pnext;

(pst->head).pnext = pnewnode;

return true;

}bool isempty(stack* pst)

//不帶出元素

bool pop(stack* pst)

node* pcur = (pst->head).pnext;

(pst->head).pnext = pcur->pnext;

free(pcur);

return true;

}//帶出元素

bool pop(stack* pst, elem_type* prt)

node* pcur = (pst->head).pnext;

(pst->head).pnext = pcur->pnext;

*prt = pcur->mdata;

free(pcur);

return true;

}elem_type top(stack* pst)

return ((pst->head).pnext)->mdata;

}void clear(stack* pst)

(pst->head).pnext = null;

}void destroyed(stack* pst)

資料結構 棧之鏈棧

鏈棧 單鏈表的頭插和頭刪時間複雜度o 1 所以鏈式棧的棧頂在頭這邊 1 基於鍊錶 帶頭結點的單鏈表 實現棧先進後出的特徵 2 對於棧,必須同乙個方向入棧和出棧 3 對於鍊錶,有頭插 頭刪,尾插和尾刪 時間複雜度 o 1 不需要迴圈遍歷,比如判斷乙個數是否為10,if a 10 o n 需要迴圈遍歷,...

資料結構 鏈棧

編寫乙個程式,實現鏈棧 假設棧中元素型別為char 的各種基本運算。並完成下面功能 1 初始化鏈棧s 2 判斷鏈棧s是否非空 3 依次進鏈棧元素a,b,c,d,e 4 判斷鏈棧s是否非空 5 輸出鏈棧長度 6 輸出從棧頂到棧底元素 7 輸出出鏈棧序列 8 判斷鏈棧s是否非空 9 釋放鏈棧。inclu...

資料結構 鏈棧

鏈式儲存的棧稱為鏈棧,結構如下圖 鏈棧無滿棧問題,空間可擴充,但有棧空問題,棧空的條件為top next null。進棧與入棧僅在棧頂進行,鏈式棧的棧頂在棧表頭。鏈棧的定義如下 struct stacknode class stack 建立頭結點 stack void push int item 入...