链表程序一枚

来源:互联网 发布:九州园艺三角梅淘宝店 编辑:程序博客网 时间:2024/05/24 06:47

之前仿写的最长的一篇文章。。有必要再重新消化吸收下了。。

#include<iostream>#include<string>#include<cstdlib>using namespace std;class book{public:int num;float price;book *next;};book *head=NULL;bool check(string str){for(int i=0;i<str.length();i++){if((str[i]<'0'||str[i]>'9')&&str[i]!='.')return false;//else return true;}return true;}book *creat(){book *p1,*p2;string str;p1=new book;head=p1;//p2=p1;cout<<"请输入图书编号,以0结束:";    cin>>str;while(!check(str)){cout<<"输入的图书编号格式不对,重新输入:"<<endl;cin>>str;}p1->num=atoi(str.c_str());if(p1->num){cout<<"请输入价格:";        cin>>str;while(!check(str)){cout<<"输入的图书价格格式不对,重新输入:"<<endl;cin>>str;}    p1->price=atof(str.c_str());}else{cout<<"输入结束,,,。。";delete p1;//p2=NULL;//p2->next=NULL;head=NULL;return head;}while(p1->num){p2=p1;p1=new book;        cout<<"继续输入编号,以0结束:";//cin>>p1->num;        cin>>str;while(!check(str)){cout<<"输入的格式不对,重新输入:"<<endl;cin>>str;}    p1->num=atoi(str.c_str());if(p1->num){cout<<"请输入价格:";cin>>str;while(!check(str)){cout<<"输入的格式不对,重新输入:"<<endl;cin>>str;}            p1->price=atof(str.c_str());}p2->next=p1;//p2->next第一次相当于head->next。}delete p1;p2->next=NULL;return head;}void show(book *head){while(head!=0){cout<<"图书编号:"<<head->num<<"\t"<<"图书价格:"<<head->price<<endl;head=head->next;}}void del(book *head){if(head==NULL){cout<<"图书为空不能删除,按回车返回"<<endl;return;}else{cout<<" 输入要删除的节点编号:";    int n;    cin>>n;    book *p;    if(head->num==n){p=head;    head=head->next;    ::head=head;            delete p;            cout<<"删除节点成功,,按回车返回。"<<endl;     return;}    while(head){if(head->next==NULL){cout<<endl;    cout<<"::没有节点可以删除"<<endl;    return;}    if(head->next->num==n){p=head->next;    head->next=p->next;    delete p;cout<<"删除节点成功,按回车返回。"<<endl;                return ;}    head=head->next;}//cout<<"找不到要删除的节点编号。";}}void insert(book *head){if(head==NULL){cout<<"目录为空,按回车返回。"<<endl;//cin.get();//cin.get();//system("cls");return;}/*cout<<"请选择插入方式:1、前插法  2、后插法 3、中间插法"<<endl;string str;cin>>str;if(str[0]=='1'){cout<<"您选择的是:前插法:";book*l=new book;cout<<"输入插入节点的编号:"<<endl;cin>>l->num;cout<<"输入插入节点的价格:"<<endl;cin>>l->price;l->next=::head;::head=l;}if(str[0]=='2'){//while(::head!=NULL)    //{::head=::head->next;}    //::head=head;    //head->next=NULL;    cout<<"您选择的是:后插法:"<<endl;    book*l=new book;    cout<<"输入插入节点的编号:"<<endl;    cin>>l->num;    cout<<"输入插入节点的价格:"<<endl;    cin>>l->price;    book *p;    while(head){p=head;    head=head->next;}    p->next=l;     //???head=l;这个不行的原因是因为末尾地址改变    //与之前失去联系。。    l->next=NULL;}    if(str[0]=='3'){cout<<"您选择的是:中间插法:";if(head->next){book*l=new book;        cout<<"输入插入节点的编号:"<<endl;        cin>>l->num;        cout<<"输入插入节点的价格:"<<endl;        cin>>l->price;        book*p;book*h;        while(head->num==l->num){p=head;            h=head->next;            p->next=l;            l->next=h;            head=head->next;}}else  cout<< "中间插法必须至少有两个点, 按回车返回。" <<endl;  }*/else{cout<<"输入要插入的节点编号:";        book*l=new book;cin>>l->num;cout<<"输入要插入节点的价格:"<<endl;cin>>l->price;book *p;    if(l->num<head->num){cout<<"前插法,";l->next=::head;    ::head=l;cout<<"插入成功,按回车返回。"<<endl;}else{while(head->next!=NULL){p=head;head=head->next;}if(l->num>head->num){head->next=l;l->next=NULL;cout<<"后插法,插入成功,按回车返回。"<<endl;}else{head=::head;//自己想到的,挺牛呀 嘎嘎                while(head->next->num<l->num){head=head->next;}if((head->next->num>l->num)&&(head->num<l->num))//第二个条件排除插入与前一点序号相同的点{p=head->next;head->next=l;l->next=p;cout<<"中间插法,插入成功,回车返回"<<endl;}else{cout<<"节点编号重复,重新输入,按回车返回"<<endl;}}}    }}//讲解的做法能否删除最后一个节点呢void number(book*head){int count=0;while(head!=NULL){count++;head=head->next;}cout<<"总的图书数目为:"<<count<<"个。"<<endl;return;}int main(){string str;begin:cout<<"1:重建图书  2:显示图书  3:插入图书 "<<"4:删除图书  5:图书数目  Q:退出"<<endl;cout<<endl;cout<<"输入您选择的编号:";    cin>>str;if(str[0]=='1'){cout<<endl;cout<<"编号为  1:重建图书"<<endl;head=creat();system("cls");goto begin;}elseif(str[0]=='2'){  cout<<endl;  cout<<"编号为   2:显示图书"<<endl;  if(head==NULL)  {cout<<"没有要显示的图书"<<endl;    cout<<"按回车键返回主程序"<<endl;  }      else  {show(head);            cout<<"按回车键返回主程序"<<endl;  }  cin.get();  cin.get();  system("cls");  goto begin;}else if(str[0]=='3'){cout<<endl;        cout<<"编号为   3:插入图书"<<endl;        insert(head);cin.get();cin.get();        system("cls");goto begin;}else if(str[0]=='4'){cout<<endl;cout<<"编号为   4:删除图书"<<endl;del(head);cin.get();cin.get();system("cls");goto begin;}else if(str[0]=='5'){cout<<endl;cout<<"编号为   5:图书数目"<<endl;number(head);cin.get();cin.get();system("cls");goto begin;}elseif(str[0]!='Q'&&str[0]!='q'){cout<<"请输入数字1到5!按回车返回。"<<endl;cin.get();cin.get();system("cls");goto begin;}cout<<endl;    cout<<"再见!"<<endl;return 0;}


0 0
原创粉丝点击