#include <iostream>
#include <limits>
using namespace Std;
Class Date
{
Public
Date ():d ate (1) {}
Date (int number):d ate (number) {}
Virtual ~date () {}
int GetDate () const {return date;}
virtual void print () const = 0;
Private
int date;
};
Class Book:public Date
{
Public
Book ():P Rice (94) {}
Book (float price,int number);
virtual void print () const
{
cout<< "Book Number:" <<book::getdate () <<endl;
cout<< "book Price:" <<Price<<endl;
}
Private
float price;
};
Book::book (float price,int number):P rice, Date (number) {}
Class Medica:public Date
{
Public
Medica ():P Rice (94) {}
Medica (float price,int number);
virtual void print () const
{
cout<< "Drug Number:" <<medica::getdate () <<endl;
cout<< "The price of the drug is:" <<Price<<endl;
}
Private
float price;
};
Medica::medica (float price,int number):P rice, Date (number) {}
Class Node
{
Public
Node (date*);
~node ();
void Setnext (Node*node) {Itsnext=node;}
Node*getnext () const;
Date*getdate () const;
Private
Node*itsnext;
Date*itsdate;
};
Node::node (date*pdate): Itsdate (PDate), Itsnext (0) {}
Node::~node ()
{
Delete itsdate;
itsdate=0;
Delete Itsnext;
itsnext=0;
}
Node*node::getnext () const
{
return itsnext;
}
Date*node::getdate () const
{
if (itsdate)
return itsdate;
Else
return NULL;
}
Class List
{
Public
List ();
~list ();
Date*list::find (int number) const;
Date*find (int &increase,int count) const;
int GetCount () Const{return count;} Get the number of nodes
Date*getfirst () const;
void Insert (date*);
void repeat () const;
date*operator[] (int) const;
void Delete (int num);
void show () const;
Private
int count;
Node *head;
};
List::list (): Head (0), count (0) {}
List::~list ()
{
Delete head;
}
Date*list::find (int number) const
{
node*pn=0;
For (Pn=head;pn!=null;pn=pn->getnext ())
{
if (Pn->getdate ()->getdate () ==number)
Break
}
if (pn=null)
return NULL;
Else
return Pn->getdate ();
}
Date*list::find (int &increase,int number) const
{
node*pn=0;
For (Pn=head,increase=0;pn!=null;pn=pn->getnext (), increase++)
if (Pn->getdate ()->getdate () ==number)
Break
if (pn==null)
return NULL;
Else
return Pn->getdate ();
}
Date*list::getfirst () const
{
if (head)
return Head->getdate ();
Else
return NULL;
}
void List::insert (Date*pdate)//Receive an address to insert the data object in the linked list
{
Node*pn=new node (pDate);//create a node in the heap and pass the address of the data object to it to return the address of the new node and save it by the temporary pointer pn
Node*pnow=head;
node*pnext=0;//the address of the next node of the current node
int new=pdate->getdate ();
int next=0;
count++;
if (!head)
{
HEAD=PN;
Return
}
if (Head->getdate ()->getdate () >new)//The first getDate is a member function of the node class, returns the address of the Date object, GetDate gets the number of the product, and finally obtains the product number of the head node
{
Pn->setnext (head);//Set the next node of the new node as the head node
head=pn;//Update the head node so that the new nodes become the head nodes, and the previous head nodes become the next node of the new node
Return
}
for (;;)
{
if (!pnow->getnext ())//Whether the next pointer of the current node is empty
{
Pnow->setnext (PN);//The next pointer of the current node does not exist through Pnow call Setnext set head node to the new node
Return
}
Pnext=pnow->getnext ();//The address of the next node is Pnext saved
Next=pnext->getdate ()->getdate ();
if (next>new)//The product number of the next node is greater than the product number of the new node
{
Pnow->setnext (PN);//Pnow call Setnext sets the next node of the head node as the new node whose pnow itself is also the address of the head junction
Pn->setnext (Pnext);//The next node of the new node is set to the next node of the current node
Return
}
pnow=pnext;//the node at the end to the current node loop judgment
}
}
void List::repeat () const//output data number
{
if (!head)
Return
Node*pn=head;
Do
Pn->getdate ()->print ();//Traversal node output data number
while (Pn=pn->getnext ());
}
date*list::operator[] (int offset) const//Gets the data of a node offset to the number of the node to be found
{
NODE*PN =head;
if (!head)
return NULL;
if (offset>=count)//count the number of elements in a linked list
return NULL;
for (int i=0;i<offset;i++)
Pn=pn->getnext ();
return Pn->getdate ();
}
void List::D elete (int num)
{
Node*pback=head;
Node*pnow=head;
if (!head)
cout << "No data to delete" <<endl;
if (Head->getdate ()->getdate () ==num)
{
if (!head->getnext ())
{
Delete head;
cout<< "data is emptied \ n";
Head = 0;
count--;
Return
}
Else
{
Head=head->getnext ();
Delete Pnow;
pnow=0;
cout<< "Delete succeeded";
count--;
Return
}
}
while (Pback)
{
if (Pback->getnext () ==null)
{
cout<< "Could not find the number to delete \ n";
Return
}
if (Pback->getnext ()->getdate ()->getdate () ==num)//delete the next node of the head node
{
Pnow=pback->getnext ();
Pback->setnext (Pback->getnext ()); The next node of the//pback node is set to the next node of the node being deleted, and the next node of the next node that sets the next node of the head junction to the head node is completed.
Delete Pnow; Set the next node of Pback to the next node of Pnow
cout<< "Delete data success";
count--;
return;
}
Pback=pback->getnext ();
}
cout<< "This number does not exist";
}
void List::show () const
{
if (!head)
{
Return
}
Node*pn=head;
Do
{
Pn->getdate ()->print ();
}while (Pn=pn->getnext ());
}
Class Repair
{
Public
void Insert (date*);
void Printall () {ll.repeat ();}
Private
Node*head;
List ll;
};
void Repair::insert (Date*newdate)
{
int num=newdate->getdate ();
int place=0;
if (!ll.find (Place,num))
Ll.insert (newdate);
Else
{
cout<< "You entered the number" <<num<< "with linked list";
Switch (place)
{
Case 0:cout<< "section" <<place+1;break;
default:cout<< "First" <<place+1;
}cout<< "Duplicate numbers";
}
}
int main ()
{
List P1;
date*pdate=0;
int number;//number of the product
float value;
int choice;
BOOL Quit=false;
while (1)
{
System ("CLS");
cout<< "(1) Increase goods (2) List all products (3) Delete items (4) Find the number of items (5) Goods (6) exit:";
cin>>choice;
Switch (choice)
{
Case 1:
while (1)
{
cout<< "(0) return (1) book (2) Medicine";
cin>>choice;
if (!choice)
Break
else if (choice==1| | choice==2)
{
cout<< "Please enter the number:";
cin>>number;
if (choice==1)
{
cout<< "Please enter the book Price:";
cin>>value;
Pdate=new Book (value,number);
}
else if (choice==2)
{
cout<< "Please enter the price of the drug:";
cin>>value;
Pdate=new Medica (Value,number);
P1.insert (pDate);
}
}
Else
{
cout<< "Please enter a number between 0-2 \ n";
}
}
Break
Case 2:
if (P1.getfirst () ==0)
{
cout<< "Your product is empty, please add goods \ n" << "press ENTER to return to main menu \ n";
Cin.get ();
Cin.get ();
}
Else
{
P1.show ();
cout<< "Please press ENTER to return to the main window \ n";
Cin.get ();
Cin.get ();
}
Break
Case 3:
cout<< "Please enter the number you want to delete" <<endl;
cin>>number;
P1. Delete (number);
Cin.get ();
Cin.get ();
Break
Case 4:
while (1)
{
cout<< "(0) return (1) query by number (2) by the number of inquiries";
cin>>choice;
if (!choice)
Break
else if (choice==1| | choice==2)
{
if (choice==1)
{
cout<< "Please enter the number requested for lookup:";
cin>>number;
Date*result=p1.find (number);
if (result==0)
{
cout<< "Cannot find this number \ n";
}
Else
Result->print ();
}
else if (choice==2)
{
cout<< "Please enter the serial number of the requested search:";
cin>>number;
if (P1[number-1])
{
P1[number-1]->print ();
}
Else
cout<< "No data found for query \ n";
}
}
Else
cout<< "Please enter a number of 0-2 \ n";
}
Break
Case 5:
cout<< "The list has" <<p1.getcount () << "node \ n";
Cin.get ();
Cin.get ();
Break
Case 6:
Quit=true;
Break
Default
Cin.clear ();
Cin.ignore (Numeric_limits<streamsize>::max (), ' \ n ');
cout<< "can only input 1-6\n" << "greet enter return re-entry \ n";
Cin.get ();
Break
}
if (quit)
{
cout<< "program end \ n";
Break
}
}
return 0;
}
Insertion and deletion of C + + linked lists