我给你动态链表清单程序参考一下 打个比方 就是几个环连在一起 1 2 3 4 只要把2拆下 再连13即可
而且何须用free 那是c语言的东西 堆中创造不方便 用new和delete来做更简单
程序代码:
#include <iostream>
#include <string>
using namespace std;
struct book
{
int num;
float price;
book *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;
}
book *head=NULL;
book* creat()
{
book *p1;
book *p2;
p1=new book;
head=p1;
p2=p1;
cout<<"请输入图书的编号,必须是数字,输入0则返回主菜单"<<endl;
string str;cin>>str;
while(!check(str)){
cout<<"输入的不是数字,请重新输入,按0返回!!!"<<endl;
cin>>str;
}
p1->num = atoi(str.c_str());
if(p1->num!=0)
{
cout<<"请输入图书的价格"<<endl;
string str;cin>>str;
while(!check(str)){
cout<<"输入的不是数字,请重新输入!!!"<<endl;
cin>>str;
}
p1->price = atof(str.c_str());
}
else
{
delete p1;p2 =NULL;head=NULL;return head;
}
system("cls");
cout<<"1->重建图书2->显示图书3->插入图书4->删除图书5->图书数目Q->退出" <<endl;
while(p1->num!=0)
{
p2=p1;
p1=new book;
cout<<"请输入图书的编号,必须为数字,输入0则返回主菜单"<<endl;
string str;cin>>str;
while(!check(str)){
cout<<"输入的不是数字,请重新输入,按0返回!!!"<<endl;
cin>>str;
}
p1->num = atoi(str.c_str());
if(p1->num!=0)
{
cout<<"请输入图书的价格,必须是数字"<<endl;
string str;cin>>str;
while(!check(str)){
cout<<"输入的不是数字,请重新输入!!!"<<endl;
cin>>str;
}
p1->price = atof(str.c_str());
}
p2->next=p1;
system("cls");
cout<<"1->重建图书2->显示图书3->插入图书4->删除图书5->图书数目Q->退出 " <<endl;
}
delete p1;
p2->next=NULL;
return head;
}
void Showbook(book *head)
{
cout<<endl;
cout<<"图书信息如下:"<<endl;
while(head)
{
cout<<"图书编号:"<<head->num<<"\t";
cout<<"价格:"<<head->price<<endl;
head=head->next;
}
}
void insert(book *head,int num,float price)
{
book *list =new book;
book *l;
while(head)
{
l=head;
head=head->next;
}
list->num=num;
list->price=price;
list->next=NULL;
l->next=list;
}
void Delete(book *head, int num)
{
book *l;
if(head->num==num)
{
l=head;
head=head->next;
::head=head;
delete l;
cout<<"操作成功"<<endl;
return;
}
while(head)
{
if(head->next==NULL)
{
cout<<"找不到要删除的编号."<<endl;
return ;
}
if(head->next->num==num)
{
l=head->next;
head->next=l->next;
delete l;
cout<<"操作成功"<<endl;
//::head=l1;
return;
}
head=head->next;
}
cout<<"找不到不要删除的编号."<<endl;
}
int getbooknum(book *head)
{
int num=0;
while(head)
{
num++;
head=head->next;
}
return num;
}
int main()
{
string str;
begin:
cout<<"1->重建图书2->显示图书3->插入图书4->删除图书5->图书数目Q->退出"<<endl;
cin>>str;
if(str[0]=='1')
{
creat();
system("cls");
goto begin;
}
else if(str[0]=='2')
{
if(head==NULL)
{
cout<<"你的图书现在是空的,请增加图书"<<endl<<"按回车键返回主程序"<<endl;
cin.get();
cin.get();
system("cls");
goto begin;
}
Showbook(head);
}
else if(str[0]=='3')
{
if(head==NULL)
{
cout<<"你的图书现在是空的,请增加图书"<<endl<<"按回车键返回主程序"<<endl;
cin.get();
cin.get();
system("cls");
goto begin;
}
int num;
float price;
cout<<"请输入要插入的图书编号:"<<endl;
string str1;cin>>str1;
while(!check(str1))
{
cout<<"输入的不是数字,请重新输入,按0返回!!!"<<endl;
cin>>str1;
}
num = atoi(str1.c_str());
cout<<"请输入要插入的图书价格:"<<endl;
string str2;cin>>str2;
while(!check(str2)){
cout<<"输入的不是数字,请重新输入,按0返回!!!"<<endl;
cin>>str2;
}
price = atof(str2.c_str());
insert(head,num,price);
cout<<"操作完毕,按回车键返回主程序"<<endl;
}
else if(str[0]=='4')
{
if(head==NULL)
{
cout<<"你的图书现在是空的,请增加图书"<<endl<<"按回车键返回主程序"<<endl;
cin.get();
cin.get();
system("cls");
goto begin;
}
int num;
cout<<"请输入要删除的图书编号:"<<endl;
string str3;cin>>str3;
while(!check(str3))
{
cout<<"输入的不是数字,请重新输入,按0返回!!"<<endl;
cin>>str3;
}
num = atoi(str3.c_str());
Delete(head,num);
}
else if(str[0]=='5')
{
cout<<"图书数目是:"<<getbooknum(head)<<endl<<"按回车键返回主程序"<<endl;
cin.get();
cin.get();
system("cls");
goto begin;
}
else
{
if(str[0]!='Q' &&str[0]!='q')
cout<<"请输入数字!按回车键返回继续操作";
}
if(str[0]!='Q' &&str[0]!='q')
{
cin.get();
cin.get();
system("cls");
goto begin;
}
system("pause");
return 0;
}