北理工數(shù)據(jù)結(jié)構(gòu)作業(yè)2_第1頁(yè)
北理工數(shù)據(jù)結(jié)構(gòu)作業(yè)2_第2頁(yè)
北理工數(shù)據(jù)結(jié)構(gòu)作業(yè)2_第3頁(yè)
北理工數(shù)據(jù)結(jié)構(gòu)作業(yè)2_第4頁(yè)
北理工數(shù)據(jù)結(jié)構(gòu)作業(yè)2_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第三章作業(yè)1、 寫出下列程序段的輸出結(jié)果。viod main ( ) Stack S;char x, y;InitStack (S);x=c; y=k;Push(S, x); Push(S, a); Push(S, y);Pop(S, x); Push(S, t); Push(S, x);Pop(S, x); Push(S, s);while ( ! StackEmpty(S) ) Pop(S, y); printf (y);printf(x); 答:stack2、 簡(jiǎn)述下列算法的功能(棧的元素類型SElemType為int)。(1)Ststus algo1(Stack S) int I, n

2、, A255; n=0; while ( ! StackEmpty(S) ) n+; Pop(S, An); for ( i=1; i<=n; i+ ) Push(S, An); 答:實(shí)現(xiàn)棧中元素的逆置(2)Status algo2(Stack S, int e) Stack T; int d; InitStack (T); while ( ! StackEmpty(S) ) Pop(S, d); if ( d!=e ) Push(T, d); while ( ! StackEmpty(T) ) Pop (T, d); Push (S, d); 答:通過(guò)棧T的輔助刪除棧S中所有值為e的元

3、素3、 設(shè)有4個(gè)元素1、2、3、4依次進(jìn)棧,而出棧操作可隨時(shí)進(jìn)行(進(jìn)出??扇我饨诲e(cuò)進(jìn)行,但要保證進(jìn)棧次序不破壞1、2、3、4的相對(duì)次序),請(qǐng)寫出所有可能的出棧次序。答:共14種情況,順序如下:1234,1243,1324,1342,1432,2134,2143,2314,2341,2431,3421,3241,3214,4321。4、 寫出下列程序段的輸出結(jié)果(隊(duì)列中的元素類型QelemType為char)。viod main ( ) Queue Q; InitQueue(Q);char x=e, y=c;EnQueue(Q, h); EnQueue(Q, r); EnQueue(Q, y);

4、DeQueue(Q, x); EnQueue(Q, x);DeQueue(Q, x); EnQueue(Q,a);while ( ! QueueEmpty(Q) ) DeQueue(Q, y); printf(y);答:cha5、 簡(jiǎn)述下列算法的功能(棧和隊(duì)列的元素類型均為int)。void algo3(Queue &Q) Stack S; int d; InitStack (S); while ( ! QueueEmpt(Q) ) DeQueue(Q, d); Push(S, d);while ( ! StackEmpty(S) ) Pop(S, d); EnQueue(Q, d);

5、 答:利用棧S將隊(duì)列Q中的元素進(jìn)行逆置。6、 為了充分利用空間,將兩個(gè)棧共同存儲(chǔ)在長(zhǎng)度為n的一維數(shù)組中,共享數(shù)組空間。設(shè)計(jì)兩個(gè)棧共享一維數(shù)組的存儲(chǔ)表示方式,畫出示意圖。答:設(shè)計(jì)兩個(gè)棧Stack1和Stack2共享數(shù)組空間,Stack1的棧底放在數(shù)組的首端,Stack2的棧底放在數(shù)組的尾端,分別向兩個(gè)棧中存儲(chǔ)相應(yīng)的元素,兩個(gè)棧的棧頂分別向數(shù)組中間擴(kuò)展。當(dāng)總的存儲(chǔ)空間不大于數(shù)組長(zhǎng)度時(shí),數(shù)組空間將得到最大利用。當(dāng)兩個(gè)棧占滿整個(gè)數(shù)組空間時(shí),這時(shí)兩個(gè)棧共享一個(gè)長(zhǎng)度為n的數(shù)組空間,再向棧中放元素時(shí)會(huì)發(fā)生上溢。0(Stack1底)12Stack1頂Stack2頂n-3n-2n-1(Stack2底)實(shí)驗(yàn)二1、

6、簡(jiǎn)單計(jì)算器。請(qǐng)按照四則運(yùn)算加、減、乘、除、冪()和括號(hào)的優(yōu)先關(guān)系和慣例,編寫計(jì)算器程序。要求: 從鍵盤輸入一個(gè)完整的表達(dá)式,以回車作為表達(dá)式輸入結(jié)束的標(biāo)志。 輸入表達(dá)式中的數(shù)值均為大于等于零的整數(shù)。中間的計(jì)算過(guò)程如果出現(xiàn)小數(shù)也只取整。例如,輸入:4+2*5=輸出:14 輸入:(4+2)*(2-10)=輸出:-48程序如下:#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define OK 1#define ERROR 0#define OVERFLOW -2#define STACK_INIT_SI

7、ZE 100 /存儲(chǔ)空間初始分配量#define STACKINCREMENT 10 /存儲(chǔ)空間分配增量typedef struct/定義運(yùn)算符棧數(shù)據(jù)類型 char *base; char *top; int stacksize;SqStack1;typedef struct/定義操作數(shù)棧數(shù)據(jù)類型 int *base; int *top; int stacksize;SqStack2;int InitStack1(SqStack1 &S)/構(gòu)造一個(gè)空的運(yùn)算符棧 S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char); if(!S.base)

8、 exit(OVERFLOW); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK;/InitStack1int InitStack2(SqStack2 &S)/構(gòu)造一個(gè)空的操作數(shù)棧 S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int); if(!S.base) exit(OVERFLOW); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK;/InitStack2char GetTop1(SqStack1 S)/若棧不空,則用ch

9、ar型元素e返回S的棧頂元素,并返回OK;否則返回ERROR char e; if(S.top=S.base) return ERROR; e=*(S.top-1); return e;/GetTop1int GetTop2(SqStack2 S)/若棧不空,則用int型元素e返回S的棧頂元素,并返回OK;否則返回ERROR int e; if(S.top=S.base) return ERROR; e=*(S.top-1); return e;/GetTop2int Push1(SqStack1 &S,char e)/插入char型元素e為新的棧頂元素 if(S.top-S.base

10、>=S.stacksize) S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char); if(!S.base) exit(OVERFLOW); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; *S.top+=e; return OK;/Push1int Push2(SqStack2 &S,int e)/插入int型元素e為新的棧頂元素 if(S.top-S.base>=S.stacksize) S.base=(int *)re

11、alloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int); if(!S.base) exit(OVERFLOW); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; *S.top+=e; return OK;/Push2int Pop1(SqStack1 &S,char &e)/若棧不空,則刪除S的棧頂元素,用char型元素e返回其值,并返回OK;否則返回ERROR if(S.top=S.base) return ERROR; e=*-S.top; return OK;

12、/Pop1int Pop2(SqStack2 &S,int &e)/若棧不空,則刪除S的棧頂元素,用int型元素e返回其值,并返回OK;否則返回ERROR if(S.top=S.base) return ERROR; e=*-S.top; return OK;/Pop2char Precede(char a,char b)/比較兩個(gè)相繼出現(xiàn)的運(yùn)算符a和b間的優(yōu)先級(jí)關(guān)系switch(a)case '+':switch(b)case'+':return'>'break;case'-':return'>

13、;'break;case'*':return'<'break;case'/':return'<'break;case'(':return'<'break;case')':return'>'break;case'=':return'>'break;case'':return'<'break;case'-':switch(b)case'+&#

14、39;:return'>'break;case'-':return'>'break;case'*':return'<'break;case'/':return'<'break;case'(':return'<'break;case')':return'>'break;case'=':return'>'break;case'':ret

15、urn'<'break;case'*':switch(b)case'+':return'>'break;case'-':return'>'break;case'*':return'>'break;case'/':return'>'break;case'(':return'<'break;case')':return'>'break;

16、case'=':return'>'break;case'':return'<'break;case'/':switch(b)case'+':return'>'break;case'-':return'>'break;case'*':return'>'break;case'/':return'>'break;case'(':return&#

17、39;<'break;case')':return'>'break;case'=':return'>'break;case'':return'<'break;case'':switch(b)case'+':return'>'break;case'-':return'>'break;case'*':return'>'break;case

18、9;/':return'>'break;case'(':return'<'break;case')':return'>'break;case'=':return'>'break;case'':return'>'break;case'(':switch(b)case'+':return'<'break;case'-':return'<

19、'break;case'*':return'<'break;case'/':return'<'break;case'(':return'<'break;case')':return'='break;case'':return'<'break;case')':switch(b)case'+':return'>'break;case'-':

20、return'>'break;case'*':return'>'break;case'/':return'>'break;case')':return'>'break;case'=':return'>'break;case'':return'>'break;case'#':switch(b)case'+':return'<'brea

21、k;case'-':return'<'break;case'*':return'<'break;case'/':return'<'break;case'(':return'<'break;case'=':return'='break;case'':return'<'break;/Precedeint pow(int a,int b)/求冪函數(shù) int x; for(x=1;b

22、>0;b-) x=x*a; return x;/powint Operate(int a,char theta,int b)/操作數(shù)a和b進(jìn)行運(yùn)算 switch(theta)case'+':return(a+b);case'-':return(a-b);case'*':return(a*b);case'/':return(a/b);case'':return(pow(a,b);/Operateint In(char c)/判斷字符c是否為運(yùn)算符if(c!='+'&&c!='-'&&c!='*'&&c!='/'&&c!=

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論