链表的应用
C++上周三试毕,题很简单,轻松拿下。最近一直在看Apple Developer上的文档,脑子里尽是ObjC。项目设计第一个题是链表,当初就没学好,跟着网上的tutorials给写出来了。这两天在看《三国演义》,网上订了《史记》和《聊斋志异》,一直都很喜欢读文言文,觉得古代小说很有意思,语言非常精炼。也是因为天天对着满屏幕的英文,有点枯燥了,遂寻汉文而读。
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
struct Buddy {
string name;
string sex;
string age;
string score;
struct Buddy *next;
};
int n=0;
// creat a new list START
struct Buddy *creat() {
struct Buddy *head;
struct Buddy *p1,*p2;
n = 0; // reset the amounst of members
p1 = p2 = (struct Buddy *)new struct Buddy;
cout << setiosflags(ios::left) << setw(15) << "Name" ;
cout << setw(10) << "Sex" << setw(10) << "Age" << setw(10) << "Score" << endl;
cin >> p1->name >> p1->sex >> p1->age >> p1->score;
head=NULL; // reset the list whenever creat a new list
while (p1->name != "0") {
n++;
if (n == 1) {
head = p1;
}
else {
p2->next = p1;
}
p2 = p1;
p1 = (struct Buddy *)new struct Buddy;
cin >> p1->name >> p1->sex >> p1->age >> p1->score;
}
p2->next = NULL;
return head;
}
// creat a new list END
// print the list START
void print(struct Buddy *head) {
struct Buddy *p;
p = head;
if (head != NULL) {
cout << '\n' << setiosflags(ios::left) << setw(15) << "Name" ;
cout << setw(10) << "Sex" << setw(10) << "Age" << setw(10) << "Score" << endl;
do {
cout << setiosflags(ios::left) << setw(15) << p->name
<< setw(10) << p->sex << setw(10) << p->age << setw(10) << p->score << endl;
p = p->next;
} while(p != NULL);
}
else {
cout << "Error: List is null." << endl;
}
}
// print the list END
// delete one member START
struct Buddy *del(struct Buddy *head, string checkingName) {
struct Buddy *p1,*p2;
if (head != NULL) {
p1 = head;
while (checkingName!=p1->name && p1->next!=NULL) {
p2 = p1;
p1 = p1->next;
}
if (checkingName == p1->name) {
if (p1 == head) {
head = p1->next;
}
else {
p2->next = p1->next;
}
cout << checkingName <<" has been deleted."<< endl;
n--;
}
else {
cout << checkingName <<" is not found." << endl;
}
}
else {
cout << "Error: List is null." << endl;
}
return head;
}
// delete one member END
// insert a member START
struct Buddy *insert(struct Buddy *head, struct Buddy *toInsert) {
struct Buddy *p0,*p1,*p2;
p0 = toInsert;
p1 = head;
if (head == NULL) {
head = p0;
p0->next = NULL;
}
else {
while ((p0->name > p1->name)&&(p1->next!=NULL)) {
p2 = p1;
p1 = p1->next;
}
}
if (p0->name <= p1->name) {
if (head == p1) {
head = p0;
}
else {
p2->next = p0;
}
p0->next = p1;
}
else {
p1->next = p0;
p0->next = NULL;
}
n++;
return head;
}
// insert a member END
// menu START
void menu() {
cout << endl;
cout << "*----------------------------------------*" << endl;
cout << "| |" << endl;
cout << "| (C) Creat a new list. |" << endl;
cout << "| (P) Print the list. |" << endl;
cout << "| (D) Delete one person. |" << endl;
cout << "| (I) Insert a new person. |" << endl;
cout << "| (E) Exit. |" << endl;
cout << "| |" << endl;
cout << "| End with four zeros: |" << endl;
cout << "| Just like 0 0 0 0 |" << endl;
cout << "| |" << endl;
cout << "*----------------------------------------*" << endl;
cout << "Enter: ";
}
// menu END
int main(int argc, const char * argv[])
{
struct Buddy *myBuddy = NULL;
struct Buddy *toInsert;
string checkingName;
char c;
menu();
inputTheC:
cin >> c;
c=toupper(c);
if (c=='C'||c=='P'||c=='D'||c=='I'||c=='E') {
if (c!='E') {
while (c!='E') {
switch (c) {
case 'C':
myBuddy=creat();
break;
case 'P':
print(myBuddy);
break;
case 'D':
cout << "Enter the name you want to delete: ";
cin >> checkingName;
myBuddy = del(myBuddy, checkingName);
break;
case 'I':
cout << '\n' << "Enter the information you want to insert:" << '\n' << endl;
toInsert = creat();
myBuddy = insert(myBuddy, toInsert);
break;
default:
break;
}
menu();
cin >> c;
c = toupper(c);
}
}
else {
cout << "Exited." << endl;
exit(1);
}
}
else {
cout << "Enter the correct letter." << endl;
goto inputTheC;
}
return 0;
}
评论