链表大杂烩

 
#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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值