C++ 链表 4-- 结构体链表-类的封装形式-动态链表-链表使用

#include <iostream>
#include <string>
using namespace std;
/*---------------------------------

     15-14使用链表   和小节“C++ 链表 3-- 结构体链表-类的封装形式-动态链表-链表的创建、插入、删除、排序”类似

---------------------------------*/
class book   //定义为类
{
public:
int     num;
float price;
book *next;
};
book *head=NULL;
void creat();
bool check(string str);
void showbook(book *head);
void deletebook(book *head,int num);
void insertrear(book *head,int num,float price);  //尾部插入法
void insertfront(book *head,int num,float price); //头部插入法
void insertmiddle(book *head,int num,float price); //中间插入法
int  getBookNum(book *head);
int   inputnum();
float inputprice();
int main()
{
string str;
begin:
cout<<"1-重建图书 2-显示图书 3-插入图书 4-删除图书 5-图书数目 q-退出"<<endl;
cin>>str;
if(str[0]=='1') //1-重建图书
{
creat();
system("cls"); //清显示屏
goto begin;
}
else if(str[0]=='2') //2-显示图书
{
if(NULL==head)
{
cout<<"您的图书现在是空的,请增加图书"<<endl<<"按回车键返回";
cin.get();//用来接收上一句输出信息最后的回车键
cin.get(); //用来接收用户按下的回车键
system("cls");
goto begin;
}
else
showbook(head);
goto begin;
}
else if(str[0]=='3') //3-插入图书
{
if(NULL==head)
{
cout<<"您的图书现在是空的,请增加图书"<<endl<<"按回车键返回";
cin.get();//用来接收上一句输出信息最后的回车键
cin.get(); //用来接收用户按下的回车键
system("cls");
goto begin;
}
else
{
int num;
float price;
num =inputnum();
price =inputprice();
insertmiddle(head,num,price);
goto begin;
}
}
else if(str[0]=='4') //4-删除图书
{
if(NULL==head)
{
cout<<"您的图书现在是空的,请增加图书"<<endl<<"按回车键返回";
cin.get();//用来接收上一句输出信息最后的回车键
cin.get(); //用来接收用户按下的回车键
system("cls");
goto begin;
}
else
{
int num;
num =inputnum();
deletebook(head,num);
goto begin;
}
}
else if(str[0]=='5') //5-图书数目
{
if(NULL==head)
{
cout<<"您的图书现在是空的,请增加图书"<<endl<<"按回车键返回";
cin.get();//用来接收上一句输出信息最后的回车键
cin.get(); //用来接收用户按下的回车键
system("cls");
goto begin;
}
else
{
cout<<"图书数目是:"<<getBookNum(head)<<endl;
goto begin;
}
}
return 0;
}


int  getBookNum(book *head)
{
int num=0;
while(head)
{
num++;
head= head->next;
}
return num;
}


int inputnum()
{
int num;
string str;
cout<<"请输入图书的编号:"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入:"<<endl;
cin>>str;
}
num = atoi(str.c_str()); //atoi()字符串转整型数据
return num;
}
float inputprice()
{
float price;
string str;
cout<<"请输入图书的价格:"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入:"<<endl;
cin>>str;
}
price = atof(str.c_str()); //atoi()字符串转整型数据
return price;
}
void insertmiddle(book *head,int num,float price) //中间插入法
{
book *list =new book;
list->num =num;
list->price =price;
if(num<=head->num)
{
list->next =head;
::head =list;
return;
}
book *temp=NULL;
while((num>head->num)&&(head->next!=NULL))
{
temp =head;
head =head->next;
}
if(num>head->num)  //大于等于,一定在尾部插入
{
head->next =list;
list->next =NULL;
}
else               //小于,一定在中间插入
{
list->next =head;
temp->next =list;
}
}
void insertfront(book *head,int num,float price) //头部插入法
{
book *list =new book;


list->next=head;
::head =list;
list->num =num;
list->price =price;
}
void insertrear(book *head,int num,float price)  //尾部插入法
{
book *list =new book;
book *l;
while(head)
{
l=head;   //最后用于存储链表的最后一个节点的位置
head=head->next;
}
l->next=list;
list->num =num;
list->price =price;
list->next =NULL;
}


void deletebook(book *head,int num)
{
book *l;


if(head->num == num)
{
l =head;
head=head->next;
::head =head;    //双冒号是全局操作符,标识head是个全局变量
delete l;        //释放头结点占用的内存空间
cout<<"操作成功"<<endl;
return;
}
l =head;                //l记录前节点的位置
head=head->next;        //head记录当前判断节点的位置
while(head)
{
if(head->num == num)
{
l->next=head->next;
delete head;    //释放要删除节点所占用的空间
cout<<"删除成功"<<endl;
return;
}
else
{
l =head;
head=head->next;
}
}
cout<<"没有该节点,未删除任何数据"<<endl;
}


void showbook(book *p)  //形参指针p,这颗指针最
{                          //终指向的位置并不影响传递进来那个指针的指向
cout<<"图书的信息如下:"<<endl;
while(p)
{
cout<<"图书编号:"<<p->num<<'\t'<<"价格:"<<p->price<<endl;
p =p->next;
}
}


bool check(string str)
{
for(int i=0;i<str.length();i++)
{
if((str[i]>'9'||str[i]<'0')&&(str[i]!='.'))
return false;
}
return true;
}


void creat()
{
book *p1,*p2;
p1 =new book;
string str;
head =p1;
p2 =p1;
cout<<"请输入图书编号,以0结束:"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入,按0返回:"<<endl;
cin>>str;
}
p1->num = atoi(str.c_str()); //atoi()字符串转整型数据
if(p1->num == 0)
head =NULL;


while(p1->num != 0)
{
cout<<"请输入图书的价格:"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入,按0返回:"<<endl;
cin>>str;
}
p1->price = atof(str.c_str()); //atoi()字符串转浮点数据
p2 =p1;
p1 =new book;
p2->next =p1;
cout<<"请输入图书编号,以0结束:"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入的不是数字,请重新输入,按0返回:"<<endl;
cin>>str;
}
p1->num = atoi(str.c_str()); //atoi()字符串转整型数据
}


cout<<"before delete p1: "<<p1<<endl;
delete p1;  //释放p1指向的内存空间,但p1指针仍旧指向那个地方
cout<<"after delete p1: "<<p1<<endl;
p1 =NULL;   
p2->next =NULL;

}


运行结果:

1-重建图书 2-显示图书 3-插入图书 4-删除图书 5-图书数目 q-退出
2
图书的信息如下:
图书编号:1     价格:12.3
图书编号:2     价格:263.3
图书编号:5     价格:25.03
1-重建图书 2-显示图书 3-插入图书 4-删除图书 5-图书数目 q-退出
3
请输入图书的编号:
4
请输入图书的价格:
23.6
1-重建图书 2-显示图书 3-插入图书 4-删除图书 5-图书数目 q-退出
2
图书的信息如下:
图书编号:1     价格:12.3
图书编号:2     价格:263.3
图书编号:4     价格:23.6
图书编号:5     价格:25.03
1-重建图书 2-显示图书 3-插入图书 4-删除图书 5-图书数目 q-退出
4
请输入图书的编号:
5
删除成功
1-重建图书 2-显示图书 3-插入图书 4-删除图书 5-图书数目 q-退出
2
图书的信息如下:
图书编号:1     价格:12.3
图书编号:2     价格:263.3
图书编号:4     价格:23.6
1-重建图书 2-显示图书 3-插入图书 4-删除图书 5-图书数目 q-退出
5
图书数目是:3
1-重建图书 2-显示图书 3-插入图书 4-删除图书 5-图书数目 q-退出
q
Press any key to continue

猜你喜欢

转载自blog.csdn.net/paulliam/article/details/80395413
今日推荐