線性表之棧的鏈式儲存實現

2021-08-03 15:48:40 字數 1114 閱讀 4096

棧的鏈式儲存也稱為鏈棧,和鍊錶的儲存原理一樣,都可以通閒散空間來儲存元素,用指標來建立各節點之間的邏輯關係。同順序棧,鍊錶的插入,刪除操作也只能在棧頂進行。

鏈棧的c語言實現:(codeblocks完美執行)

#include #include typedef struct node

pnode;

typedef struct stack

linkstack;

//初始化鏈棧

linkstack* create()

return stack;

}//判斷棧是否為空,是返回1,不是返回0

int isempty(linkstack *stack)

//獲得鏈棧大小

int getsize(linkstack *stack)

//取得棧頂元素

pnode* gettop(linkstack *stack)

//入棧

int push(linkstack *stack,int val)

return 1;

}//出棧

pnode* pop(linkstack *stack)

pnode *node = stack->top; //node指向棧頂

stack->top = stack->top->next; //top指向下乙個元素

stack->size--; //鏈棧長度減少

return node;

}//銷毀棧

void destroy(linkstack *stack)

//如果鏈棧中資料不為空,就需要把棧中的節點都刪除再釋放

else

printf("鏈棧消除成功!\n");

}}int main()

}destroy(stack);//銷毀鏈

return 0;

}

執行介面:

線性表的鏈式儲存實現

include include 下面的單鏈表是帶頭節點的 typedef int elementtype struct listnode typedef struct listnode list 函式宣告 int insert list tmp,elementtype x,int pos void ...

線性表之鏈式儲存結構

下面 為單鏈表的一些基本操作 include using namespace std typedef int elemtype typedef struct lnode linklist 逆序建立鍊錶 linklist recreatelinklist linklist l cout l n cha...

線性表6 棧的鏈式儲存(鏈棧)

4.應用 數制轉換 從資料結構角度看,棧和佇列也是線性表,只不過是操作受限的線性表。棧 只允許在一端進行插入和刪除操作的線性表,允許操作的一端為棧頂,不允許操作的一段稱之為棧底。棧頂是表尾,棧底是表頭。後進先出 lifo 先進後出 filo first in last out 儲存 順序儲存 鏈式儲...