資料結構 棧的陣列實現

2021-08-27 16:17:40 字數 1406 閱讀 7691

棧是一種先入後出的資料結構,在計算機表示式求值時是一種常用的資料結構。具體提出:在解決需要判斷將來問題才來作出判斷時,常需要將當前的資料儲存,而棧就是一種這樣的資料結構,需要儲存當前資料時,先壓入堆疊,使用時再彈出。堆疊也可以認為是具有一定約束的線性表,其插入與刪除都作用在同乙個位置(棧頂)。

一、對於棧的定義:

1、棧儲存的資料型別;

2、棧頂;

3、棧的最大容量;

c語言實現:

struct snode;

二、建立空棧

建立乙個空棧,首先要給定棧的大小,然後對棧頂初始化。

stack create_stack(int maxsize)

三、壓棧

壓棧實際上就是在棧頂插入資料,棧頂同時加1。在壓棧之前還需要判斷是否棧滿。

bool push(stack s, elementtype x)

else    

}四、出棧

出棧實際上就是刪除棧頂資料,棧頂同時減1。在出棧之前還需要判斷棧空。

elementtype pop(stack s)

else    

}  總結:其實棧也是一種特殊的線性表,多用用就熟悉了。下面給出乙個例項:

#include #include #define false 0

#define true 1

typedef int elementtype;

typedef int position;

typedef int bool;

typedef struct snode *stack;

struct snode;

stack create_stack(int maxsize);

bool isempty(stack s);

bool isfull(stack s);

bool push(stack s, elementtype x);

elementtype pop(stack s);

int main() /*

** 建立空棧

*/ stack create_stack(int maxsize)

/*** 判斷空棧

*/bool isempty(stack s)

/* ** 判斷棧滿

*/bool isfull(stack s) /*

** 壓棧

*/bool push(stack s, elementtype x)

else

} /*

** 出棧

*/elementtype pop(stack s)

else

}

資料結構 棧 陣列的實現

首先是定義棧的基本結構,因為用陣列實現 private string stack private int top 0 然後是構造方法 stackofstrings int capacity 然後是push,注意,top永遠指向的是壓入元素的後一位。public void push string st...

資料結構之陣列實現棧結構

include include int top int s 返回棧頂位置 int stack empty int s 判斷棧是否為空 int stack full int s 判斷棧是否已滿 void push int s,int x int pop int s return x int main ...

資料結構 棧的陣列實現法

棧 stack 是一種線性儲存結構,它具有如下特點 棧中的資料元素遵守 先進後出 first in last out 的原則,簡稱filo結構。限定只能在棧頂進行插入和刪除操作。下面將使用c 實現棧的結構與入棧出棧等操作 參考 include include include using namesp...