//single_chain_table.cpp
#include<iostream>
#include<string>
using namespace std;
struct Node
{
double score;
Node *next;
};
class Single_chain_table
{
public:
Single_chain_table() { now=first.next = NULL; count = 0; }
void Set();
void Delete();
void Seek();
void Print();
void Insert();
private:
Node first;
Node *now;
int count;
};
int main()
{
Single_chain_table 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;
}
void Single_chain_table::Set()
{
cout << "enter a score:";
Node *s = new Node;
cin >> s->score;
if (count == 0)
{
first.next = now = s;
s->next = NULL;
}
else
{
now->next = s;
now = s;
s->next = NULL;
}
count++;
}
void Single_chain_table::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 = p->next->next;
delete temp;
cout << "Destroied.";
count--;
break;
}
p = p->next;
}
if (p->next == NULL) cout << "no match was found.";
}
void Single_chain_table::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 Single_chain_table::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 Single_chain_table::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->score = insert_score;
s->next = p->next;
p->next = s;
}
信管117118李志荣数据结构实验三---单链表实现
最新推荐文章于 2021-01-13 17:36:29 发布