快速 實現 棧

2021-10-04 04:01:40 字數 2556 閱讀 9892

1、棧的概念及結構

棧:一種特殊的線性表,其只允許在固定的一端進行插入和刪除元素操作。進行資料插入和刪除操作的一端稱為棧頂,另一端稱為棧底。棧中的資料元素遵守後進先出lifo(last in first out)的原則。

壓棧:棧的插入操作叫做進棧/壓棧/入棧,如資料在棧頂。

出棧:棧的刪除操作叫做出棧。出資料也在棧頂。

2、棧的實現

棧的實現一般可以使用陣列或者鍊錶實現,相對而言陣列的結構實現更優一些。因為陣列在尾上插入資料的代價比較小。

定義結構體

typedef

int stdatatype;

typedef

struct stack

stack;

入棧

void stackpush(stack ps, stdatatype x);*

1.判斷是否有空間可以入棧

2、有空間可以直接入棧

3、沒有空間,擴大棧的容量

void

stackpush

(stack* ps, stdatatype x)

ps->_a[ps->_top]

= x;

ps->_top++

;}

出棧

void stackpop(stack ps);*

將棧的有效資料個數減一,就完成了出棧。

void

stackpop

(stack* ps)

棧頂

stdatatype stacktop(stack ps);*

1、判斷棧是否為空

2、不為空輸出棧頂元素

3、為空assert報錯。

stdatatype stacktop

(stack* ps)

stack.c

#include

"stack.h"

void

stackinit

(stack *ps)

void

stackdestory

(stack* ps)

void

stackpush

(stack* ps, stdatatype x)

ps->_a[ps->_top]

= x;

ps->_top++;}

void

print

(stack* ps)

}void

stackpop

(stack* ps)

stdatatype stacktop

(stack* ps)

intstackempty

(stack* ps)

intstacksize

(stack* ps)

stack.h

#define _crt_secure_no_warnings 1

#include

#include

#include

#include

typedef

int stdatatype;

typedef

struct stack

stack;

//初始化

void

stackinit

(stack *ps)

;//銷毀

void

stackdestory

(stack* ps)

;//入棧

void

stackpush

(stack* ps, stdatatype x)

;//出棧

void

stackpop

(stack* ps)

;//棧頂

stdatatype stacktop

(stack* ps)

;//棧是否為空

intstackempty

(stack* ps)

;//棧的有效資料

intstacksize

(stack* ps)

;void

text

(stack* ps)

;void

print

(stack* ps)

;

textstack.c

#include

"stack.h"

intmain()

用棧實現快速排序

include include include include include include 快速排序 一次劃分過程 int partation int arr,int low,int high 返回值為low與high相等時的下標 if low high else while low 從前往後找...

用棧實現快速排序

include include include include include include 快速排序 一次劃分過程 int partation int arr,int low,int high 返回值為low與high相等時的下標 if low high else while lowtmp el...

快速排序演算法實現(遞迴實現 棧實現)

基本思想 選擇乙個基準元素,比如選擇最後乙個元素,通過一趟掃瞄,將待排序列分成兩部分,一部分比基準元素小,一部分大於等於基準元素,此時基準元素在其排好序後的正確位置,又稱為軸位置,此位置的元素確定後不再參與排序,然後再用同樣的方法遞迴地排序劃分的兩部分。分析 快速排序是不穩定的排序。快速排序的時間複...