|
发表于 2021-9-28 18:25:18
|
显示全部楼层
本帖最后由 jhq999 于 2021-9-28 18:27 编辑
注意大小写;函数类型不能和函数名连在一起;声明数组用方括号不是括号
- #include<iostream>
- #include<cstdlib>
- #include<string.h>
- #include<iomanip>
- using namespace std;
- typedef struct
- {
- char num[5];
- char name[9];
- char phone[13];
- }ElemType;
- typedef struct node
- {
- ElemType data;
-
- struct node *next;
- }ListNode,*LinkList;
- int menu_select(){
- int sn;
- cout<<"Welcome to Friends Book Management"<<endl;
- cout<<"1.LinkList of FriendsBook--Create"<<endl;
- cout<<"2.Node of Friends Book--Insert"<<endl;
- cout<<"3.Node of Friends Book--Search"<<endl;
- cout<<"4.Node of Friends Book--Delete"<<endl;
- cout<<"5.LinkList of Friends Book--Show"<<endl;
- cout<<"6.Exit"<<endl;
- cout<<"CeEnter 1-6 to select the menu: ";
- for(; ;){
- cin>>sn;
- if(sn<1 || sn>6)
- cout<<"\n\tInput Error! please enter 1-6 to select!\n\nCeEnter 1-6 to select the menu: ";
- else break;
- }
- cout<<endl<<endl;
- return sn;
- }
- void PrintLIst(LinkList head){
- LinkList p;
- p=head->next;
- if(p){
- cout<<"number(4) name(8) phone(11)"<<endl;
- cout<<"-------------------------------"<<endl;
- }
- else cout<<"No record!"<<endl;
- while(p){
- cout<<setw(6)<<p->data.num<<setw(10)<<p->data.name<<setw(13)<<p->data.phone<<endl;
- p=p->next;
- }
- cout<<"---------------------------"<<endl<<endl;
- }
- void CreateList(LinkList &head){///////////////
- ListNode *p, *rear;
- int flag=0;
- rear=head;
- while(flag==0){
- cout<<"---------------------------------"<<endl;
- cout<<"- number(4) name(8) phone(11) -"<<endl;
- cout<<"---------------------------------"<<endl;
- cout<<"Please input the data of record including : number(4) name(8) phone(11)"<<endl;
- p=new ListNode;
- cin>>p->data.num>>p->data.name>>p->data.phone;
- rear->next=p;
- rear=p;
- cout<<"Stop Creating List?(1:Stop or 0:Continue)\n";
- cin>>flag;
- }
- rear->next=NULL;
- PrintLIst(head);///////////
- return;
- }
- void InsertNode(LinkList &head, ListNode *p){
- ListNode *p1,*p2;
- p1=head;
- p2=p1->next;
- while(p2&&(strcmp(p2->data.num,p->data.num)<0)) {
- p1=p2;
- p2=p2->next;
- }
- p1->next=p;
- p->next=p2;
- PrintLIst(head);///////////
- }
- ListNode *ListFind(LinkList head){
- LinkList p;
- char num[5];//////////////
- char name[9];
- int selnum;
-
- cout<<"===========================\n";
- cout<<"1.search by number \n";
- cout<<"2.search by name \n";
- cout<<"===========================\n";
- cout<<"Enter your selection: ";
- p=head->next;
- cin>>selnum;
- if(selnum==1){
- cout<<"Please enter the number for searching: ";
- cin>>num;
- while(p && strcmp(p->data.num,num)!=0)
- p=p->next;
- if(p==NULL || strcmp(p->data.num,num)!=0)
- p=NULL;
- }
- else
- if(selnum==2){
- cout<<"Please enter the name for searching: ";
- cin>>name;
- while(p && strcmp(p->data.name,name)!=0)
- p=p->next;
- }
- return p;
- }
- void DelNode(LinkList head){/////////////////
- char ch;
- LinkList p,q;
- p=ListFind(head);
- if(!p){
- cout<<"not found!\n\n";
- return;
- }
- cout<<"Delete it, sure(y/n)?";
- cin>>ch;
- if(ch=='y'||ch=='Y'){
- q=head;
- while(q&&q->next!=p)
- q=q->next;
- q->next=p->next;
- delete p;
- cout<<"it has been delete!\n\n";
- PrintLIst(head);//////////////////////////
- }
- }
- void DestroyList(LinkList &L){
- LinkList p;
- while(L) {
- p=L;
- L=L->next;
- delete p;
- }
- return;
- }
- int main(){
- LinkList head,p;
-
- head=new ListNode;
- head->next=NULL;
- while(1){
- switch(menu_select()){
- case 1:
- cout<<"**************************"<<endl;
- cout<<"*LinkList of Friends Book--Create *"<<endl;
- cout<<"**************************"<<endl;
- CreateList(head);
- break;
- case 2:
- cout<<"**************************"<<endl;
- cout<<" Node of Friends Book--Insert *"<<endl;
- cout<< "**************************"<<endl;
- cout<<"* number(4) name(8) phone(11) *"<<endl;
- cout<<"**************************"<<endl;
- cout<<"Please input the data of record including:number(4) name(8) phone(11) "<<endl;
- p=new ListNode;
- cin>>p->data.num>>p->data.name>>p->data.phone;
- InsertNode(head,p);
- break;
- case 3:
- cout<<"**************************"<<endl;
- cout<<"* Node of Friends Book --Search *"<<endl;
- cout<<"*********************************"<<endl;
- p=ListFind(head);
- if(p)
- {
- cout<<" number(4) name(8) Phone(11) "<<endl;
- cout<<"*********************************"<<endl;
- cout<<setw(6)<<p->data.num <<setw(10)<<p->data.name<<setw(13)<<p->data.phone<<endl;/////////////////////
- cout<<"*********************************"<<endl;
- }
- else
- cout<<"Not Found\n\n";
- break;
- case 4:
- cout<<"*********************************"<<endl;
- cout<<"* Node of Friends Book --Delete *"<<endl;
- cout<<"*********************************"<<endl;
- DelNode(head);
- break;
- case 5:
- cout<<"*********************************"<<endl;
- cout<<"* LinkList of Friends Book --Show *"<<endl;
- cout<<"*********************************"<<endl;
- PrintLIst(head);////////////////////////
- break;
- case 6:
- cout<<"\t Quit! \n";
- DestroyList(head);
- return 0;
- }
- }
- }
复制代码 |
|