#include<iostream>
using namespace std;
class book
{
public:
int num;
float price;
book *next;
};
book *head;
//尾插法创建链表
book *CreatEnd()
{
book *p1;
book *p2;
p1=new book;
head=p1;
p2=p1;
cout<<"pls input book numbers 0 quit;"<<endl;
cin>>p1->num;
if(p1->num!=0)
{
cout<<"pls input book price:"<<endl;
cin>>p1->price;
}
else
{
delete p1;p1=NULL;p2=NULL;head=NULL;
return head;
}
while(p1->num!=0)
{
p2=p1;
p1=new book;
cout<<"pls input book numbers 0 quit;"<<endl;
cin>>p1->num;
if(p1->num!=0)
{
cout<<"pls input book price:"<<endl;
cin>>p1->price;
}
p2->next=p1;
}
delete p1;p2->next=NULL;
return head;
}
//前插法创建链表
book *CreatFront()
{
book *p1;
//book *p2;
p1=new book;
cout<<"pls input numbers:"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"pls input prices:"<<endl;
cin>>p1->price;
p1->next=head;
//head=NULL;
::head=p1;
}
else
{
delete p1;p1=NULL;head=NULL;
return head;
}
while (p1->num!=0)
{
p1=new book;
cout<<"pls input numbers:"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"pls input prices:"<<endl;
cin>>p1->price;
p1->next=head;
//head=NULL;
::head=p1;
}
}
delete p1;p1=NULL;
return head;
}
void Show(book *head)
{
cout<<endl;
cout<<"THE BOOK MENU IS:"<<endl;
while(head)
{
cout<<"The NUMBER IS :"<<head->num<<"\t";
cout<<"The Price Is :"<<head->price<<endl;
head=head->next;
}
}
void Find(book *head,int n)
{
//book link;
//link=head;
while(head)
{
if(head->num==n)
{
cout<<"book number:"<<head->num<<"\t"<<"book price:"<<head->price<<endl;
}
head=head->next;
}
}
void delete1(book *head, int num)
{
book *l;
//删除的节点为头节点;
if (head->num==num)
{
l=head; //先连;
head=head->next; //先连;
::head=head;
delete l; //之后断开;
cout<<"success"<<endl;
}
while (head)
{
if (head->next==NULL)
{
cout<<"Can't find the node!(inside while)"<<endl;
}
//删除的节点为中间或者其他节点;
if (head->next->num==num)
{
l=head->next; //定位要删除的节点,连接;
head->next=l->next; //连接;
delete l; //删除节点;
cout<<"success"<<endl;
}
head=head->next;
}
cout<<"Can't find the node!(outside while)"<<endl;
}
//前插法
void insertFront(book *head,int num,float price)
{
book *l;
//while (num!==0)
//{
l=new book;
l->num=num;
l->price=price;
l->next=head;
::head=l;
//}
}
//尾插法
void insertEnd(book *head,int num,float price)
{
book *link,*l;
link=new book;
while (head)
{
l=head;
head=head->next;
}
l->next=link;
link->num=num;
link->price=price;
link->next=NULL;
}
int main()
{
int n;
float pri;
CreatFront();
Show(head);
/*
cout<<"in put what you found:"<<endl;
cin>>n;
Find(head,n);
cout<<"in put what you delete:"<<endl;
cin>>n;
delete1(head,n);
Show(head);
*/
cout<<"in put what you Insert(number):"<<endl;
cin>>n;
cout<<"in put what you Insert(price):"<<endl;
cin>>pri;
insertFront(head,n,pri);
Show(head);
system("pause");
return 0;
}
08-18