一.实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相
关知识来解决具体问题。
1.建立一个由 n 个学生成绩的顺序表,n 的大小由自己确定,每一个学生的成绩信息由自己
确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
要求如下:
1)用顺序表来实现
2)用单链表来实现
3)用双链表实现
4)用静态链表实现
5)用间接寻址实现
Sequence List
#include<iostream.h>
const int N=5;
class Student
{
private:
intscores[N];
intlength;
public:
Student(intscore[],int n);
~Student(){}
voidInsert(int i,int x);
intDelete(int i);
intGet(int i);
intSearch(int x);
voidShow();
};
Student::Student(int score[],int n)
{
if(n>N)
throw"Parameter illegal";
for(inti=0;i<n;i++)
score[i]=score[i];
length=n;
}
void Student::Insert(int i,int x)
{
if(length>N)
throw"Top full";
if(i<0||i>length+1)
throw"Position illegal";
for(intj=length;j>=i;j--)
scores[j]=scores[j-1];
scores[i-1]=x;
length++;
}
int Student::Delete(int i)
{
if(length<0)
throw"Down full";
if(i<0||i>length+1)
throw"Position illegal";
intx=scores[i-1];
for(intj=i-1;j<length-1;j++)
scores[j]=scores[j+1];
length--;
returnx;
}
int Student::Get(int i)
{
return scores[i-1];
}
int Student::Search(int x)
{
intn;
for(inti=0;i<length;i++)
if(scores[i]==x)
n=i+1;
returnn;
}
void Student::Show()
{
for(inti=0;i<length;i++)
cout<<scores[i]<<'';
cout<<endl;
}
int main()
{
inta[3]={91,75,68};
StudentS(a,3);
cout<<"Original excel:"<<endl;
S.Show();
cout<<"Insert 99 after the first one:"<<endl;
S.Insert(1,99);
cout<<"After 'Insert' operation:"<<endl;
S.Show();
cout<<"Delete the second one:"<<endl;
S.Delete(2);
cout<<"After 'Delete' operation:"<<endl;
S.Show();
cout<<"Search 68:"<<S.Search(68)<<','<<"Search the second one "<<S.Get(2)<<endl;
return0;
}
Single Linked List
#include <iostream>
using namespace std;
const int N=5;
struct Std
{
int date;Std *next;
}*p,*r;
class Student
{
private:Std *first;
int lenght;
public:
Student();
Student(int a[],int n);
~Student(){}
void Insert(int i,int x);
int Delete(int i);
int Get(int i);
int Locate(int x);
void Show();
};
Student::Student()
{
first =new Std;first->next=NULL;
}
Student::Student(int a[],int n)
{
first=new Std;first->next=NULL;
r=first;
for (int i=0;i<n;i++)
{
p=new Std;p->date=a[i];
r->next=p;
r=p;
}
r->next=NULL;
lenght=n;
}
void Student::Insert(int i,int x)
{
r=first;int j=0;
while (r!=NULL&&j<i-1)
{
r=r->next;j++;
}
if (r==NULL) throw "Position illegal";
else
{
p=new Std;p->next=r->next;
p->date=x;
r->next=p;
lenght++;
}
}
int Student::Delete(int i)
{
r=first;int j=0;
while (r!=NULL&&j<i-1)
{
r=r->next;j++;
}
if (r==NULL) throw "Position illegal";
else
{
p=r->next;r->next=p->next;
delete p;
lenght--;
}
return p->date;
}
int Student::Get(int i)
{
r = first;int j = 0;
while (r != NULL && j<i - 1)
{
r = r->next;j++;
}
if (r == NULL) throw "Position illegal";
else
p = r;
return p->date;
}
int Student::Search(int x)
{
r = first;int j=0;
while (r != NULL)
{
r = r->next;j++;
if (r->date == x) break;
}
if (r == NULL) throw "Position illegal"
}
void Student::Show()
{
r = first;while (r->next!= NULL)
{
r = r->next;cout << r->date << ' ';
}
cout<<endl;
}
int main()
{
int a[3] = { 91,75,68};Student S(a, 3);
cout << "Original excel:" << endl;
S.Show();
cout << "Insert 99 after the first one:" << endl;
S.Insert(1, 99);
cout << "After 'Insert' operation:" << endl;
S.Show();
cout << "Delete the second one:" << endl;
S.Delete(2);
cout << " " << endl;
S.Show();
cout << "Search 68:" << S.Search(68) << ',' << "Search the second one :" << S.Get(2) << endl;
return 0;
}
Double Linked List
#include <iostream>
using namespace std;
const int N = 5;
struct Std
{
int date;
Std *next,*prior;
}*p, *r;
class Student
{
private:
Std * first;
int lenght;
public:
Student();
Student(int a[], int n);
~Student() {}
void Insert(int i, int x);
int Delete(int i);
int Get(int i);
int Locate(int x);
int Getper(int x,int i);
void Show();
};
Student::Student()
{
first = new Std;
first->next =first;
first->prior = first;
}
Student::Student(int a[], int n)
{
first = new Std;
first->next = NULL;
r = first;
for (int i = 0; i<n; i++)
{
p = new Std;
p->date = a[i];
r->next = p;
p->prior = r;
r = p;
}
r->next = first;
first->prior = r;
lenght = n;
}
void Student::Insert(int i, int x)
{
r = first;
int j = 0,count = 0;
while (count<=lenght && j<i - 1)
{
r = r->next;
count++;
j++;
}
if (count>lenght) throw "Position illegal";
else
{
p = new Std;
p->next = r->next;
r->next->prior = p;
r->next = p;
p->date = x;
p->prior = r;
lenght++;
}
}
int Student::Delete(int i)
{
r = first;
int x,j = 0,count = 0;
while (count<=lenght && j<i - 1)
{
r = r->next;
count++;
j++;
}
if (count>lenght) throw "Position illegal";
else
{
p = r->next;
p->next->prior = r;
r->next = p->next;
x = p->date;
delete p;
}
return x;
}
int Student::Get(int i)
{
r = first;
int j = 0,count = 0;
while (count<=lenght && j<i - 1)
{
r = r->next;
count++;
j++;
}
if (count>lenght) throw "Position illegal";
else
p = r->next;
return p->date;
}
int Student::Search(int x)
{
r = first;
int j = 0,count = 0;
while (count<=lenght)
{
r = r->next;
count++;
j++;
if (r->date == x) break;
}
if (count>lenght) cout<<"Cannot find the data!"<<endl;
return j;
}
void Student::Show()
{
r = first;
while (r->next != first)
{
r = r->next;
cout << r->date << ' ';
}
cout << endl;
}
int Student::Getper(int x,int i)
{
r = first;
int j,count = 0;
while (count<=lenght)
{
r = r->next;
count++;
if (r->date == x) break;
}
if (count>lenght) throw "";
if (i>lenght) throw "Move front illegal!";
for (j=0;j<i;j++)
r=r->prior;
return r->date;
}
int main()
{
int a[3] = { 91,75,68 };
try
{
Student S(a, 3);
cout << "Original excel:" << endl;
S.Show();
cout << "Insert 99 after the first one:" << endl;
S.Insert(1, 98);
cout << "" << endl;
S.Show();
cout << "Delete the second one:" << endl;
S.Delete(2);
cout << "After 'Delete' operation:" << endl;
S.Show();
cout << "Search 68:" << endl;
cout<<S.Search(68) <<endl;
cout<< "Search the second one" <<endl;
cout<< S.Get(2) << endl;
cout<< "Search the two grades before 75" <<endl;
cout<< S.Getper(75,2) << endl;
}
catch(char *s)
{
cout<<s<<endl;
}
return 0;
}
Static Linked List
#include <iostream>
using namespace std;
const int N=100;
struct Std
{
int date;
int next;
}score[N];
class Student
{
private:
static int first;
static int avail;
int lenght;
public:
Student();
Student(int a[],int n);
~Student(){}
void Insert(int i,int x);
int Delete(int i);
int Get(int i);
int Locate(int x);
void Show();
};
int Student::first=0;
int Student::avail=1;
Student::Student()
{
score[first].next=-1;
for (int i=avail;i<N-1;i++)
score[i].next=i+1;
score[i].next=-1;
}
Student::Student(int a[],int n)
{
int s;cout<<first;
score[first].next=avail;
for (int i=0;i<n;i++)
{
s=avail;
avail=score[avail].next;
score[s].date=a[i];
score[s].next=avail;
}
lenght=n;
}
void Student::Insert(int i,int x)
{
if (i>lenght) throw "Position illegal";
int s;
s=avail;
avail=score[avail].next;
score[s].date=x;
score[s].next=score[i-1].next;
score[i-1].next=s;
lenght++;
}
int Student::Delete(int i)
{
if (i>lenght) throw "Position illegal";
int r=first;
while (score[r].next<i)
r=score[r].next;
score[r].next=score[i].next;
score[i].next=avail;
avail=i;
lenght--;
return score[i].date;
}
int Student::Get(int i)
{
if (i>lenght) throw "Position illegal";
int r=first;
while (r<i)
r=score[r].next;
return score[r].date;
}
int Student::Search(int x)
{
int r=first;
while (score[r].date!=x)
r=score[r].next;
return r;
}
void Student::Show()
{
int r=first;
for (int j=0;j<lenght;j++)
{
r=score[r].next;
cout<<score[r].date<<' ';
}
cout<<endl;
}
int main()
{
Student Ava;
int a[3] = {91,75,68 };
Student S(a, 3);
cout << "Oringinal excel:" << endl;
S.Show();
cout << "Insert 99 after the first one:" << endl;
S.Insert(1,99);
cout << "After 'Insert' operation:" << endl;
S.Show();
cout << "Delete the second one:" << endl;
S.Delete(2);
cout << "After 'Delete' operation:" << endl;
S.Show();
cout << "Search 75" << S.Search(75) << ',' << "Search the second one:" << S.Get(2) << endl;
return 0;
}
Indirect Addressing
#include <iostream>
using namespace std;
const int N=5;
class Student
{
private:
int *scores[N];
int lenght;
public:
Student(int score[],int n);
~Student(){}
void Insert(int i,int *x);
int Delete(int i);
int Get(int i);
int Locate(int x);
void Show();
};
Student::Student(int score[],int n)
{
if (n>N) throw "Parameter illegal!";
for (int i=0;i<n;i++)
scores[i]=&score[i];
lenght=n;
}
void Student::Insert(int i,int *x)
{
if (lenght>N) throw "Top full";
if (i<0||i>lenght+1) throw "Position illegal";
for (int j=lenght;j>=i;j--)
scores[j]=scores[j-1];
scores[i-1]=x;
lenght++;
}
int Student::Delete(int i)
{
if (lenght<0) throw "Down full";
if (i<0||i>lenght+1) throw "Position illegal";
int x=*scores[i-1];
for (int j=i-1;j<lenght-1;j++)
scores[j]=scores[j+1];
lenght--;
return x;
}
int Student::Get(int i)
{
return *scores[i-1];
}
int Student::Locate(int x)
{
int n;
for (int i=0;i<lenght;i++)
if (*scores[i]==x)
n=i+1;
return n;
}
void Student::Show()
{
for (int i=0;i<lenght;i++)
cout<<*scores[i]<<' ';
cout<<endl;
}
int main ()
{
int a[3]={91,75,68};
Student S(a,3);
cout<<"Original excel:"<<endl;
S.Show();
cout<<"Insert 99 after the first one:"<<endl;
int x=99;
S.Insert(1,&x);
cout<<"After 'Insert' operation:"<<endl;
S.Show();
cout<<"Delete the second one:"<<endl;
S.Delete(2);
cout<<"After 'Delete' operation"<<endl;
S.Show();
cout<<"Search 75"<<S.Search(75)<<','<<"Search the second one"<<S.Get(2)<<endl;
return 0;
}