码迷,mamicode.com
首页 > 其他好文 > 详细

第十五章 链表

时间:2014-06-12 06:44:48      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

bubuko.com,布布扣
/**
数组与链表的区别:数组易随机访问,链表易插入和删除
链表组成:储存数据元素的数据域,储存下一结点地址的指针域
链表易于插入与删除
lists 的用法?????????????????????
*/
//建立一个图书链表
#include<iostream> #include <string> using namespace std; struct book //第一步: 用于建立节点。 class 默认为私有 struct 默认为公有 //节点组成:①数据域 ②指针域(*next) { int num; //图书编号 float price; //图书价格 book *next; }; class list { private: book *head; public: list(){head = NULL;} void insertlist(int bnum,float bprice); //插入 void deletelist(int dnum); //删除 void outputlist(); //显示 void countlist(); //统计链表数 bool check1(string x); //检测图书编号输入是否正确 bool check2(string y); //检测图书价格输入是否正确 }; void list::insertlist(int bnum,float bprice)//插入 { book *Pf,*Pm,*Pb;//Pf为上一结点指针,Pm为本结点指针,Pb为要被插入结点指针 Pm=head;//将本结点Pm设为第一个结点(只是为了方便阅读,其实完全可以用head代替) Pb= new book; //建立要插入的结点 Pb->num = bnum; Pb->price = bprice; //error*******if(head->num == NULL) //因为没有head->num这个东西 if(head == NULL)//若为空链表,则将被插入结点设为第一个结点(建立第一个结点) { head = Pb; Pb->next = NULL; } else if(bnum < head->num)//若要插入的结点编号小于第一个结点的编号(前插) { Pb->next = head; head = Pb; } else { while((bnum >= Pm->num) && (Pm->next != NULL))//遍历满足条件的结点 { Pf = Pm; Pm = Pm->next; } if(bnum >= Pm->num)//尾插 { Pm->next = Pb; Pb->next = NULL; } else //中插 { Pf->next = Pb; Pb->next = Pm; } } } void list::deletelist(int dnum) { book *Pf,*Pm;//Pf为上一结点指针,Pm为本结点指针 Pm=head; if(head == NULL) // 若为空结点 { cout<<"图书储存为空"<<endl; return; } if(Pm->num == dnum) //若被删除的为第一个指针 { head=Pm->next; delete Pm; } else { while(Pm->num != dnum && Pm->next != NULL) { Pf = Pm; Pm = Pm->next; } if(Pm->num == dnum) { Pf->next = Pm->next; delete Pm; } else { cout<<"没有你要删除的图书"<<endl; } } } void list::outputlist() { book *Pm; Pm = head; if(head == NULL) { cout<<"链表为空链表,请选择是否插入链表"<<endl; return; } while(Pm != NULL) { cout<<"图书编号:"<<Pm->num<<\t; cout<<"图书价格:"<<Pm->price<<endl; Pm=Pm->next; } cout<<endl; } void list::countlist() { book *Pm; Pm = head; int countnum = 0; while(Pm != NULL) { countnum++; Pm=Pm->next; } cout<<"图书统计为:"<<countnum<<""<<endl; cout<<endl; } bool list::check1(string x) { int i,j=0; //增加变量j来保存"."出现的次数 for(i = 0;i<x.length();i++) { if(x[i]>9 || x[i]<0)//如果不是0到9或者小数点的话就返回false { cout<<"你输入的不是纯数字,请重新输入"<<endl; return false; } } return true; } bool list::check2(string y) { int i,j=0; //增加变量j来保存"."出现的次数 /*if (y[0]==‘.‘) //假如第一个字符是‘.‘ { cout<<"你输入的不是数字,请重新输入"<<endl; return false; //不是数字,返回false }*/ for(i = 0;i<y.length();i++) //增加for循环来检测"."出现的次数 { if((y[i]==.)) //假如"."出现 j++; //将j自加 } if (j>1) //假如"."超过1个 { cout<<"你输入的不是数字,请重新输入"<<endl; return false; //不是数字,返回false } for(i = 0;i<y.length();i++) { if((y[i]>9 || y[i]<0)&&(y[i]!=.))//如果不是0到9或者小数点的话就返回false { cout<<"你输入的不是数字,请重新输入"<<endl; return false; } } return true; } int main() { list A_student; cout<<"创建一个叫A_student的图书链表"<<endl; while(1) { cout<<"1-显示列表"<<"2-插入列表"<<"3-删除列表"<<"4-统计列表"<<"0-退出列表"<<endl; int n; string xnum,xprice; bool breaklist=false; cin>>n; switch(n) { case 1:A_student.outputlist(); break; case 2:do { cout<<"您要输入的图书编号是:"<<endl; cin>>xnum; }while(!A_student.check1(xnum)); do { cout<<"您要输入的图书价格是:"<<endl; cin>>xprice; }while(!A_student.check2(xprice)); A_student.insertlist(atoi(xnum.c_str()),atof(xprice.c_str()));//将string型转化被asii型 break;//此处要使用多线程,才能保证连续插入列表 case 3:do { cout<<"您要删除的图书编号是:"<<endl; cin>>xnum; }while(!A_student.check1(xnum)); A_student.deletelist(atoi(xnum.c_str())); break; case 4:A_student.countlist(); break; case 0: breaklist=true; break; default:cout<<"请输入菜单内的数字:"<<endl; break; } if(breaklist == true) break; } return 0; }
bubuko.com,布布扣

 

第十五章 链表,布布扣,bubuko.com

第十五章 链表

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/zenseven/p/3782787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!