1. 程式人生 > >實驗三:線性表綜合應用

實驗三:線性表綜合應用

一.實驗目的
鞏固線性表的資料結構的儲存方法和相關操作,學會針對具體應用,使用線性表的相

關知識來解決具體問題。

二.實驗內容
1.建立一個由n 個學生成績的順序表,n的大小由自己確定,每一個學生的成績資訊由自己
確定,實現資料的對錶進行插入、刪除、查詢等操作。分別輸出結果。
1) 用順序表來實現。
2) 用單鏈表來實現。
3) 用雙鏈表實現。
4) 用靜態連結串列實現。

5) 用間接定址實現。

1)順序表:

#include<iostream>
using namespace std;
const int MaxSize=100;
class Student
{
	int data[MaxSize];//建立陣列
    int length;//線性表長度
  public:
      Student(){length=0;} //建立空表
      Student(int a[],int n);  //建立長度為n順序表
      ~Student(){}//解構函式
      int Get(int i);//按位查詢,第i個
      void Input (int i,int x);//插入
      int Delete(int i);//刪除
      void Print();//輸出
 };


Student::Student(int a[],int n)      
{   
	
int i;   if(n>MaxSize)throw"引數非法";   for(i=0;i<n;i++)      data[i]=a[i];   length=n; } int  Student::Get(int i)  {   if(i<1&&i>length)   throw"查詢位置非法";    return data[i-1]; } void Student::Input(int i,int x)      //插入 {   int j;   if(length>=MaxSize)throw"上溢";   if(i<1||i>length+1)throw"位置";   for(j=length;j>=i;j--)     data[j]=data[j-1];    data[i-1]=x;    length++; } int Student::Delete(int i)            //刪除 {   int x,j;   if(length==0)throw"下溢";   if(i<1||i>length)throw"位置";   x=data[i-1];   for(j=i;j<length;j++)     data[j-1]=data[j];   length--;   return x; } void Student::Print()                  //輸出 {     int i;   for(i=1;i<length;i++)   cout<<"第"<<i<<"位學生成績:"<<data[i-1]<<endl;   cout<<endl; } void main() { int a[5]={50,60,70,80,90}; Student L(a,5); cout<<"錄入學生資訊:"<<endl; L.Print(); cout<<"在第2個位置插入85"<<endl; L.Input(2,85); cout<<"插入後學生成績為:"<<endl; L.Print(); cout<<"第三位學生成績為:"<<endl; cout<<L.Get(3)<<endl; cout<<"刪除第一個學生成績"<<endl; L.Delete(1); cout<<"刪除後學生成績為:"<<endl; L.Print(); }

2)單鏈表

#include <iostream>  
using namespace std;  
struct Std{  
    int data;  
    Std *next;  
}*p,*q;  
class Student
{  
private:  
    Std *first;  
public:  
    Student();  
    Student(int a[],int n);  
    ~Student();
	int Get(int i); 
	int Locate(int x);  
    void Insert(int i,int x);  
    int Delete(int i);  
    void Print();  
};  

Student::Student()//無參構造
{  
    first =new Std;  
    first->next=NULL;  
}  

Student::Student(int a[],int n)//初始化
{  
    first=new Std;  
    first->next=NULL;  
    for (int i=0;i<n;i++)
	{  
        p=new Std;  
        p->data=a[i];  
        p->next=first->next;  
        first->next=p;
    }  
}  

Student::~Student()//解構函式
{
	while(first!=NULL)
	{
		p=first;
		first=first->next;
		delete p;
	}
}

int Student::Get(int i) //按位查詢
{  
	p=first->next;
	int count=1;
    while (p != NULL && count<i) 
	{  
        p = p->next;  
        count++;  
    }  
    if (p == NULL) throw "位置非法";  
    else   
    return p->data;  
}  

int Student::Locate(int x)//按值查詢
{  
    p=first->next; 
	int count=1;
    while (p!=NULL)
	{  
        if(p->data==x)
			return count;
		p=p->next;
		count++;
    }
	return 0;
}
   
void Student::Insert(int i,int x)//插入
{
	p=first;
	int count=0;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL)throw"位置";
	else
	{
		q=new Std;
		q->data=x;
		q->next=p->next;
		p->next=q;
	}
}


int Student::Delete(int i)//刪除
{  
    p=first;  
    int count=0;  
    while (p!=NULL&&count<i-1)
	{  
        p=p->next;  
        count++;  
    }  
    if (p==NULL||p->next==NULL) throw "位置非法";  
    else 
	{  
		int x;
        q=p->next;
		x=q->data;
        p->next=q->next;  
        delete q;  
		return x;
    }  
}  


void Student::Print() {  
    p = first->next;  
    while (p!= NULL) 
	{  
          cout << p->data<< ' '; 
		  p= p->next; 
    }  
    cout<<endl;  
}  
  
void main()
{
int a[5]={50,60,70,80,90};
Student L(a,5);
cout<<"錄入學生資訊:"<<endl;
L.Print();
cout<<"在第2個位置插入85"<<endl;
L.Insert(2,85);
cout<<"插入後學生成績為:"<<endl;
L.Print();
cout<<"第三位學生成績為:"<<endl;
cout<<L.Get(3)<<endl;
cout<<"刪除第一個學生成績"<<endl;
L.Delete(1);
cout<<"刪除後學生成績為:"<<endl;
L.Print();
}

3)雙鏈表

#include <iostream>  
using namespace std;  
struct Std
{  
    int data;  
    Std *next; 
	Std *prior;
}*p,*q;  
class Student
{  
private:  
    Std *first;  
public:  
    Student();  //無參構造
    Student(int a[],int n);  //初始化
    ~Student(); //解構函式
	int Get(int i); //按位查詢
	int Locate(int x);  //按值查詢
	int Before(int x); //前元素查詢
    void Insert(int i,int x);  //插入
    void Delete(int i);  //刪除
    void Print();  //輸出
};  

Student::Student()//無參構造
{  
    first =new Std;  
    first->next=NULL; 
	first->prior=NULL;
}  

Student::Student(int a[],int n)//初始化
{  
    first=new Std;  
    first->next=NULL;
	first->prior=NULL;
	q=first;
    for (int i=0;i<n;i++)
	{  
        p=new Std;  
        p->data=a[i]; 
		p->prior=q;
		p->next=q->next;
        q->next=p;
		q=p;
    }  
}  

Student::~Student(){}//解構函式

int Student::Get(int x) //按位查詢
{  
	p=first->next;
	int count=1;
    while (p != NULL && count<x) 
	{  
        p = p->next;  
        count++;  
    }  
    if (p == NULL) throw "位置非法";  
    else   
    return p->data;  
}  

int Student::Locate(int x)//按值查詢
{  
    p=first->next; 
	int count=1;
    while (p!=NULL)
	{  
        if(p->data==x)
			return count;
		p=p->next;
		count++;
    }
	return 0;
}
   
int Student::Before(int x) //前元素查詢
{  
	for(p=first->next;p!=NULL&&p->data!=x;)
	{  
		p=p->next;
    }
    if (p==NULL) 
		throw "位置非法";  
    else return p->prior->data;  
	return 0;
}  

void Student::Insert(int i,int x)//插入
{
	p=first;
	int count=0;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL)throw"位置";
	else
	{
		q=new Std;
		q->data=x;
		q->prior=p;
		q->next=p->next;
		p->next->prior=q;
		p->next=q;
	}
}


void Student::Delete(int i)//刪除
{  
    p=first->next;  
    int count=0;  
    while (p!=NULL&&count<i-1)
	{  
        p=p->next;  
        count++;  
    }  
    if (p==NULL) throw "位置非法";  
    else 
	{  
        (p->prior)->next=p->next;	
        (p->next)->prior=p->prior;  
        delete p;  	
    }  
}  


void Student::Print() //輸出
{  
    p = first->next;  
    while (p!= NULL) 
	{  
          cout << p->data<< ' '; 
		  p= p->next; 
    }  
    cout<<endl;  
}  
  
void main()
{
int a[5]={50,60,70,80,90};
Student L(a,5);
cout<<"錄入學生資訊:"<<endl;
L.Print();
cout<<"在第2個位置插入85"<<endl;
L.Insert(2,85);
cout<<"插入後學生成績為:"<<endl;
L.Print();
cout<<"85的前一個學生成績為:"<<endl;
cout<<L.Before(85)<<endl;
cout<<"第三位學生成績為:"<<endl;
cout<<L.Get(3)<<endl;
cout<<"刪除第一個學生成績"<<endl;
L.Delete(1);
cout<<"刪除後學生成績為:"<<endl;
L.Print();
}

4)靜態連結串列

#include <iostream>
using namespace std;
const int N=100;
struct Std
{
	int data;
	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].data=a[i];
		score[s].next=avail;
	}
	lenght=n;
}

void Student::Insert(int i,int x)
{
	if (i>lenght) throw "位置非法";
	int s;
	s=avail;
	avail=score[avail].next;
	score[s].data=x;
	score[s].next=score[i-1].next;
	score[i-1].next=s;
	lenght++;
}

int Student::Delete(int i)
{
	if (i>lenght) throw "位置非法";
	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].data;
}
int Student::Get(int i) 
{
	if (i>lenght) throw "位置非法";
	int r=first;
	while (r<i)
	r=score[r].next;
	return score[r].data;
}
int Student::Locate(int x) 
{
	int r=first;
	while (score[r].data!=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].data<<' ';
	}
	cout<<endl;
}

void main()
{
int a[5]={50,60,70,80,90};
Student L(a,5);
cout<<"錄入學生資訊:"<<endl;
L.Show();
cout<<"在第2個位置插入85"<<endl;
L.Insert(2,85);
cout<<"插入後學生成績為:"<<endl;
L.Show();
cout<<"第三位學生成績為:"<<endl;
cout<<L.Get(3)<<endl;
cout<<"刪除第一個學生成績"<<endl;
L.Delete(1);
cout<<"刪除後學生成績為:"<<endl;
L.Show();
}

5)間接定址

#include <iostream>
using namespace std;
const int N=100;

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 "引數非法";
	for (int i=0;i<n;i++)
		scores[i]=&score[i];
	lenght=n;
}
void Student::Insert(int i,int *x)
{
	if (lenght>N) throw "上溢";
	if (i<0||i>lenght+1) throw "位置非法";
	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 "下溢";
	if (i<0||i>lenght+1) throw "位置非法";
	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;
}


void main()
{
int a[5]={50,60,70,80,90};
Student L(a,5);
cout<<"錄入學生資訊:"<<endl;
L.Show();
cout<<"在第2個位置插入85"<<endl;
int x=85;
L.Insert(2,&x);
cout<<"插入後學生成績為:"<<endl;
L.Show();
cout<<"第三位學生成績為:"<<endl;
cout<<L.Get(3)<<endl;
cout<<"刪除第一個學生成績"<<endl;
L.Delete(1);
cout<<"刪除後學生成績為:"<<endl;
L.Show();
}