山東大學(xué)數(shù)據(jù)結(jié)構(gòu)試驗(yàn)報(bào)告三_第1頁
山東大學(xué)數(shù)據(jù)結(jié)構(gòu)試驗(yàn)報(bào)告三_第2頁
山東大學(xué)數(shù)據(jù)結(jié)構(gòu)試驗(yàn)報(bào)告三_第3頁
山東大學(xué)數(shù)據(jù)結(jié)構(gòu)試驗(yàn)報(bào)告三_第4頁
山東大學(xué)數(shù)據(jù)結(jié)構(gòu)試驗(yàn)報(bào)告三_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

山東大學(xué)軟件工程學(xué)院數(shù)據(jù)結(jié)構(gòu)課程實(shí)驗(yàn)報(bào)告學(xué)號(hào):姓名:班級(jí):軟件工程2014級(jí)2班實(shí)驗(yàn)題目:線性表操作實(shí)驗(yàn)學(xué)時(shí):實(shí)驗(yàn)日期:2015.11.4實(shí)驗(yàn)?zāi)康模?、掌握線性表的基本操作:插入、刪除、查找。2、掌握鏈表遍歷器的使用方法。硬件環(huán)境:實(shí)驗(yàn)室軟件環(huán)境:VistualStudio2013實(shí)驗(yàn)步驟與內(nèi)容:實(shí)驗(yàn)內(nèi)容:1、創(chuàng)建線性表類。線性表的存儲(chǔ)結(jié)構(gòu)使用鏈表。2、提供操作:自表首插入元素、刪除指定元素、搜索表中是否有指定元素、輸出鏈表。3、接收鍵盤錄入的一系列整數(shù)(例10,25,8,33,60)作為節(jié)點(diǎn)的元素值,創(chuàng)建鏈表。輸出鏈表內(nèi)容。4、輸入一個(gè)整數(shù)(例33),在鏈表中進(jìn)行搜索,輸出其在鏈表中的位置。如果不存在輸出0。5、使用鏈表遍歷器實(shí)現(xiàn)鏈表的反序輸出。6、創(chuàng)建兩個(gè)有序鏈表,使用鏈表遍歷器實(shí)現(xiàn)鏈表的合并。代碼體:Chainnode.h#ifndefCHAINNODE_H#defineCHAINNODE_HclassChainNode(friendclassList;friendclassListIterator;private:intdata;ChainNode*link;};#endifList.h#ifndefLIST_H#defineLIST_H#include<iostream>usingnamespacestd;#include"ChainNode.h"classList(friendclassListIterator;public:List(){first=0;}

~List();boolIsEmpty()const{returnfirst==0;}intLength()const;boolFind(intk,int&x)const;intSearch(constint&x)const;List&Delete(intk,int&x);List&Add(constint&x);voidOutput(ostream&out)const;private:ChainNode*first;};#endifList.cpp#include<iostream>usingnamespacestd;#include"List.h"List::?List(){〃鏈表的析構(gòu)函數(shù),用于刪除鏈表中的所有節(jié)點(diǎn)ChainNode*next;while(first){while(first){next=first->link;deletefirst;first=next;}}intList::Length()const(//返回鏈表中的元素總數(shù)ChainNode*p=first;intlength=0;while(p){length++;p=p->link;}returnlength;}boolList::Find(intk,int&x)const{//尋找鏈表中的第k個(gè)元素,并將其傳送至x〃如果不存在第k個(gè)元素,則返回false,否則返回trueif(k<1||k>Length())returnfalse;ChainNode*p=first;intindex=1;while(index!=k){index++;}"i;x=p->data;returntrue;回x的地址〃如果x不在鏈表中,則返回0ChainNode*p=first;intindex=1;while(p&&p->data!=x){index++;「…;if(p)returnindex;elsereturn0;List&List::Delete(intk,int&x){〃按位置刪除元素if(k<1||k>Length()){cout<<〃不存在所要?jiǎng)h除的元素〃<<endl;return*this;}if(k==1)first=first->link;else{ChainNode*p=first;intindex=1;while(index!=k-1){index++;p=p->link;}p->link=p->link->link;}return*this;}List&List::Add(constint&x){〃在鏈表頭進(jìn)行鏈表的添加操作ChainNode*p=newChainNode;p->data=x;p->link=first;first=p;return*this;}voidList::Output(ostream&out)const{//將鏈表元素送至輸出流ChainNode*p;for(p=first;p;p=p->link){out<<p->data<<””;}out<<endl;}listiterator.h#ifndefLISTITERATOR_H#defineLISTITERATOR_H#include"ChainNode.h"classListIterator(public:int*Initialize(constList&c);int*Next();private:ChainNode"location;};#endiflistiterator.cpp#include〃ListIterator.h"#include"List.h"int*ListIterator::Initialize(constList&c){location=c.first;if(location)return&location->data;return0;}int*ListIterator::Next(){if(!location)return0;location=location->link;if(location)return&location->data;return0;}Test.cpp#include<iostream>#include<stdlib.h>usingnamespacestd;#include"List.h"#includeListIterator.hintmain(){/********************************************/〃從鍵盤輸入一組數(shù)存入鏈表并輸出/********************************************/List*list1=newList();//建立鏈表1,輸入的數(shù)存入其中intx;cout<<〃請(qǐng)輸入一系列整數(shù)作為鏈表節(jié)點(diǎn)的元素值(回車后按Ctrl+z鍵后再按回車結(jié)束輸出):〃<<endl;while(cin>>x){list1->Add(x);}cin.clear();cout<<〃鏈表內(nèi)容為:〃<<endl;list1->Output(cout);//輸出鏈表1cout<<〃請(qǐng)輸入你想尋找的鏈表中的整數(shù),將輸出其在鏈表中的位置(不存在則為0):〃<<endl;inty;cin>>y;cout<<〃位置為:"<<list1->Search(y)<<endl;/*******************************************/〃將鏈表反向輸出/*******************************************/cout<<〃反序輸出為:〃<<endl;ListIterator"iterator=newListIterator();//建立鏈表遍歷器,遍歷鏈表1int*n;n=iterator->Initialize(*list1);List*list2=newList();//建立反向鏈表while(n){//將鏈表1中的元素放入反向鏈表list2->Add(*n);n=iterator->Next();}list2->Output(cout);//輸出反向鏈表/*******************************************/〃實(shí)現(xiàn)鏈表的合并/*******************************************/List*list3=newList();//建立鏈表aList*list4=newList();//建立鏈表bfor(inti=10;i>=0;i--){list3->Add(2*i+1);}for(intj=5;j>=0;j--){list4->Add(2*j);}cout<<〃鏈表a為:"<<endl;list3->Output(cout);〃輸出鏈表acout<<〃鏈表b為:"<<endl;list4->Output(cout);〃輸出鏈表bListIterator*iterator1=newListIterator();〃建立鏈表遍歷器1,遍歷鏈表aListIterator*iterator2=newListIterator();〃建立鏈表遍歷器2,遍歷鏈表bList*list5=newList();〃建立鏈表5int*a=iterator1->Initialize(*list3),*b=iterator2->Initialize(*list4);while(a&&b){〃將鏈表a、b中的元素按大小先后輸入鏈表5,得到按從大到小排序的鏈表if(*a<*b){list5->Add(*a);a=iterator1->Next();}else{list5->Add(*b);b=iterator2->Next();}}while(a){list5->Add(*a);a=iterator1->Next();}while(b){list5->Add(*b);b=iterator2->Next();}n=iterator->Initialize(*list5);〃遍歷鏈表5List*list6=newList();〃建立合并鏈表while(n){〃將鏈表5反向,得到鏈表a、b的合并鏈表list6->Add(*n);n=iterator->Next();}cout<<〃合并后鏈表為:〃<<endl;list6->Output(cout);system(〃pause〃);return0;實(shí)驗(yàn)結(jié)果:已\uvmrnqufub\documents\vizua\studio2013\Proj^^3\Debu— □X輸入一系列整數(shù)作為鏈表節(jié)點(diǎn)的元素值(按匚七式+工鍵結(jié)束輸出):1234567894444568鏈表內(nèi)容為:S5644449S7654221輸入要尋找的鏈表中的整數(shù),將輸出其在鏈表中的位置(不存在則為G:倉直歷

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論