C語言實(shí)現(xiàn)鏈棧的步驟
更新時間:2021年05月20日 10:22:03 作者:憶想不到的暉
鏈棧是棧的鏈?zhǔn)酱鎯Y(jié)構(gòu),鏈棧可以用單鏈表的頭插法實(shí)現(xiàn),本文主要講述了如何用c語言去實(shí)現(xiàn)鏈棧,感興趣的朋友可以了解下
鏈棧圖解

鏈棧的常規(guī)操作
/********************* 鏈棧的常規(guī)操作 ****************************/ LinkStack InitLinkStack(); // 初始化鏈棧 int StackEmpty(); // 判斷鏈棧空 int StackLength(); // 求鏈棧長(鏈棧元素個數(shù)) int Push(); // 入棧 壓棧 ElemType Pop(); // 出棧 彈棧 void DestroyStack(); // 銷毀鏈棧 /***************************************************************/
定義鏈棧結(jié)構(gòu)體
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
typedef int ElemType; // 鏈棧存儲元素的數(shù)據(jù)類型
/*
* 定義鏈棧結(jié)構(gòu)體
*/
typedef struct Node{
ElemType data; // 棧結(jié)點(diǎn)數(shù)據(jù)域
struct Node *next; // 棧結(jié)點(diǎn)指針域
}*LinkStack, Node;
初始化鏈棧
// 初始化鏈棧(帶頭結(jié)點(diǎn)的鏈棧)
LinkStack InitLinkStack(){
LinkStack s = (LinkStack)malloc(sizeof(struct Node));
s -> next = NULL;
return s;
}
鏈棧判空
/*
* 判斷鏈棧是否空
* s 鏈棧
*/
int StackEmpty(LinkStack s){
if(s == NULL){
return FALSE;
}
return s -> next == NULL;
}
因?yàn)槭擎準(zhǔn)酱鎯Y(jié)構(gòu),無需鏈棧判滿。
計(jì)算鏈棧的長度
/*
* 求鏈棧長度(棧中元素個數(shù))
* s 鏈棧
*/
int StackLength(LinkStack s){
LinkStack p;
int len = 0;
if(StackEmpty(s)){
return FALSE;
}
p = s -> next; // 帶頭結(jié)點(diǎn)的鏈棧要先移動一下
while(p != NULL){
len ++;
p = p -> next;
}
return len;
}
鏈棧入棧(Push)
/*
* 入棧 壓棧
* s 鏈棧
* data 入棧數(shù)據(jù)
*/
int Push(LinkStack s, ElemType data){
// 分配入棧結(jié)點(diǎn)
Node *new_node = (Node *)malloc(sizeof(struct Node));
if (new_node == NULL) return FALSE; // 結(jié)點(diǎn)分配失敗
// 跟單鏈表一樣使用頭插法
new_node -> data = data;
new_node -> next = s -> next;
s -> next = new_node;
return TRUE;
}
鏈棧出棧(Pop)
/*
* 出棧 彈棧
* s 鏈棧
*/
ElemType Pop(LinkStack s){
LinkStack top;
ElemType data;
// 判???
if(StackEmpty(s)){
return FALSE;
}
top = s -> next; // 訪問棧頂結(jié)點(diǎn)
data = top -> data; // 取出棧頂元素
s -> next = top -> next;
free(top); // 釋放棧頂空間
return data;
}
鏈棧各操作測試
// 程序主入口
int main(int argc, char const *argv[])
{
LinkStack s = InitLinkStack();
printf("StackEmpty():%d\n", StackEmpty(s));
printf("StackLength():%d\n\n", StackLength(s));
// 入棧元素
ElemType datas[] = {1, 3, 5, 7, 9};
// 動態(tài)計(jì)算入棧元素個數(shù)
int len = sizeof(datas) / sizeof(datas[0]);
// for循環(huán)依次入棧
printf("Push():");
for(int i = 0; i < len; i++){
printf("%d\t", datas[i]);
Push(s, datas[i]);
}
printf("\nStackEmpty():%d\n", StackEmpty(s));
printf("StackLength():%d\n\n", StackLength(s));
// 出棧 彈棧
printf("Pop(): ");
while(!StackEmpty(s)){
printf("%d\t", Pop(s));
}
printf("\nStackEmpty():%d\n", StackEmpty(s));
printf("StackLength():%d\n\n", StackLength(s));
return 0;
}
結(jié)果如下:
StackEmpty():1 StackLength():0 Push():1 3 5 7 9 StackEmpty():0 StackLength():5 Pop(): 9 7 5 3 1 StackEmpty():1 StackLength():0
源代碼
源代碼已上傳到 GitHub Data-Structure-of-C,歡迎大家來訪。
以上就是C語言實(shí)現(xiàn)鏈棧的步驟的詳細(xì)內(nèi)容,更多關(guān)于C語言實(shí)現(xiàn)鏈棧的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在C++中關(guān)于友元函數(shù)的進(jìn)一步理解
今天小編就為大家分享一篇關(guān)于在C++中關(guān)于友元函數(shù)的進(jìn)一步理解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
C++編程中刪除運(yùn)算符與相等運(yùn)算符的使用解析
這篇文章主要介紹了C++編程中刪除運(yùn)算符與相等運(yùn)算符的使用解析,delete和==以及!=運(yùn)算符的使用是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01
c++只保留float型的小數(shù)點(diǎn)后兩位問題
這篇文章主要介紹了c++只保留float型的小數(shù)點(diǎn)后兩位問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

