資料結構實驗三:線性表綜合實驗
一.實驗目的
鞏固線性表的資料結構的儲存方法和相關操作,學會針對具體應用,使用線性表的相
1.建立一個由 n 個學生成績的順序表,n 的大小由自己確定,每一個學生的成績資訊由自己
確定,實現資料的對錶進行插入、刪除、查詢等操作。分別輸出結果。
要求如下:
1)用順序表來實現
2)用單連結串列來實現
3)用雙連結串列實現
4)用靜態連結串列實現
鞏固線性表的資料結構的儲存方法和相關操作,學會針對具體應用,使用線性表的相
關知識來解決具體問題。
二.實驗內容1.建立一個由 n 個學生成績的順序表,n 的大小由自己確定,每一個學生的成績資訊由自己
確定,實現資料的對錶進行插入、刪除、查詢等操作。分別輸出結果。
要求如下:
1)用順序表來實現
2)用單連結串列來實現
3)用雙連結串列實現
4)用靜態連結串列實現
5)用間接定址實現
實驗程式碼如下:
1)用順序表來實現
#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 "引數非法";
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;
}
int main (){
int a[3]={90,80,75};
Student S(a,3);
cout<<"原表:"<<endl;
S.Show();
cout<<"在第3位插入98:"<<endl;
S.Insert(3,98);
cout<<"插入後:"<<endl;
S.Show();
cout<<"刪除第2位:"<<endl;
S.Delete(2);
cout<<"刪除後:"<<endl;
S.Show();
cout<<"查75分的位置:"<<S.Locate(75)<<','<<"查第2位的成績:"<<S.Get(2)<<endl;
return 0;
}
2)用單連結串列來實現
#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 "位置非法";
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 "位置非法";
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 "位置非法";
else
p = r;
return p->date;
}
int Student::Locate(int x) {
r = first;
int j=0;
while (r != NULL) {
r = r->next;
j++;
if (r->date == x) break;
}
if (r == NULL) throw "位置非法";
return j;
}
void Student::Show() {
r = first;
while (r->next!= NULL) {
r = r->next;
cout << r->date << ' ';
}
cout<<endl;
}
int main() {
int a[3] = { 90,80,75 };
Student S(a, 3);
cout << "原表:" << endl;
S.Show();
cout << "在第3位插入98:" << endl;
S.Insert(3, 98);
cout << "插入後:" << endl;
S.Show();
cout << "刪除第2位:" << endl;
S.Delete(2);
cout << "刪除後:" << endl;
S.Show();
cout << "查75分的位置:" << S.Locate(75) << ',' << "查第2位的成績:" << S.Get(2) << endl;
return 0;
}
3)用雙連結串列實現
#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 "位置非法";
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 "位置非法";
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 "位置非法";
else
p = r->next;
return p->date;
}
int Student::Locate(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<<"找不到該資料"<<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 "前移非法";
for (j=0;j<i;j++)
r=r->prior;
return r->date;
}
int main() {
int a[3] = { 90,80,75 };
try{
Student S(a, 3);
cout << "原表:" << endl;
S.Show();
cout << "在第3位插入98:" << endl;
S.Insert(1, 98);
cout << "插入後:" << endl;
S.Show();
cout << "刪除第2位:" << endl;
S.Delete(2);
cout << "刪除後:" << endl;
S.Show();
cout << "查75分的位置:" << endl;
cout<<S.Locate(75) <<endl;
cout<< "查第2位的成績:" <<endl;
cout<< S.Get(2) << endl;
cout<< "查75分前2位的成績:" <<endl;
cout<< S.Getper(75,2) << endl;
}
catch(char *s){
cout<<s<<endl;
}
return 0;
}
4)用靜態連結串列實現
#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 "位置非法";
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 "位置非法";
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 "位置非法";
int r=first;
while (r<i)
r=score[r].next;
return score[r].date;
}
int Student::Locate(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] = { 90,80,75 };
Student S(a, 3);
cout << "原表:" << endl;
S.Show();
cout << "在第3位插入98:" << endl;
S.Insert(3, 98);
cout << "插入後:" << endl;
S.Show();
cout << "刪除第2位:" << endl;
S.Delete(2);
cout << "刪除後:" << endl;
S.Show();
cout << "查75分的位置:" << S.Locate(75) << ',' << "查第2位的成績:" << S.Get(2) << endl;
return 0;
}
5)用間接定址實現
#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 "引數非法";
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;
}
int main (){
int a[3]={90,80,75};
Student S(a,3);
cout<<"原表:"<<endl;
S.Show();
cout<<"在第3位插入98:"<<endl;
int x=98;
S.Insert(3,&x);
cout<<"插入後:"<<endl;
S.Show();
cout<<"刪除第2位:"<<endl;
S.Delete(2);
cout<<"刪除後:"<<endl;
S.Show();
cout<<"查75分的位置:"<<S.Locate(75)<<','<<"查第2位的成績:"<<S.Get(2)<<endl;
return 0;
}
截圖:
1)2)4)5)
3)雙連結串列
相關文章
- 資料結構實驗之查詢七:線性之雜湊表資料結構
- 綜合實驗
- 資料結構實驗三 2024_樹與圖實驗資料結構
- OSPF綜合實驗
- BGP綜合實驗
- OSPF 綜合實驗
- 資料結構實驗1資料結構
- 資料結構實驗(4)資料結構
- 資料結構,雜湊表hash設計實驗資料結構
- 資料結構與演算法實驗1——線性表的應用之棧與佇列資料結構演算法佇列
- 資料結構:線性表(Python實現基本操作)資料結構Python
- 靜態路由綜合實驗路由
- 資料結構實驗課五-1資料結構
- [資料結構] - 線性表資料結構
- 資料結構——線性表資料結構
- 資料結構 | 線性表資料結構
- 資料結構-線性表資料結構
- 資料結構—線性表資料結構
- 綜合實驗,策略路由(BFD,NAT)路由
- 靜態路由及綜合實驗路由
- WPF 資料繫結之ValidationRule資料校驗綜合Demo
- Java實現資料結構之線性結構Java資料結構
- 資料結構和演算法(一)線性表實現資料結構演算法
- 資料結構實驗之連結串列三:連結串列的逆置資料結構
- 資料結構實驗:連結串列的應用資料結構
- Linux備份任務綜合實驗Linux
- 資料結構 - 線性表 - 順序表資料結構
- 資料結構實驗 多維陣列的實現資料結構陣列
- 資料結構實驗 二維矩陣的實現資料結構矩陣
- 資料結構-線性表、連結串列資料結構
- 資料結構:線性表-例題資料結構
- 線性表__資料結構筆記資料結構筆記
- 南郵資料結構實驗1.1:順序表的相關操作資料結構
- 實驗2.2 線性表的應用:遊戲遊戲
- 資料結構實驗之連結串列八:Farey序列資料結構
- 考研資料結構-線性表-順序表資料結構
- 010.OpenShift綜合實驗及應用
- 【資料結構】線性表-單連結串列資料結構