《數(shù)據(jù)結構》實驗二 棧和隊列_第1頁
《數(shù)據(jù)結構》實驗二 棧和隊列_第2頁
《數(shù)據(jù)結構》實驗二 棧和隊列_第3頁
《數(shù)據(jù)結構》實驗二 棧和隊列_第4頁
《數(shù)據(jù)結構》實驗二 棧和隊列_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

付費下載

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論