/*
目的:學習資料結構,學校c語言
功能:棧的鏈式結構實現*/
#include
#include
#define ok 1
#define false 0
#define true 1
#define error 0
typedef int status;
typedef int selemtype;
//結點型別
typedef struct node
stacknode,
*pstacknode;
//鍊錶型別
typedef struct linkstack
linkstack,
*plinkstack;
status initlinkstack(linkstack *s)
;status emptylinkstack(linkstack *s)
;void clearlinkstack(linkstack *s)
;void destroystack(linkstack *s)
;int stacklength(linkstack *s)
;status gettop(linkstack *s, selemtype *e)
;status push(linkstack *s, selemtype e)
;status pop(linkstack *s, selemtype *e)
;void trasverlinkstack(linkstack *s)
;status initlinkstack(linkstack *s)
s->top = s-
>base;
s->base-
>
next
=null
;return ok;
}status emptylinkstack(linkstack *s)
void clearlinkstack(linkstack *s)
stacknode *p = s-
>top,
*q;while
(p !
= s-
>base)
s->top = s-
>base;
}void destroystack(linkstack *s)
s->top = s-
>base;
free(s-
>base);s-
>base =
null;s-
>top =
null;}
int stacklength(linkstack *s)
return i;
}status gettop(linkstack *s, selemtype *e)
*e = s-
>top-
>data;
return ok;
}status push(linkstack *s, selemtype e)
r->data = e;
r->
next
= s-
>top;
s->top = r;
return ok;
}status pop(linkstack *s, selemtype *e)
stacknode *q = s-
>top;
*e = q-
>data;
s->top = q-
>
next
;free(q)
;q =
null
;return ok;
}void trasverlinkstack(linkstack *s)
stacknode *p = s-
>top;
while
(p !
= s-
>base)
printf(
"\n");
}int main(void)
else
printf(
"--->棧中元素個數: %d\n"
, stacklength(
&s));/
/入棧push(
&s, 1)
;push(
&s, 2)
;push(
&s, 3)
;push(
&s, 4)
;push(
&s, 5)
;push(
&s, 6)
;printf(
"--->棧中元素個數: %d\n"
, stacklength(
&s))
;trasverlinkstack(
&s);
//清空棧
printf(
"--->清空棧\n");
clearlinkstack(
&s);
printf(
"--->棧中元素個數: %d\n"
, stacklength(
&s))
;push(
&s, 2)
;push(
&s, 4)
;push(
&s, 5)
;push(
&s, 6)
;push(
&s, 8)
;push(
&s, 9);/
/獲取棧頂元素
trasverlinkstack(
&s);
gettop(
&s,&e)
;printf(
"--->棧頂元素: %d\n"
, e);/
/出棧printf(
"--->棧中元素個數: %d\n"
, stacklength(
&s))
;while
(!emptylinkstack(
&s))
printf(
"--->棧中元素個數: %d\n"
, stacklength(
&s))
;trasverlinkstack(
&s);
destroystack(
&s);
return 0;}
資料結構棧(鏈式實現)
真正的棧操作是在棧頂,這裡給出的棧是帶了頭結點的棧,也就是說head next代表圖示棧頂,head next data是1 實現 include include typedef int datatype typedef struct snode lsnode 初始化帶頭結點的鏈式堆疊,初始化函式中...
資料結構 鏈式棧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 函式,因為其他函式有可能會修改該物件的成員,編譯器為了避免該類事情發生,會...