版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
《數(shù)據(jù)結構》實驗指導及報告書
2022/2022學年第_二學期
姓名:
學號:
班級:
指導教師:
計算機科學與工程學院
2022
實驗二棧和隊列
一、實驗目的
1、掌握棧的結構特性及其入棧,出棧操作;
2、掌握隊列的結構特性及其入隊、出隊的操作,掌握循環(huán)隊列的特點及其操作。
二、實驗內(nèi)容和要求
1、閱讀下面程序,將函數(shù)Push和函數(shù)Pop補充完整。要求輸入元素序列12345e,運
行結果如下所示。
#include<stdio.h>
#include<malloc.h>
#defineERROR0
#defineOK1
#defineSTACK.INT_SIZE10/*存儲空間初始分配量*/
#defineSTACKINCREMENT5/*存儲空間分配增量*/
typedefintElemType;/*定義兀素的類型*/
typedefstruct(
ElemType*base;
ElemType*top;
intstacksize;/*當前已分配的存儲空間*/
}SqStack;
intInitStack(SqStack*S);/*構造空棧*/
intpush(SqStack*S,ElemType*e);/*入蔻*/
intPop(SqStack*S,ElemType*e);/*出棧*/
intCreateStack(SqStack*S);/*創(chuàng)建棧*/
voidPrintStack(SqStack*S);/*出棧并軸出棧中兀素*/
intInitStack(SqStack*S){
S->base=(ElemType*)malloc(STACK.INT_SIZE*sizeof(ElemType));
if(!S->base)returnERROR;
S->top=S->base;
S->stacksize=STACK_INT_SIZE;
returnOK;
}/*InitStack*/
intPush(SqStack*S,ElemTypee){
}/*Push*/
intPop(SqStack*S,ElemType*e){
}/*Pop*/
intCreateStack(SqStack*S){
inte;
if(InitStack(S))
else{
returnERROR;
)
Push(S,e);
returnOK;
}/*CreateStack*/
voidPrintStack(SqStack*S){
ElemTypee;
while(Pop(S,&e))
}/*Pop_and_Print*/
intmain(){
SqStackss;
CreateStack(&ss);
PrintStack(&ss);
return0;
)
.算法分析:輸入元素序列12345,為什么輸出序列為54321?體現(xiàn)了棧的什么
特性?
#include<stdio.h>
#include<malloc.h>
#defineERROR0
ttdefineOK1
^defineSTACKJNT_SIZE10/*存儲空間初始分配量*/
^defineSTACKINCREMENT5/*存儲空間分配增量*/
typedefintElemType;/*定義元素的類型*/
typedefstruct(
ElemType*base;
ElemType*top;
intstacksize;/*當前已分配的存儲空間*/
}SqStack;
intInitStack(SqStack*S);/*構造空棧*/
intPush(SqStack*S,ElemType*e);/*入棧*/
intPop(SqStack*S,ElemType*e);/*出棧*/
intCreateStack(SqStack*S);/*創(chuàng)建棧*/
voidPrintStack(SqStack*S);/*出棧并輸出棧中元素*/
int1nitStack(SqStack*S){
S->base=(ElemType*)malloc(STACK_INT_SIZE*sizeof(ElemType));
if(!S->base)returnERROR;
S->top=S->base;
S->stacksize=STACK_INT_SIZE;
returnOK;
}/*InitStack*/
intPush(SqStack*S,ElemTypee){
if(S->top-S->base>=S->stacksize)
S->base=(ElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(Ele
mType));
if(!S->base)returnERROR;
S->top=S->base+S->stacksize;
S->stacksize+二STACKINCREMENT;
}
*S->top++=e;
returnOK;
}/*Push*/
intPop(SqStack*S,ElemType*e){
if(S->top=S->base)returnERROR;
e=-S->top;
returnOK;
}/*Pop*/
intCreateStack(SqStack*S){
inte;
if(InitStack(S))
else{
returnERROR;
Push(S,e);
returnOK;
}/*CreateStack*/
voidPrintStack(SqStack*S){
ElemTypee;
while(Pop(S,&e))
}/*Pop_and_Print*/
intmain(){
SqStackss;
CreateStack(&ss);
Pop(&ss,&e);
PrintStack(&ss);
return0;
)
2、在第1題的程序中,編寫一個十進制轉換為二進制的數(shù)制轉換算法函數(shù)(要求利用棧來
實現(xiàn)),并驗證其正確性。
實現(xiàn)代碼
■
#include<stdio.h>
#include<malloc.h>
^defineERROR0
^defineOK1
^defineSTACKINITSIZE10/*存W?儲加0空?間?初?始°?分Q?配?量C?*/
^defineSTACKINCREMENT5/*存&?儲&i6空?間?分Q?配?增?量C?*/
typedefintSElcmType;/*定i§義°?元打素?的t?類Cf-CS型"a*/
typedefstruct
(
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
intInitStack(SqStack&S);
intCreateStack(SqStack*S);
intGetTop(SqStackS,SElemType&e);
intPush(SqStack&S,SElemTypee);
intPop(SqStack&S,SElemType&e);
voidconversion();
voidmain()
(
inte;
SqStackS;
CreateStack(S);
GetTop(S,e);
Push(S,e);
conversion();
)
intInitStack(SqStack&S)
(
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)returnERROR;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
)
intCreateStack(SqStack&S)
(
inte;
if(InitStack(S))
printf();
else{
printf();
returnERROR;
)
printf();
while(scanf(,&e))
Push(S,e);
returnOK;
)
intGetTop(SqStackS,SElemType&e)
(
if(S.top==S.base)returnERROR;
e=*(S.top-1);
returnOK;
)
intPush(SqStack&S,SElemTypee)
(
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)returnERROR;
S.top=S.base+S.stacksize;
S.stacksize+二STACKINCREMENT;
)
)
intPop(SqStack&S,SElemType&e)
(
if(S.top==S.base)returnERROR;
e=*-S.top;
returnOK;
)
voidconversion()
(
intn,e;
SqStackS;
printf();
scanf(,n);
while(n)
{
Push(S,n);
n=n/2;
)
Pop(S,e);
printf(,e);
3、閱讀并運行程序,并分析程序功能。
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#defineM20
#defineelemtypechar
typedefstruct
{
elemtypestack[M];
inttop;
}
stacknode;
voidinit(stacknode*st);
voidpush(stacknode*st,elemtypex);
voidpop(stacknode*st);
voidinit(stacknode*st)
{
st->top=0;
)
voidpush(stacknode*st,elemtypex)
{
if(st->top==M)
else
(
st->top=st->top+1;
st->stack[st->top]=x;
}
}
voidpop(stacknode*st)
{
st->top=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 供應鏈物流管理操作流程手冊
- 社保局撫恤補助申請書
- 宿管會競聘申請書范文
- 石家莊保障房換房申請書
- 2026年塑料材料的力學性能實驗研究
- 2025年水務行業(yè)水質(zhì)監(jiān)測操作手冊
- 個人預支款申請書
- 施工進度復審申請書范本
- 游樂設備項目更換申請書
- 生物骨干教師面試題目及答案
- 廣西南寧市本年度(2025)小學一年級數(shù)學統(tǒng)編版專題練習(上學期)試卷及答案
- 公安特警測試題及答案
- ERCP治療膽總管結石的護理
- 2025年國際政治格局:多極化與地緣政治風險
- 有害物質(zhì)管控標準
- T-CSUS 69-2024 智慧水務技術標準
- 國家開放大學法學本科《商法》歷年期末考試試題及答案題庫
- UL583標準中文版-2018電動工業(yè)車輛UL中文版標準
- 2024年新華東師大版七年級上冊數(shù)學全冊教案(新版教材)
- 冀人版五年級科學上冊期末測試卷4份(含答案)
- 菜肴造型與盛裝工藝
評論
0/150
提交評論