鏈式棧 C語言資料結構

2021-08-10 17:39:21 字數 1338 閱讀 1005

棧的鏈式儲存結構

棧的鏈式儲存結構與線性表的鏈式儲存結構相同,是通過由結點構成的單鏈表實現的。為操作方便我們使用無頭結點的單鏈表。此時棧頂為單鏈表的第乙個結點,整個單鏈表為乙個鏈棧。

//鏈棧的型別定義

typedef struct node

linkstack;                 /*鏈棧結點型別*/

top 為棧頂,它唯一地確定乙個棧。空棧時為null。因為鏈棧地動態分配空間的,所以操作時無需考考慮上益問題。

下面是鏈棧的部分的基本操作:

1.判斷空棧

//判別空棧

int stackempty(linkstack *top)

返回0,則不為空。

2.取棧頂元素

//取棧頂元素

datatype gettop(linkstack *top)

return top->data;

}3.入棧

//入棧

linkstack *push(linkstack *top,datatype x)

4.出棧

//出棧

linkstack *pop(linkstack *top)

//判斷是否為空棧n

p=top;  //指向被刪除的棧頂

top=top->next; //修改棧頂指標

free(p);

return top;

}5.main函式測試

main()

,i,isemtpy;

linkstack *linkstack;

linkstack = (linkstack *)malloc(sizeof(linkstack));

linkstack->data=1;

linkstack->next=null;

for(i=1;i<5;i++)

//取棧頂元素為

printf("棧頂元素為:");

printf("%d/n",gettop(linkstack));

//把棧頂元素出棧

linkstack=pop(linkstack);

//出棧後的棧頂元素是

printf("出棧後的棧頂元素是:");

printf("%d",gettop(linkstack));

printf("/n");

//判斷是否為空棧

isemtpy = stackempty(linkstack);

if(isemtpy==0)

printf("linkstack為非空鏈棧!/n");

else

printf("linkstack為空鏈棧!/n");

}說明:

資料結構 鏈式棧c

棧的最基本特點先進後出,本文簡單介紹一下用c 寫的鏈式棧 標頭檔案 1 ifndef linkedstack h 2 define linkedstack h 34 template class linkedstack 56 template 7class chainnode8 建立新的結點,lin...

資料結構 鏈式棧

編譯錯誤 passing const linkstack as this argument discards qualifiers fpermissive 解決方法 c 中const 引用的是物件時只能訪問該物件的const 函式,因為其他函式有可能會修改該物件的成員,編譯器為了避免該類事情發生,會...

資料結構 棧 c鏈式實現

目的 學習資料結構,學校c語言 功能 棧的鏈式結構實現 include include define ok 1 define false 0 define true 1 define error 0 typedef int status typedef int selemtype 結點型別 type...