链表实现简易通讯录(debug练习)

    科技2022-07-12  121

    Debug Practise

    这是我最开始编写的一个小程序,目的是用来练习链表的存储结构,但是在执行的过程在发现很多处错误,于是就开始一个个的修改错误,我发现这个过程非常适合刚刚接触链表且对链表存储结构了解不是很透彻的同学,通过对这个程序的Debug来更透彻的了解链表的存储结构

    在当前这个阶段只实现了从文件.txt(cpp程序所在文件夹)读入数据、对数据合法性进行简单的判断,以及显示链表中的数据。这两个函数,其他函数暂时还没有做(因为最开始的目的就是练习存储结构)

    文件格式: 名字回车 电话回车

    #include <iostream> #include <cctype> #include <cstdlib> #include <fstream> #include <cstring> using namespace std; const int MaxSize = 50; //电话簿成员数组大小 const int Size = 20; //文件名数组大小 typedef struct{ //电话簿结构 char name[MaxSize]; char tel[MaxSize]; }book; typedef struct LNode{ //链表结构 book *data; struct LNode *next; }LNode,*LinkList; bool InitList(LinkList *L); bool GetList(LinkList *L,int *cont); bool InsertList(LinkList *L,int insloc); bool DeleteList(LinkList *L,int delloc); bool ChangeList(LinkList *L,int chaloc); bool SearchList(LinkList *L,int sealoc); void ShowList(LinkList L); void ShowMenu(); int LengthList(LinkList L); bool Checkname(char *); bool Checktel(char *); int main() { LinkList book; //创建链表存储结构 InitList(&book); //初始化链表 cout << "Initial List Success.\n"; ShowMenu(); //显示菜单 char oops; //用户输入控制变量 while(1){ cout.width(20); cout << "Enter a number that you need(# to quit): "; //用户操作界面 cin >> oops; switch(oops) { case '1': int cont; GetList(&book,&cont); cout << cont << " have readed.\n"; break; /* case '2': int insloc; cout.width(15); cout << "Enter insert locate: "; cin >> insloc; InsertList(&L,insloc); cout << "Insert Done.\n"; break; case '3': int delloc; cout << "Enter delete locate: "; cin >> delloc; DeleteList(&L,delloc); cout << "Delete Done.\n"; break; case '4': int chanloc; cout << "Enter change locate: "; cin >> chanloc; ChangeList(&L,chanloc); cout << "CHange Done.\n"; break; case '5': int searloc; cout << "Enter a search locate: "; cin >> searloc; SearchList(L,searloc); break; */ case '6': ShowList(book); cout << "This is your List.\n"; break; default : cout << "Done.\n"; return 0; } } return 0; } bool InitList(LinkList *L) { LNode *head; head = (LinkList)malloc(sizeof(LNode)); if(!head) { cerr << "Err1.\n"; } head->data = (book *)malloc(sizeof(book)); if(!head->data) { cerr << "Err3.\n"; exit(EXIT_FAILURE); } head->next = NULL; return true; } void ShowMenu() { cout << "Press 1 to Get Item From File.\n"; cout << "Press 2 to Insert.\n"; cout << "Press 3 to Delete.\n"; cout << "Press 4 to Change.\n"; cout << "Press 5 to Search.\n"; cout << "Press 6 to Show List.\n"; } bool GetList(LinkList *L,int *cont) { LNode *p; p = *L; char filename[Size]; //数据文件名 ifstream inFile; //文件流输入 cout << "Enter the data file name: "; getchar(); //吃掉回车 cin.getline(filename,Size); //输入文件名 inFile.open(filename); //打开文件 if(!inFile.is_open()) //如果打开文件失败 { cerr << "Could not open the file " << filename << endl; cerr << "Porgram terminating.\n"; exit(EXIT_FAILURE); } int count = 0; //正确输入数据个数 while(inFile.good() && !inFile.eof()){ //打开文件成功且文件没有结束 LNode *temp; temp = (LinkList)malloc(sizeof(LNode)); if(!temp) { cerr << "Err2.\n"; return false; } temp->data = (book *)malloc(sizeof(book)); if(!temp->data) { cerr << "Err4.\n"; return false; } inFile >> temp->data->name; //从文件读入名称 if(!Checkname(temp->data->name)) //名称非法 { delete(temp); //清除临时空间 continue; //进行下一次数据读入 } inFile >> temp->data->tel; //电话非法 if(!Checktel(temp->data->tel)) { delete(temp); //清除临时空间 continue; //进行下一次数据读入 } temp->next = p->next; // p->next = temp; // count++; //正确读入个数+1 } *cont = count; //带回正确输入个数 inFile.close(); //关闭文件 return true; } bool Checkname(char *a) { int len = strlen(a); //新字符串长 for(int i=0; i<len; i++){ //判断是否为字符 if(!isalpha(a[i])) return false; } return true; } bool Checktel(char *a) { int len = strlen(a); //新的字符串长 for(int i=0; i<len; i++){ //判断是否为数字 if(!isdigit(a[i])) return false; } return true; } void ShowList(LinkList L) { LNode *pshow = L; //建立指针指向头链表 while(pshow){ cout << "name: " << pshow->data->name; cout << " tel: " << pshow->data->tel << endl; pshow = pshow->next; } }

    对于链表掌握不是很透彻的同学可以复制这个程序,然后自己debug一下,尝试是否能修改成功,后续我会上传完整代码。

    一位不甘堕落的大学生

    Processed: 0.010, SQL: 8