線性表應用一 棧(線性表實訓)

2021-10-05 14:01:43 字數 4949 閱讀 9907

用前面已經實現的線性表來實現乙個整數棧(棧裡的資料是整數)。共需要補全三個函式(也是棧的基本功能):判斷棧空的 empty 函式、壓棧的 push 函式和彈棧的 pop 函式。

相關知識

// 定義結點結構

struct node

;typedef node * intstack;

// 定義型別別名,intstack即相當於node*

其中:定義intstack是為了使程式的可讀性更好一些。因為本關是用線性表實現棧,而訪問乙個線性表其實只需要乙個煉表頭指標就可以了,intstack實際上就是node型別,所以後面可以這樣宣告棧sk:

intstack sk = null; // 宣告棧sk,並初始化為空棧(空線性表)

實際上sk就是乙個node型別的指標,用它可以訪問乙個線性表,也就可以看成乙個棧了。

三個需要使用者補全的函式的函式原型分別為:

判斷棧是否為空:函式empty判斷棧sk是否為空,為空則返回true,否則返回false;

bool empty(intstack sk);

彈棧:函式pop實現從棧sk中彈出棧頂元素,引數sk,傳引用,因為彈棧可能會改變sk的值,返回值為彈棧彈出的資料,如果棧空,則返回-1;

int pop(intstack &sk);

#include

#include

"mstack.h"

//#include "linearlist.h"

using

namespace std;

//定義結點結構

struct node

;//函式listlength:計算並返回鍊錶的長度

//引數:h-煉表頭指標

//返回值:鍊錶長度

intlistlength

(node * h)

;//函式delhas:刪除鍊錶中data為n的結點,如果有多個這樣的結點,只刪除第乙個

//引數:h-煉表頭指標,n-結點包含的資料

node *

delhas

(node * h,

int n)

;//函式delat:刪除鍊錶中序號為i的結點,如果i是非法序號則不做操作

//引數:h-煉表頭指標,i-要刪除結點的序號

node *

delat

(node * h,

int i)

;//函式search:在鍊錶中查詢包含資料num的結點

//引數:h-煉表頭指標,num-要查詢的資料

node *

search

(node * h,

int num)

;//函式insertsort:鍊錶排序插入

//引數:h-煉表頭指標,t-指向要插入的結點

node *

insertsort

(node *h, node *t)

;//函式inserthead:鍊錶頭部插入

//引數:h-煉表頭指標,t-指向要插入的結點

node *

inserthead

(node *h, node *t)

;//函式printlist:輸出鍊錶,每個資料之間用乙個空格隔開

//引數:h-煉表頭指標

void

printlist

(node *h)

;//函式inserttail:鍊錶尾部插入

//引數:h-煉表頭指標,t-指向要插入的結點

node *

inserttail

(node *h, node *t)

;//函式dellist:刪除鍊錶,釋放空間

//引數:h-煉表頭指標

void

dellist

(node *h)

;/函式dellist:刪除鍊錶,釋放空間

//引數:h-煉表頭指標

void

dellist

(node *h)

}//函式printlist:輸出鍊錶,每個資料之間用乙個空格隔開

//引數:h-煉表頭指標

void

printlist

(node *h)

cout

}//函式inserttail:鍊錶尾部插入

//引數:h-煉表頭指標,t-指向要插入的結點

node *

inserttail

(node *h, node *t)

//非空鍊錶的情況

node *p=h;

//讓p指向最後乙個結點

while

(p->next)

p=p-

>next;

p->next = t;

//讓最後乙個結點的指標域指向結點t

t->next=

null

;//鍊錶尾指標置為null

return h;

//返回第乙個結點的位址(即煉表頭指標)

}//函式inserthead:鍊錶頭部插入

//引數:h-煉表頭指標,t-指向要插入的結點

node *

inserthead

(node *h, node *t)

//函式insertsort:鍊錶排序插入

//引數:h-煉表頭指標,t-指向要插入的結點

node *

insertsort

(node *h, node *t)

if(p==

null

)//插入鏈首

if(q==

null

)//插入鏈尾

//插入p、q之間

t->next=q;

p->next=t;

return h;

}//函式search:在鍊錶中查詢包含資料num的結點

//引數:h-煉表頭指標,num-要查詢的資料

node *

search

(node * h,

int num)

return

null

;//沒找到包含num的結點

}//函式delat:刪除鍊錶中序號為i的結點,如果i是非法序號則不做操作

//引數:h-煉表頭指標,i-要刪除結點的序號

node *

delat

(node * h,

int i)

if(p)

//p指向的結點存在,不是刪除首結點

else

//刪除首結點

}//函式delhas:刪除鍊錶中data為n的結點,如果有多個這樣的結點,只刪除第乙個

//引數:h-煉表頭指標,n-結點包含的資料

node *

delhas

(node * h,

int n)

//刪除q指向的結點

if(p==

null

)//刪除頭結點

//不是頭結點

p->next=q-

>next;

//把q指向結點的指標域(q後面結點的位址)賦值給p指向結點的指標域

return h;

}//函式listlength:計算並返回鍊錶的長度

//引數:h-煉表頭指標

//返回值:鍊錶長度

intlistlength

(node * h)

return n;

}typedef node * intstack;

// 定義型別別名,intstack即相當於node*

// 函式empty:判斷棧sk是否為空

// 引數:sk-棧

// 返回值:true-sk為空,false-sk不為空

bool

empty

(intstack sk)

;// 函式pop:從sk中彈棧

// 引數:sk-棧,傳引用,彈棧可能會改變sk的值,n-要壓棧的整數

// 返回值:彈棧的彈出的整數,如果棧空,返回-1

intpop

(intstack &sk)

;// 函式push:壓棧,將整數n壓入棧sk中

// 引數:sk-棧,傳引用,壓棧會改變sk的值,n-要壓棧的整數

// 返回值:無,採用鍊錶實現棧,只要還有記憶體,壓棧都會成功

void

push

(intstack &sk,

int n)

;int

main()

else

cout<<

"stack empty"

;case1:

cin>>n;

//輸入要壓棧的資料

push

(sk,n)

; cout<<

"push "

;default:;

} cin>>command;

//輸入下一條指令

}//刪除棧

dellist

(sk)

;return0;

}// 函式empty:判斷棧sk是否為空

// 引數:sk-棧

// 返回值:true-sk為空,false-sk不為空

bool

empty

(intstack sk)

// 函式pop:彈棧

// 引數:sk-棧,傳引用,彈棧可能會改變sk的值

// 返回值:彈棧的彈出的整數,如果棧空,返回-1

intpop

(intstack &sk)

// 函式push:壓棧,將整數n壓入棧sk中

// 引數:sk-棧,傳引用,壓棧會改變sk的值,n-要壓棧的整數

// 返回值:無,採用鍊錶實現棧,只要還有記憶體,壓棧都會成功

void

push

(intstack &sk,

int n)

查詢元素(線性表實訓)

相關知識 由於鍊錶結點都是動態記憶體分配得到的,在記憶體中不是連續儲存,沒法使用二分法之類的演算法來實現資訊檢索,但可以使用順序查詢的方法。順序查詢需要遍歷整個鍊錶,逐個檢查每個結點是否滿足條件。程式中的printlist函式已實現了遍歷功能,可以參照其來實現查詢函式。其中printlist函式的實...

線性表 棧 佇列

輔助定義 define maxsize 5 define ok 0 define error 1 typedef int selemtype typedef int status 棧 順序 鏈式 相關結構體定義 順序棧 typedef struct sqstack 順序棧共享空間 typedef s...

特殊線性表 棧

定義 限定僅在表尾進行插入和刪除操作的線性表。操作特性 後進先出。注意 棧只是對錶插入和刪除操作的位置進行了限制,並沒有限定插入和刪除操作進行的時間。const int max size 100 template class t class seqstack template class t voi...