數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告_第1頁(yè)
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告_第2頁(yè)
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告_第3頁(yè)
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告_第4頁(yè)
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩9頁(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、數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告一、實(shí)驗(yàn)?zāi)康募耙?)掌握棧和隊(duì)列這兩種特殊的線性表,熟悉它們的特性,在實(shí)際問(wèn)題背景下靈活運(yùn)用它們。本實(shí)驗(yàn)訓(xùn)練的要點(diǎn)是“?!焙汀瓣?duì)列”的觀點(diǎn);二、實(shí)驗(yàn)內(nèi)容1) 利用棧,實(shí)現(xiàn)數(shù)制轉(zhuǎn)換。2) 利用棧,實(shí)現(xiàn)任一個(gè)表達(dá)式中的語(yǔ)法檢查(選做)。3) 編程實(shí)現(xiàn)隊(duì)列在兩種存儲(chǔ)結(jié)構(gòu)中的基本操作(隊(duì)列的初始化、判隊(duì)列空、入隊(duì)列、出隊(duì)列);三、實(shí)驗(yàn)流程、操作步驟或核心代碼、算法片段順序棧:Status InitStack(SqStack &S)S.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType);if(!S.base)return ER

2、ROR;S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;Status DestoryStack(SqStack &S)free(S.base);return OK;Status ClearStack(SqStack &S)S.top=S.base;return OK;Status StackEmpty(SqStack S)if(S.base=S.top)return OK;return ERROR;int StackLength(SqStack S)return S.top-S.base;Status GetTop(SqStack S,El

3、emType &e)if(S.top-S.base=S.stacksize)S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType);if(!S.base) return ERROR;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;*S.top+=e;return OK;Status Push(SqStack &S,ElemType e)if(S.top-S.base=S.stacksize)S.base=(ElemType *)rea

4、lloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType);if(!S.base)return ERROR;S.top=S.base+S.stacksize;S.stacksize+=STACKINCREMENT;*S.top+=e;return OK;Status Pop(SqStack &S,ElemType &e)if(S.top=S.base)return ERROR;e=*-S.top;return OK;Status StackTraverse(SqStack S)ElemType *p;p=(ElemType *)mallo

5、c(sizeof(ElemType);if(!p) return ERROR;p=S.top;while(p!=S.base)/S.top上面一個(gè).p-;printf(%d ,*p);return OK;Status Compare(SqStack &S)int flag,TURE=OK,FALSE=ERROR;ElemType e,x;InitStack(S);flag=OK;printf(請(qǐng)輸入要進(jìn)?;虺鰲5脑兀?;while(x= getchar)!=#&flag)switch (x)case (:case :case :if(Push(S,x)=OK)printf(括號(hào)匹配成功!nn

6、);break;case ):if(Pop(S,e)=ERROR | e!=()printf(沒(méi)有滿足條件n);flag=FALSE;break;case :if ( Pop(S,e)=ERROR | e!=)flag=FALSE;break;case :if ( Pop(S,e)=ERROR | e!=)flag=FALSE;break;if (flag & x=# & StackEmpty(S)return OK;elsereturn ERROR;鏈隊(duì)列:Status InitQueue(LinkQueue &Q)Q.front =Q.rear=(QueuePtr)malloc(sizeo

7、f(QNode);if (!Q.front) return ERROR;Q.front-next = NULL;return OK;Status DestoryQueue(LinkQueue &Q)while(Q.front)Q.rear=Q.front-next;free(Q.front);Q.front=Q.rear;return OK;Status QueueEmpty(LinkQueue &Q)if(Q.front-next=NULL)return OK;return ERROR;Status QueueLength(LinkQueue Q)int i=0;QueuePtr p,q;p

8、=Q.front;while(p-next)i+;p=Q.front;q=p-next;p=q;return i;Status GetHead(LinkQueue Q,ElemType &e)QueuePtr p;p=Q.front-next;if(!p)return ERROR;e=p-data;return e;Status ClearQueue(LinkQueue &Q)QueuePtr p;while(Q.front-next )p=Q.front-next;free(Q.front);Q.front=p;Q.front-next=NULL;Q.rear-next=NULL;retur

9、n OK;Status EnQueue(LinkQueue &Q,ElemType e)QueuePtr p;p=(QueuePtr)malloc(sizeof (QNode);if(!p)return ERROR;p-data=e;p-next=NULL;Q.rear-next = p;Q.rear=p; /p-next 為空return OK;Status DeQueue(LinkQueue &Q,ElemType &e)QueuePtr p;if (Q.front = Q.rear)return ERROR;p = Q.front-next;e = p-data;Q.front-next

10、 = p-next;if (Q.rear = p)Q.rear = Q.front; /只有一個(gè)元素時(shí)(不存在指向尾指針)free (p);return OK;Status QueueTraverse(LinkQueue Q)QueuePtr p,q;if( QueueEmpty(Q)=OK)printf(這是一個(gè)空隊(duì)列!n);return ERROR;p=Q.front-next;while(p)q=p;printf(%ddata);q=p-next;p=q;return OK;循環(huán)隊(duì)列:Status InitQueue(SqQueue &Q)Q.base=(QElemType*)mallo

11、c(MAXQSIZE*sizeof(QElemType);if(!Q.base)exit(OWERFLOW);Q.front=Q.rear=0;return OK;Status EnQueue(SqQueue &Q,QElemType e)if(Q.rear+1)%MAXQSIZE=Q.front)return ERROR;Q.baseQ.rear=e;Q.rear=(Q.rear+1)%MAXQSIZE;return OK;Status DeQueue(SqQueue &Q,QElemType &e)if(Q.front=Q.rear)return ERROR;e=Q.baseQ.front;Q.front=(Q.front+1)%MAXQSIZE;return OK;int QueueLength(SqQueue Q)return(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;Status DestoryQueue(SqQueue &Q)free(Q.base);return OK;Status QueueEmpty(SqQueue Q) /判空if

溫馨提示

  • 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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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)論