//head.h
#include<iostream>
#include"head.h"
using namespace std;
int main()
{
Double_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 Double_linked_list::Set()
{
cout << "enter a score:";
Node *s = new Node;
cin >> s->score;
if (count == 0)
{
first.next = now = s;
s->next = NULL;
s->prior = NULL;
}
else
{
s->prior = now;
now->next = s;
now = s;
s->next = NULL;
}
count++;
}
void Double_linked_list::Delete()
{
if (first.next == NULL) throw "no score";
cout << "enter the score you want to delete:";
int delete_score;
cin >> delete_score;
Node *temp, *p = &first;
while (p->next != NULL)
{
if (delete_score == p->next->score)
{
temp = p->next;
p->next->next->prior = p;
p->next = p->next->next;
delete temp;
cout << "Destroied.";
count--;
break;
}
p = p->next;
}
if (p->next == NULL) cout << "no match was found.";
}
void Double_linked_list::Seek()
{
if (first.next == NULL) throw "no score";
cout << "enter the location you want to find";
int location;
cin >> location;
if (location > count) throw "wrong location";
Node *p = first.next;
for (int i = 1; i<location; i++)
{
p = p->next;
}
cout << "the score is:" << p->score << endl;
}
void Double_linked_list::Print()
{
if (first.next == NULL) throw "no score";
Node *p = first.next;
cout << "Printing all score:" << p->score;
while (p->next != NULL)
{
p = p->next;
cout << " " << p->score;
}
}
void Double_linked_list::Insert()
{
if (first.next == NULL) throw "no score";
cout << "enter the location you want to insert";
int location;
cin >> location;
if (location > count) throw "wrong location";
cout << "enter the score you want to insert :";
int insert_score;
cin >> insert_score;
Node *p = first.next;
for (int i = 1; i<location - 1; i++)
{
p = p->next;
}
Node *s = new Node;
s->prior = p;
s->score = insert_score;
s->next = p->next;
p->next = s;
}
struct Node{double score;Node *next;Node *prior;};class Double_linked_list{public:Double_linked_list() { now = first.next = NULL; count = 0; }void Set();void Delete();void Seek();void Print();void Insert();private:Node first;Node *now;int count;};