c++类的作业实例(单链表)
这两周的c++课都没怎么听,也许是对自己太自信了,第一次的类的作业竟然不会做(丢人),翻看了书本,加上论坛大佬的代码才大概弄明白;然而这次的上机作业,要求再用类写个链表系统,做的时候有发现问题了,虽然之前总结过链表,但是是用结构体写的,貌似不支持string(基于类的链表支持string),这次又浏览了论坛上大佬的帖子,总结了一波,做出了以下单链表系统(阉割版)
#include<iostream>
#include<string>
using namespace std;
typedef struct student {
string name, ID, publisher, author;
student* next;
} student, * stu;
class list {
public:
~list();//析构函数
list(string, string, string, string);//构造函数
void search(string name);
void insert(string, string, string, string);
void del(string);
void print();
void edit(string);
struct student* head;
};
list::~list()
{
cout << "release successfully" << endl;
system("pause");
}
//析构函数不要出现在主函数里,不然会报错
list::list(string a, string b, string c, string d)
{
head = new student;
head->next = NULL;
student* s = NULL;
s = new student;
s->name = a;
s->ID = b;
s->author = c;
s->publisher = d;
s->next = head->next;
head->next = s;
}
//初始化链表
void list::search(string name)
{
student* p = head->next;
while (p != NULL) {
if (p->name == name) {
//return p;
cout << "name: " << p->name << endl;
cout << "ID: " << p->ID << endl;
cout << "author: " << p->author << endl;
cout << "publisher: " << p->publisher << endl;
break;
}
p = p->next;
}
system("pause");
//return NULL;
}
//查找链表中的成员
void list::insert(string a, string b, string c, string d)
{
student* p = head, * s = NULL;
s = new student;
s->name = a;
s->ID = b;
s->author = c;
s->publisher = d;
s->next = p->next;
head->next = s;
}
//往链表添加新成员
void list::del(string name)
{
student* p = head->next, * s = head, * q = NULL;
q = new student;
while (p != NULL && p->name != name) {
p = p->next;
s = s->next;
}
if (p->name == name) {
s->next = p->next;
q = p;
delete q;
cout << "delete successfully" << endl;
}
}
//删除链表的结点
void list::print()
{
student* p = head->next;
cout << "name\t" << "ID\t" << "author\t" << "publisher" << endl;
while (p != NULL) {
cout << p->name << '\t' << p->ID << '\t' << p->author << '\t' << p->publisher << endl;
p = p->next;
}
cout << endl;
}
//打印链表
void list::edit(string name)
{
student* p = head->next;
while (p != NULL) {
if (p->name == name) {
break;
}
p = p->next;
}
while (1) {
cout << "what data do you wanna edit(enter 'none' to exit): ";
string temp;
cin >> temp;
cout << "enter the new data: ";
if (temp == name) {
cin >> p->name;
}
else if (temp == "ID") {
cin >> p->ID;
}
else if (temp == "author") {
cin >> p->author;
}
else if (temp == "publisher") {
cin >> p->publisher;
}
else if (temp == "none") {
break;
}
else {
cout << "error, please check what you enter" << endl;
}
}
}
//编辑链表成员的数据
int main()
{
list li("a", "b", "c", "d");
while (1) {
cout << "---------------------------\n";
cout << "1.add the info \n";
cout << "2.search the info \n";
cout << "3.edit the info \n";
cout << "4.delete the info \n";
cout << "5.print the list \n";
cout << "0.exit \n";
cout << "---------------------------\n";
cout << "choose a mode: ";
int mode;
cin >> mode;
if (mode == 1) {
string a, b, c, d;
cout << "enter the new data(name ID author publisher): ";
cin >> a >> b >> c >> d;
li.insert(a, b, c, d);
}
else if (mode == 2) {
string temp;
cout << "enter the book's name that you want to search: ";
cin >> temp;
li.search(temp);
}
else if (mode == 3) {
string temp;
cin >> temp;
li.edit(temp);
}
else if (mode == 4) {
string temp;
cin >> temp;
li.del(temp);
system("pause");
}
else if (mode == 5) {
li.print();
system("pause");
}
else if (mode == 0) {
cout << endl;
system("pause");
break;
}
else {
cout << "error, please select the mode again\n";
system("pause");
}
system("cls");
}
}