1. 程式人生 > >《資料結構》實驗二:線性表的實驗(實驗報告)

《資料結構》實驗二:線性表的實驗(實驗報告)

一.實驗目的

     鞏固線性表的資料結構,學會線性表的應用。

1.回顧線性表的邏輯結構,線性表的物理儲存結構和常見操作。

2.學習運用線性表的知識來解決實際問題。

3.進一步鞏固程式除錯方法。

4.進一步鞏固模板程式設計。

二.實驗時間

   準備時間為第2周到第4周,具體集中實驗時間為第4周第2次課。2個學時。

三.實驗內容

1.建立一個N個學生成績的順序表,對錶進行插入、刪除、查詢等操作。分別輸出結果。

要求如下:

1)用順序表來實現。

2)用單鏈表來實現。

2.解決約瑟夫問題

    設有編號為1,2,3,n的n(n>0)個人圍在一起,每人持有一個密碼m,從第一個人開始報數,報到m時停止報數,報m的人出圈,再從下一個人開始重新報數,報到m時停止報數,報m的人出圈,……直到的所有人出圈為止。當給定n和m後,輸出出圈的次序。

要求如下:自定義資料結構,確定儲存方法,並設計演算法。在主程式中輸入n和m後,輸出結果。

3.實現兩個集合的相等判定、並、交和差運算。要求:

  1)自定義資料結構

  2)自先儲存結構,並設計演算法。在VC中實現。

以上三題,第1題必須完成。第2和第3題可以作為選做題。

三.實驗結果

實驗2.1.1

#include<iostream.h>
const int MaxSize=100;
class SeqList
{
public:
SeqList(){length=0;}
SeqList(int a[],int n);
~SeqList(){}
void Insert(int i,int x);
int Delete(int i);
int Get(int i);
int Locate(int x);
void PrintList();
private:
int data[MaxSize];
int length;
};
SeqList::SeqList(int a[],int n)
{
if(n>MaxSize) throw"引數非法";
for(int i=0;i<n;i++)
data[i]=a[i];
   length=n;
}
void SeqList::Insert(int i,int x)
{
if(length>=MaxSize) throw"上溢";
if(i<1||i>length+1) throw"位置非法";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
int SeqList::Delete(int i)
{
if(length==0) throw"下溢";
if(i<1||i>length+1) throw"位置非法";
int x=data[i-1];
for(int j=i;j<length;j++)
data[j-1]=data[j];
length--;
return x;
}
int SeqList::Get(int i)
{
if(i<1 && i>length) throw"查詢位置非法";
else return data[i-1];
}
int SeqList::Locate(int x)
{
for(int i=0;i<length;i++)
if(data[i]==x) return i+1;
return 0;
}
void SeqList::PrintList()
{
for(int i=0;i<length;i++)
cout<<data[i]<<" ";
cout<<endl;
}
void main()
{
int score[5],n,m,s;
for(int i=0;i<5;i++)
{
cout<<"請輸入第"<<i+1<<"個學生的成績:";
cin>>score[i];
}
SeqList L(score,5);
cout<<"執行插入操作前的資料為:"<<endl;
L.PrintList();
cout<<"請插入資料:";
cin>>n;
cout<<"插入資料的位置為:";
cin>>m;
L.Insert(m,n);
cout<<"執行插入操作後的資料為:"<<endl;
L.PrintList();
cout<<"請輸入需要查詢資料的位置:";
cin>>m;
cout<<L.Get(m)<<endl;
cout<<"請輸入需要查詢的資料值:";
cin>>s;
cout<<L.Locate(s)<<endl;
cout<<"請輸入需要刪除的資料的位置:";
cin>>m;
L.Delete(m);
cout<<"執行刪除操作後的資料為:"<<endl;
L.PrintList();
}


實驗2.1.2

#include<iostream.h>


struct Node
{
int data;
Node *next;
};


class LinkList
{
public:
LinkList();
LinkList(int a[],int n);
~LinkList();
int Locate(int x);
void Insert(int i,int x);
int Delete(int i);
void PrintList();
private:
Node *first;
};




LinkList::LinkList()
{
first=new Node;
first->next=NULL;
}


LinkList::LinkList(int a[],int n)
{
Node *r,*s;
first=new Node;
r=first;
for(int i=0;i<n;i++)
{
s=new Node;
s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}


LinkList::~LinkList()
{
Node *q=NULL;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}


void LinkList::Insert(int i,int x)
{
Node *p=first,*s=NULL;
int count=0;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL) throw"位置";
else
{
s=new Node;s->data=x;
s->next=p->next;p->next=s;
}
}


int LinkList::Delete(int i)
{
Node *p=first,*q=NULL;
int x,count=0;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL)
throw"位置";
else
{
q=p->next;x=q->data;
p->next=q->next;
delete q;
return x;
}
}


int LinkList::Locate(int x)
{
Node *p=first->next;
int count=1;
while(p!=NULL)
{
if(p->data==x) return count;
p=p->next;
count++;
}
return 0;
}


void LinkList::PrintList()
{
Node *p=first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}


void main()
{
int score[5]={98,60,85,77,90};
LinkList L(score,5);
cout<<"執行插入操作前的資料為:"<<endl;
L.PrintList();
L.Insert(1,100);
cout<<"執行插入操作後的資料為:"<<endl;
L.PrintList();
cout<<"值為100的元素的位置為:";
cout<<L.Locate(60)<<endl;
cout<<"執行刪除操作前的資料為:"<<endl;
L.PrintList();
L.Delete(6);
cout<<"執行刪除操作後的資料為:"<<endl;
L.PrintList();
}