合肥學(xué)院數(shù)據(jù)結(jié)構(gòu)實驗報告.doc_第1頁
合肥學(xué)院數(shù)據(jù)結(jié)構(gòu)實驗報告.doc_第2頁
合肥學(xué)院數(shù)據(jù)結(jié)構(gòu)實驗報告.doc_第3頁
合肥學(xué)院數(shù)據(jù)結(jié)構(gòu)實驗報告.doc_第4頁
合肥學(xué)院數(shù)據(jù)結(jié)構(gòu)實驗報告.doc_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

合肥學(xué)院 數(shù)學(xué)與物理系實驗報告課程名稱: 數(shù)據(jù)結(jié)構(gòu)與算法語言 實驗項目: 順序表、鏈表、棧和隊列的應(yīng)用 實驗類別: 綜合型 學(xué)生姓名: 學(xué) 號: 專業(yè)班級: 實驗時間: 2012.02.122012.04.05 指導(dǎo)教師: 邵桂偉 實驗成績: 一、實驗?zāi)康模?.了解順序表的建立及使用;2.了解鏈表的結(jié)構(gòu),掌握其使用方法;3.熟悉棧的應(yīng)用。二、實驗要求:1.根據(jù)具體問題實現(xiàn)順序表的查找并輸出;2.根據(jù)具體問題掌握循環(huán)單鏈表的操作應(yīng)用;3.根據(jù)具體問題實現(xiàn)棧及隊列的操作應(yīng)用。三、實驗內(nèi)容:1.程序?qū)崿F(xiàn)順序表的查找(查找成功、不成功兩種情況)。2.程序?qū)崿F(xiàn)循環(huán)單鏈表的建立、插入元素和刪除。3.程序?qū)崿F(xiàn)查找一元素,用棧和隊列分別實現(xiàn)。四、數(shù)據(jù)結(jié)構(gòu):1. 順序表的查找typedef structint key;int other;recordtype;typedef structrecordtype rlist_size;int length;recordlist;2. 循環(huán)單鏈表的建立、插入元素和刪除typedef struct LNode int data; struct LNode *next;LNode,*LinkList;3.a).隊列實現(xiàn)查找元素#define maxlen 100typedef structdatatype datamaxlen;int front;int rear;SeqQueue;b). 棧實現(xiàn)查找元素typedef structint data100; int top;SqStack;五、源程序及結(jié)果:1.(1)源程序(小四羅馬字體)#include#define LIST_SIZE 10typedef struct int key;int other;Recordtype;typedef structRecordtype rLIST_SIZE;int length;Recordlist;void main()Recordlist p,*L=&p;int i,x,n=0;L-length=10;printf(請輸入10個數(shù):n);for(i=0;ilength;i+)scanf(%d,&L-ri.key);printf(請輸入待查元素:n);scanf(%d,&x);for(i=0;ilength;i+)if(L-ri.key=x)n=i;if(n=0)printf(沒有找到該元素);elseprintf(位置是:%d,n);(2)運行結(jié)果(可以使用截圖)2.(1)源程序:#include #include #include typedef struct nodeint data;struct node *next;* linklist,LNode;int main()linklist L,newp,p,q,s;int i,n,e,j=1,t;L=(linklist)malloc(100*sizeof(LNode);(*L).next=NULL;printf(n表長:);scanf(%d,&n);printf(n表元素:);p=L;for(i=0;idata); p-next=newp;p=p-next;p-next=NULL;printf(n建立的鏈表:);s=L-next;while(s!=NULL)printf(%3d,s-data);s=s-next;printf(nn請輸入要插入的元素:);scanf(%d,&e);q=L;p=(*L).next;while(p!=NULL & jnext;if(p!=NULL)newp=(linklist)malloc(sizeof(LNode);newp-data=e;newp-next=p-next;p-next=newp;printf(n插入元素后的鏈表:);s=L-next;while(s!=NULL)printf(%3d,s-data);s=s-next;printf(nn請輸入要刪除的元素:);scanf(%d,&e); i=0; t=0; p=L-next; while(p!=NULL) i+; if(p-data=e) t=i; break; p=p-next; q=L;p=(*L).next;j=1;while(p!=NULL & jnext;if(p!=NULL)q-next=p-next;t=p-data;free(p);printf(n刪除元素后的鏈表:);s=L-next;while(s!=NULL)printf(%3d,s-data);s=s-next;return 0;(2)運行結(jié)果(可以使用截圖)3.(1)源程序:A.隊列實現(xiàn)元素查找#includestdio.h#includestdlib.h#includeiostream.h#define OK 1#define ERROR 0typedef int status;typedef struct Qnodechar data;struct Qnode *next;Qnode, *queueptr;typedef structqueueptr front;queueptr rear;linkqueue; status ini(linkqueue &Q) /初始化Q.front=Q.rear=(queueptr)malloc(sizeof(Qnode);if(!Q.front)exit(0);Q.front-next=NULL;return OK;status en(linkqueue &Q,char e) /進隊Qnode *p;p=(queueptr)malloc(sizeof(Qnode);if(!p)exit(0);p-data=e;p-next=NULL;Q.rear-next=p;Q.rear=p;return OK;status de(linkqueue &Q,char &e) /出隊if(Q.front=Q.rear)return ERROR;queueptr p;p=Q.front-next;e=p-data;Q.front-next=p-next;if(Q.rear=p)Q.rear=Q.front;free(p);return OK;void main()linkqueue Q;ini(Q);char in;char out;char temp;int flag=0; printf(輸入要入隊的元素(回車間隔,#結(jié)束): n);do scanf(%c,&in); en(Q,in);while(in!=#); printf(輸入要查找的元素: ); cinout; /此處C的輸入應(yīng)為 scanf(%c,&out);do de(Q,temp); if(temp=out) printf(查找成功!n ); flag=1; while(de(Q,temp)!=0);if(flag=0) printf(隊列中無此元素!n );B棧實現(xiàn)元素查找#include stdio.h#include stdlib.h#include iostream.h#define ERROR 0;#define OK 1;#define TRUE 1;#define FALSE 0;#define LIST_INIT_SIZE 100 #define STACKINCREMENT 10 typedef char SElemType;typedef struct SElemType *top; SElemType *base; int stacksize; SqStack;int InitStack(SqStack &S) /構(gòu)造 S.base=(SElemType*)malloc(LIST_INIT_SIZE*sizeof(SElemType); if(!S.base) exit(0);S.top=S.base;S.stacksize=LIST_INIT_SIZE; return OK; int GetTop(SqStack &S,SElemType &e) /返e if(S.base=S.top) return ERROR; e=*(S.top-1); return OK; int Push(SqStack &S,SElemType e) /入棧 if(S.top-S.base=S.stacksize) S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType); if(!S.base) exit(EXIT_FAILURE); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; *S.top+=e; return OK; int Pop(SqStack &S,SElemType &e) /出棧 if(S.top=S.base) return ERROR; e=*(-S.top); return OK; void main()SqStack L;InitStack(L);char r;char t;int flag=0;printf(入棧:(每個以回車間隔,以#結(jié)束):n); do scanf(%c,&r);

溫馨提示

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

評論

0/150

提交評論