//head.h const int Maxsize = 5; struct Node { double score; int next; }; class Static_linked_list { public: Static_linked_list() { for (int i = 0;i<Maxsize-1;i++) { data[i].next = i +1; } data[Maxsize - 1].next = -1; count=avail = 0; rear = first = -1; } void Set(); void Delete(); void Seek(); void Print(); void Insert(); private: int first; int rear; int avail; int count; Node data[Maxsize]; };
#include<iostream> #include"head.h" using namespace std; int main() { Static_linked_list Student; cout << "MENU"; int use = 0; while (use != 6) { cout << "\n1.SET\n2.Delete\n3.Seek\n4.Print\n5.Insert\n6.Exit\nNow input a number to use function:"; cin >> use; switch (use) { case 1:Student.Set(); break; case 2:Student.Delete(); break; case 3:Student.Seek(); break; case 4:Student.Print(); break; case 5:Student.Insert(); break; default: break; } } return 0; }
#include<iostream> #include"head.h" using namespace std; void Static_linked_list::Set() { if (count==Maxsize) throw "overflow"; cout << "enter a score: "; int new_score; cin >> new_score; if (count == 0) { rear = first = avail; avail = data[avail].next; data[first].score = new_score; data[first].next = 1; count++; } else { int p=data[avail].next; data[avail].score = new_score; data[rear].next = avail; rear = avail; data[rear].next = -1; avail = p; count++; } } void Static_linked_list::Delete() { if (count==0) throw "underflow"; cout << "enter the location of preorder node of the data you want to delete(if you want to delete the first data ,enter 0):"; int p; cin >> p; if (p == first&&rear!=first) first = p + 1; else first = -1; if (p == rear) rear = p - 1; int q; data[p - 1].next = data[p].next; q = avail; avail = p; data[avail].next = q; count--; } void Static_linked_list::Seek() { if (count == 0) throw"no data"; cout << "enter the location you want to find:"; int location; cin >> location; if (location > Maxsize) throw "overflow"; int i = first; for(;data[i].next!=-1;i=data[i].next) { if(i+1==location) cout << "the score: " << data[location-1].score; } if (i + 1 == location) cout << "the score: " << data[location - 1].score; } void Static_linked_list::Print() { if (count == 0) throw"no data"; cout << "all data are:"; int i = first; for(;data[i].next!=-1;i++) { cout << data[i].score << " "; } cout << data[i].score<<endl; } void Static_linked_list::Insert() { if (count == 0) throw "no data"; cout << "enter a score"; double new_score; cin >> data[avail].score; data[rear].next = avail; rear = avail; avail = data[avail].next; data[rear].next = -1; count++; }