I reviewed the singly linked list in the past two days, wrote some basic operations of the singly linked list myself, and made up for some of the different knowledge in the previous class.
This time I divided the address book into three files, and the basic operations of the linked list are encapsulated with functions, so the code looks more clear.
I encountered a small problem when writing the selection operation function. At first, my options were of type int, but as long as I input non-int type, the program would crash. Finally, I replaced the options with char type, which is perfect solved this problem. Although this code is still a little buggy, it can still be used for your reference.
Here is the complete code for the address book:
address.h
#ifndef AdDDRESS_H_HH #define AdDDRESS_H_HH #include <iostream> #include <cstring> #include <cstdlib> using namespace std; struct Node { char name[15]; char phone[20]; char address[30]; struct Node * next; }; extern struct Node* createlist(); extern struct Node* createnode(struct Node info); extern bool insertinfo(struct Node* list, struct Node info); extern void printinfo(struct Node* list); extern void menu(); extern char choose(); extern void work(struct Node* list); extern bool insertoperation(struct Node* list); extern bool deleteinfo(struct Node* list, char *name); extern bool deleteoperation(struct Node* list); extern struct Node searchinfo(struct Node* list, char *name); extern bool searchoperation(struct Node* list); extern void returnmenu(); extern void clearscreen(); #endif
addressfunc.cpp
#include "address.h" //create a linked list struct Node* createlist() { struct Node *list = new struct Node; list->next = NULL; return list; } //create a node struct Node* createnode(struct Node info) { struct Node* newnode = new struct Node; newnode->next = NULL; strcpy(newnode->name, info.name); strcpy(newnode->phone, info.phone); strcpy(newnode->address, info.address); // cout << "Create node debug" << endl; // cout << newnode->name << endl; return newnode; } //Insert a node at the end bool insertinfo(struct Node* list, struct Node info) { struct Node* newnode = createnode(info); if(newnode->name == NULL) return false; struct Node* temp = list; while (temp->next != NULL) { temp = temp->next; } newnode->next = NULL; temp->next = newnode; // cout << "Insert Node Debug" << endl; // cout << newnode->name << endl; return true; } //delete contact bool deleteinfo(struct Node* list, char *name) { struct Node* temp = list; struct Node* p = list->next; if(p == NULL) { cout << "No contact to call" << name << "" << endl; return false; } while(strcmp(p->name, name) != 0) { if(p->next == NULL) { cout << "No contact to call" << name << "" << endl; return false; } p = p->next; temp = temp->next; } temp->next = p->next; delete p; return true; } //find contacts struct Node searchinfo(struct Node* list, char* name) { struct Node* temp = list; struct Node* p = list->next; struct Node a; if(p == NULL) { cout << "No contact to call" << name << "" << endl; strcpy(a.name, "nonono"); strcpy(a.phone, "nonono"); strcpy(a.address, "nonono"); return a; } while (strcmp(p->name, name)) { if(p->next == NULL) { cout << "No contact to call" << name << "" << endl; strcpy(a.name, "nonono"); strcpy(a.phone, "nonono"); strcpy(a.address, "nonono"); return a; } temp = temp->next; p = p->next; } cout << "Find Contacts Debug" << endl; cout << p->name << endl; return *p; } // print all information void printinfo(struct Node* list) { struct Node* temp = list->next; // cout << "Print all info debugging" << endl; // cout << temp->name << endl; if(temp == NULL) { cout << "No information" << endl; return; } cout << "The following is all the information" << endl; while(temp != NULL) { cout << "名字:" << temp->name << endl << "电话:" << temp->phone << endl << "地址:" << temp->address <<endl << endl; temp = temp->next; } } //Menu Bar void menu() { cout << " -------------------------------------" << endl; cout << " - address book-" << endl; cout << " - A. Deposit contact-" << endl; cout << "- B, delete contact-" << endl; cout << "- C, find contacts -" << endl; cout << " - D, print all information -" << endl; cout << " - E, exit address book -" << endl; cout << " -------------------------------------" << endl; } //choose char choose() { system("clear"); menu(); char a; cout << "Please enter your choice" << endl; cin >> a; //Convert lowercase letters to uppercase letters. if('a' == a) a = 'A'; else if('b' == a) a = 'B'; else if('c' == a) a = 'C'; else if('d' == a) a = 'D'; else if('e' == a) a = 'E'; while(a < 'A' || a > 'E') { cout << "You entered incorrectly, please try again" << endl; cin >> a; if('a' == a) a = 'A'; else if('b' == a) a = 'B'; else if('c' == a) a = 'C'; else if('d' == a) a = 'D'; else if('e' == a) a = 'E'; } return a; } //The specific operation of inserting information bool insertoperation(struct Node* list) { struct Node info; cout << "Please enter information" << endl; cout << "名字:"; cin >> info.name; cout << "Phone: "; cin >> info.phone; while(strlen(info.phone) != 11) { cout << "Please enter an 11-digit mobile phone number" << endl; cin >> info.phone; } cout << "地址:"; cin >> info.address; bool insertok = insertinfo(list, info); return insertok; } //The specific operation of deleting information bool deleteoperation(struct Node* list) { cout << "Please enter the name of the contact you want to delete" << endl; char name[15] = "0"; cin >> name; bool deleteok = deleteinfo(list, name); return deleteok; } // Find the specific operation of the contact bool searchoperation(struct Node* list) { cout << "Please enter the name of the contact you are looking for" << endl; char name[15] = "0"; cin >> name; struct Node searchresult = searchinfo(list, name); if(strcmp(searchresult.name, name) == 0) { cout << "名字:" << searchresult.name << endl << "电话:" << searchresult.phone << endl << "地址:" << searchresult.address <<endl << endl; return true; } else return false; } //The flow of the operation void work(struct Node* list) { char a = choose(); switch (a) { case 'A'://insert information { clearscreen(); int ret = insertoperation(list); if(ret == true) cout << "Information saved successfully" << endl; else cout << "Information failed to save" << endl; returnmenu(); }break; case 'B'://delete information { clearscreen(); int ret = deleteoperation(list); if(ret == true) cout << "Information deleted successfully" << endl; else cout << "Information deletion failed" << endl; returnmenu(); }break; case 'C'://find contacts { clearscreen(); int ret = searchoperation(list); if(ret == true) cout << "Find contacts successfully" << endl; else cout << "Failed to find contact" << endl; returnmenu(); }break; case 'D': // print all information { clearscreen(); printinfo(list); returnmenu(); } break; case 'E': exit(1); break; } } //return to menu bar void returnmenu() { cout << "Enter q to return to the menu bar" << endl; char a; cin >> a; while(a != 'q' && a != 'Q') { cout << "Incorrect input" << endl; cin >> a; } } //clear screen operation void clearscreen() { system("clear"); cout << endl << endl << endl ; }
addressmain.cpp
#include "address.h" intmain() { struct Node* list = createlist(); while(1) { work(list); } return 0; }