实验三双链表

#include <iostream>  
using namespace std;  
const int N = 5;  
struct Std {  
    int date;  
    Std *next,*prior;  
}*p, *r;  
class Student {  
private:  
    Std * first;  
    int length;  
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<=length && j<i - 1) {  
        r = r->next;  
        count++;  
        j++;  
    }  
    if (count>length) throw "位置非法";  
    else {  
        p = new Std;  
        p->next = r->next;  
        r->next->prior = p;  
        r->next = p;  
        p->date = x;  
        p->prior = r;  
        length++;  
    }  
}  
int Student::Delete(int i) {  
    r = first;  
    int x,j = 0,count = 0;  
    while (count<=length && j<i - 1) {  
        r = r->next;  
        count++;  
        j++;  
    }  
    if (count>length) 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<=length && j<i - 1) {  
        r = r->next;  
        count++;  
        j++;  
    }  
    if (count>length) throw "位置非法";  
    else  
        p = r->next;  
    return p->date;  
}  
int Student::Locate(int x) {  
    r = first;  
    int j = 0,count = 0;  
    while (count<=length) {  
        r = r->next;  
        count++;  
        j++;  
        if (r->date == x) break;  
    }  
    if (count>length) 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<=length) {  
        r = r->next;  
        count++;  
        if (r->date == x) break;  
    }  
    if (count>length) throw "找不到该数据";  
    if (i>length) 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;  
}  

猜你喜欢

转载自blog.csdn.net/weixin_41937706/article/details/80266184