數(shù)據(jù)結構及數(shù)據(jù)庫及實驗報告_第1頁
數(shù)據(jù)結構及數(shù)據(jù)庫及實驗報告_第2頁
數(shù)據(jù)結構及數(shù)據(jù)庫及實驗報告_第3頁
數(shù)據(jù)結構及數(shù)據(jù)庫及實驗報告_第4頁
數(shù)據(jù)結構及數(shù)據(jù)庫及實驗報告_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

-.z實驗1ADT表的編程與實現(xiàn)電工二**:*德良**:6實驗目的:加深對抽象數(shù)據(jù)類型ADT表的理解;實驗原理:參照課本p.44-49,及Figure3.6-3.13.實驗內(nèi)容:編寫程序實現(xiàn)ADT表的定義,及常用操作:1〕、判斷表是否為空;源程序*include"stdaf*.h"*include"stdio.h"*include"stdlib.h"*include"malloc.h"http://定義鏈表中的節(jié)點typedefstructnode{intmember;//節(jié)點中的元素structnode*pNe*t;//指向下一個節(jié)點的指針}Node,*pNode;//函數(shù)聲明pNodeCreateList();//創(chuàng)立鏈表函數(shù)voidTraverseList(pNode);//遍歷鏈表函數(shù)boolIs_Empty(pNode);//判斷鏈表是否為空intmain(){pNodepHead=NULL;//定義初始化頭節(jié)點structNode*pHead==NULLintflag;//存放鏈表是否為空的標志,intLen;pHead=CreateList();//創(chuàng)立一個非循環(huán)單鏈表,并將該鏈表的頭結點的地址付給pHeadTraverseList(pHead);//調用遍歷鏈表函數(shù)if(Is_Empty(pHead)==true)//判斷列表是否為空{return0;}return0;}//創(chuàng)立鏈表函數(shù)pNodeCreateList(){inti;intlen;intval;pNodepHead=(pNode)malloc(sizeof(Node));pNodepTail=pHead;pTail->pNe*t=NULL;printf("請輸入節(jié)點個數(shù):");scanf("%d",&len);for(i=0;i<len;i++){printf("第%d個節(jié)點的數(shù)值:",i+1);scanf("%d",&val);pNodepNew=(pNode)malloc(sizeof(Node));pNew->member=val;pTail->pNe*t=pNew;pNew->pNe*t=NULL;pTail=pNew;}returnpHead;}//遍歷鏈表函數(shù)voidTraverseList(pNodepHead){pNodep=pHead->pNe*t;while(NULL!=p){printf("%d",p->member);p=p->pNe*t;}printf("\n");return;}//判斷鏈表是否為空boolIs_Empty(pNodepHead){if(NULL==pHead->pNe*t){printf("鏈表為空!\n");returntrue;}else{returnfalse;}}運行結果輸入節(jié)點個數(shù)及各節(jié)點數(shù)值如下2〕獲取第i個節(jié)點的內(nèi)容源程序*include"stdaf*.h"*include"stdio.h"*include"stdlib.h"*include"malloc.h"*definema*size50typedefstruct{intdata[ma*size];intlast;}Sequenlist;Sequenlist*InitList()//創(chuàng)立順序表{Sequenlist*L;L=(Sequenlist*)malloc(sizeof(Sequenlist));L->last=0;return(L);}Sequenlist*creat()//創(chuàng)立一個有具體內(nèi)容的順序表{Sequenlist*L;inti=1,n;L=InitList();printf("請輸入數(shù)據(jù),以0完畢\n");scanf("%d",&n);while(n!=0&&L->last<ma*size){L->data[i]=n;i=i++;L->last++;scanf("%d",&n);}return(L);}intGetData(Sequenlist*L,inti)//獲取第i個元素{if(i>=1&&i<=L->last)return(L->data[i]);else{printf("參數(shù)i不合理!\n");return0;}}voidmain(){intlength,i;intvalue,number,location;intj,k;intflag;Sequenlist*L;L=creat();printf("Pleaseinputthelocation\n");scanf("%d",&location);value=GetData(L,location);printf("Thenumberis%d\n",value);}運行程序依次輸入鏈表元素的值,以0完畢輸入要查找元素的位置3〕鏈表刪除元素源程序*include"stdaf*.h"*include"stdio.h"*include"stdlib.h"*include"malloc.h"typedefintElemType;typedefvoidStatus;typedefstructNode{ ElemTypedata; structNode*ne*t;}LNode,*LinkList;StatusCreatList(int,LinkList);StatusTraverse(LinkList);StatusFreeList(LinkList);ElemTypeDelElem(LinkList,ElemType);intmain(){ intLength; ElemTypee; LinkListL; L=(LinkList)malloc(sizeof(LNode)); L->ne*t=NULL; printf("InputtheLengthofList:\n"); scanf("%d",&Length); CreatList(Length,L); printf("TraverstheList:\n"); Traverse(L); while(1) { printf("請輸入要刪除的元素:\n"); scanf("%d",&e); if(DelElem(L,e))//在L中將元素e刪除 { printf("刪除%d后:\n",e);break; } printf("未找到該元素,刪除失敗\n"); } Traverse(L); FreeList(L); printf("ListreleaseSuccess!\n"); return0;}StatusCreatList(intLength,LinkListL){ inti; LinkListBody=NULL,p=L; for(i=0;i<Length;i++) { Body=(LinkList)malloc(sizeof(LNode)); p->ne*t=Body; printf("Inputthe%dthnum:\n",i+1); scanf("%d",&(Body->data)); p=Body; } Body->ne*t=NULL;}StatusFreeList(LinkListL){ LinkListtemp=NULL,p=L; while(p) { temp=p->ne*t; free(p); p=temp; }}StatusTraverse(LinkListL){ LinkListp=L; p=p->ne*t; while(p)//while(p!=NULL) { printf("%d",p->data); p=p->ne*t; } printf("\n");}ElemTypeDelElem(LinkListL,ElemTypee){LinkListp,q; q=p=L; p=p->ne*t;//p指向頭結點后第一個元素 while(p) { if(p->data==e) { q->ne*t=p->ne*t; free(p); return1; } else { q=p;//q始終指向p上一個結點p=p->ne*t; } } //沒有執(zhí)行上個return,說明沒找到 return0;}運行結果:輸入節(jié)點個數(shù)依次輸入各個節(jié)點的數(shù)值輸入要刪除的元素4〕鏈表插入元素。源程序*include"stdaf*.h"*include"stdio.h"*include"stdlib.h"*include"malloc.h"*defineN5typedefintelemtype;typedefstructnode{elemtypedata;structnode*ne*t;}linklist;linklist*Creatlist(linklist*L){L=(linklist*)malloc(sizeof(linklist));L->ne*t=NULL;returnL;}intJudge(linklist*L){if(L->ne*t==NULL){printf("建表成功...\n");}elseprintf("建表失敗.\n");return0;}intInput(linklist*L,int*,linklist*r){inti;linklist*p;p=(linklist*)malloc(sizeof(linklist));p->data=*;p->ne*t=NULL;r->ne*t=p;printf("%d",p->data);return0;}intInsert1(linklist*L,inti){linklist*p,*q,*r,*t;intj=1,item;p=L->ne*t;q=L;r=L;if(L->ne*t==NULL){printf("表空.\n");return0;}else{while(p!=NULL&&j<i){q=p;p=p->ne*t;j++;}if(p==NULL){printf("%d不在表的*圍內(nèi).\n");return0;}else{t=(linklist*)malloc(sizeof(linklist));t->ne*t=NULL;printf("請輸入item:");scanf("%d",&item);t->data=item;t->ne*t=p;q->ne*t=t;}printf("在第%d位上插入值為%d的節(jié)點后的所有數(shù)據(jù)為\n",i

溫馨提示

  • 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

提交評論