棧結構 順序棧 鏈棧

2021-10-08 21:21:28 字數 4328 閱讀 5332

定義:棧是一種與線性表相似的線性結構。

不同之處是當需要對節點做增刪操作時,只能操作棧頂的節點,因此具有先進後出(或者後進先出)的特點。

按儲存結構的不同,可分為順序棧和鏈棧。

棧結構的幾個屬性:

adt stack

// elemtype為型別識別符號

資料關係:

r =

資料操作:

(1) stackseq * initstack(); // 初始化棧

(2) void destroystack(stackseq *s); // 銷毀棧

(3) bool stackempty(stackseq *s); // 棧是否為空

(4) bool stackfull(stackseq *s); // 棧是否為滿

(5) int stacklength(stackseq *s); // 返回棧中元素個數——棧長度

(6) bool push(stackseq *s,elemtype e); // 入棧

(7) void pop(stackseq *s,elemtype *e); // 出棧

(8) bool gettop(stackseq *s,elemtype *e); // 取棧頂資料元素

(9) void dispstack(stackseq *s); // 輸出棧

}

對比線性表的抽象資料型別可以發現,棧的抽象資料型別少了返回指定序號的元素返回指定元素的序號兩個方法,並且插入節點和刪除節點方法也有位置上的限制,這些在計算上的限制正是棧結構的特點,因此棧也被稱為受限的線性表。

順序棧的儲存結構是利用一組位址連續的儲存空間依次存放自棧底到棧頂的資料元素,同時設定棧頂元素的下標top指向棧頂元素的位置。

定義順序棧的儲存結構:

#define maxsize 50

typedef char elemtype;

typedef struct node

stackseq;

順序棧**實現:

#include #include #define maxsize 50

#define true 1

#define false 0

typedef int bool;

typedef char elemtype;

typedef struct node

stackseq;

//初始化棧

stackseq * initstack();

//銷毀棧

void destroystack(stackseq *s);

//棧是否為空

bool stackempty(stackseq *s);

//棧是否為滿

bool stackfull(stackseq *s);

//返回棧中元素個數——棧長度

int stacklength(stackseq *s);

//入棧

bool push(stackseq *s,elemtype e);

//出棧

void pop(stackseq *s,elemtype *e);

//取棧頂資料元素

bool gettop(stackseq *s,elemtype *e);

//輸出棧

void dispstack(stackseq *s);

int main()

printf("\n");

printf("(8)棧為%s\n",(stackempty(s) == 1?"空":"非空"));

printf("(9)釋放棧\n");

destroystack(s);

return 0;

}stackseq * initstack()

bool stackempty(stackseq *s)

bool stackfull(stackseq *s)

bool push(stackseq *s,elemtype e)

int stacklength(stackseq *s)

void dispstack(stackseq *s)

printf("\n");

}void pop(stackseq *s,elemtype *e)

void destroystack(stackseq *s)

鏈棧的實現與頭插法的鍊錶很相似。

鏈棧的儲存結構定義為:

typedef int elemtype;

typedef struct node

stacklink;

鏈棧的儲存結構與鍊錶一樣,區別在於鍊錶的指標始終指向第乙個節點,而鏈棧的指標始終指向棧頂節點。

鏈棧的儲存結構也可以定義為:

typedef int elemtype;

typedef struct node

stacknode;

typedef struct

stacklink;

但是結構體變數stacklink只包含乙個成員,所以直接把成員定義成變數。

因為struct node *nextstacknode *top一樣作用,所以直接使用結構體變數stacknode初始化鏈棧。

最後鏈棧的儲存結構定義為:

typedef int elemtype;

typedef struct node

stacklink;

鏈棧的抽象運算與順序棧一致,但是由於儲存結構不一樣,所以具體實現也完全不一樣。

鏈棧**實現:

#include #include #define true 1

#define false 0

typedef char elemtype;

typedef int bool;

typedef struct node

stacklink;

// 初始化棧

stacklink * initstack();

// 銷毀棧

void destroystack(stacklink *s);

// 棧是否為空

bool stackempty(stacklink *s);

// 鏈棧沒有棧滿的概念

bool stackfull(stacklink *s);

// 返回棧中元素個數——棧長度

int stacklength(stacklink *s);

// 入棧

bool push(stacklink *s,elemtype e);

// 出棧

void pop(stacklink *s,elemtype *e);

// 取棧頂資料元素

bool gettop(stacklink *s,elemtype *e);

// 輸出棧

void dispstack(stacklink *s);

int main()

printf("\n");

printf("(8)鏈棧為%s\n",(stackempty(s) == 1?"空":"非空"));

printf("(9)釋放鏈棧\n");

destroystack(s);

return 0;

}//初始化棧

stacklink * initstack()

bool stackempty(stacklink *s)

// 入棧

bool push(stacklink *s,elemtype e)

void dispstack(stacklink *s)

printf("\n");

}int stacklength(stacklink *s)

return n;

}// 出棧

void pop(stacklink *s,elemtype *e)

// 銷毀棧

void destroystack(stacklink *s)

free(s);

}}

棧,順序棧,鏈棧

棧作為一種限定性線性表,是將表的插入刪除限制為僅在表的一端進行,通常將表中允許插入刪除的一端叫做棧頂 top 因此棧頂的當前位置是動態變化的。棧的另一端叫做棧底 bottom 當棧中沒有元素時稱為空棧。插入操作稱為進棧或入棧,刪除操作稱為出棧或退棧。棧是先進後出的線性表,簡稱為lifo表。棧主要有兩...

棧 順序棧 鏈棧

棧 順序棧 鏈棧 分別用順序表和煉表實現棧,完成入棧 出棧 窺探棧頂元素等操作 commom.h ifndef common h define commom h include include include include include define elemtype int void swap...

順序棧,鏈棧

二,鏈棧 public inte ce istack public class seqstack implements istack 將棧置空 public void clear 判斷棧是否為空 public boolean isempty 返回棧中元素的個數 public intlength 返回...