1. 程式人生 > >C++鏈表的插入與刪除

C++鏈表的插入與刪除

pac out 當前 include intall streams 新節點 repair 節點

#include<iostream>
#include<limits>
using namespace std;
class Date
{
public:
Date ():date(1){}
Date(int number):date(number){}
virtual ~Date(){}
int GetDate()const {return date;}
virtual void print() const = 0;
private:
int date;
};
class Book:public Date
{
public:
Book():Price(94){}
Book(float Price,int number);
virtual void print()const
{
cout<<"圖書編號為:"<<Book::GetDate()<<endl;
cout<<"圖書的價格為:"<<Price<<endl;
}
private:
float Price;
};
Book::Book(float price,int number):Price(price),Date(number){}
class medica:public Date
{
public:
medica():Price(94){}
medica(float Price,int number);
virtual void print()const
{
cout<<"藥品的編號為:"<<medica::GetDate()<<endl;
cout<<"藥品的價格為:"<<Price<<endl;
}
private:
float Price;
};
medica::medica(float price,int number):Price(price),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;}//獲得節點的個數
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)//接收一個要插入鏈表中數據對象的地址
{
Node*pn=new Node(pDate);//堆中創建一個節點Node並將數據對象的地址傳遞給它 返回新節點的地址 並由臨時指針pn保存
Node*pNow=head;
Node*pNext=0;//當前節點的下一個節點的地址
int New=pDate->GetDate();
int next=0;
count++;
if(!head)
{
head=pn;
return;
}
if(head->getDate()->GetDate()>New)//第一個getDate是Node類的成員函數,返回date對象的地址;GetDate獲取商品的編號;最終獲得頭結點的商品編號
{
pn->setnext(head);//將新節點的下一個節點設置為頭結點
head=pn;//更新頭結點 這樣就完成了新節點成為頭結點,先前的頭結點成為了新節點的下一個節點
return;
}
for(;;)
{
if(!pNow->getnext())//當前節點的Next指針是否為空
{
pNow->setnext(pn);//當前節點的Next指針不存在 通過PNow調用setnext設置頭節點的下一個節點為新節點
return;
}
pNext=pNow->getnext();//下一個節點的地址有PNext保存
next=pNext->getDate()->GetDate();
if(next>New)//下一節點的商品編號大於新節點的商品編號
{
pNow->setnext(pn);//通過PNow調用setnext設置頭節點的下一個節點為新節點其PNow本身還為頭結點的地址
pn->setnext(pNext);//新節點的下一個節點設置為當前節點的下一個節點
return;
}
pNow=pNext;//把末尾的節點重新賦給當前節點 循環判斷
}
}
void List::repeat()const// 輸出數據編號
{
if(!head)
return;
Node*pn=head;
do
pn->getDate()->print();//遍歷節點 輸出數據編號
while(pn=pn->getnext());
}
Date*List::operator[](int offset)const//獲取某個節點的數據 offset為待找節點的編號
{
Node*pn =head;
if(!head)
return NULL;
if(offset>=count)//count鏈表的元素個數
return NULL;
for(int i=0;i<offset;i++)
pn=pn->getnext();
return pn->getDate();
}
void List::Delete(int num)
{
Node*pBack=head;
Node*pNow=head;
if(!head)
cout <<"沒有數據可刪除"<<endl;
if(head->getDate()->GetDate()==num)
{
if(!head->getnext())
{
delete head;
cout<<"數據被清空\n";
head =0;
count--;
return;
}
else
{
head=head->getnext();
delete pNow;
pNow=0;
cout<<"刪除成功";
count--;
return;
}
}

while(pBack)
{
if(pBack->getnext()==NULL)
{
cout<<"找不到要刪除的編號\n";
return;
}
if(pBack->getnext()->getDate()->GetDate()==num)//刪除頭節點的下一個節點
{
pNow=pBack->getnext();
pBack->setnext(pBack->getnext());//pBack節點的下一個節點設置為被刪除節點的下個節點;完成將頭結點的下個節點設置為頭結點的的下個節點的下個節點
delete pNow; //將pBack的下一個節點設置為PNow的下一個節點
cout<<"刪除數據成功";
count--;
return ;
}
pBack=pBack->getnext();
}
cout<<"不存在此編號";
}
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<<"您輸入的編號"<<num<<"與鏈表中";
switch(place)
{
case 0:cout<<"第"<<place+1;break;
default: cout<<"第"<<place+1;
}cout<<"個編號重復";
}
}
int main()
{
List p1;
Date*pDate=0;
int number;//商品的編號
float value;
int choice;
bool quit=false;
while(1)
{
system("cls");
cout<<"(1)增加商品(2)列出所有商品(3)刪除商品(4)查找上商品(5)商品的數目(6)退出:";

cin>>choice;
switch(choice)
{
case 1:
while (1)
{
cout<<"(0)返回(1)圖書(2)藥品";
cin>>choice;
if(!choice)
break;
else if(choice==1||choice==2)
{
cout<<"請輸入編號:";
cin>>number;
if(choice==1)
{
cout<<"請輸入圖書價格:";
cin>>value;
pDate=new Book(value,number);

}
else if(choice==2)
{
cout<<"請輸入藥品的價格:";
cin>>value;
pDate=new medica(value,number);
p1.insert(pDate);
}
}
else
{
cout<<"請輸入0—2之間的數字\n";
}
}

break;
case 2:
if(p1.getfirst()==0)
{
cout<<"你的商品為空,請增加商品\n"<<"按回車鍵返回主菜單\n";
cin.get();
cin.get();
}
else
{
p1.show();
cout<<"請按回車鍵返回主窗口\n";
cin.get();
cin.get();
}
break;
case 3:
cout<<"請輸入你要刪除的編號"<<endl;
cin>>number;
p1.Delete(number);
cin.get();
cin.get();
break;
case 4:
while (1)
{
cout<<"(0)返回(1)按編號進行查詢(2)按序號進行查詢";
cin>>choice;
if(!choice)
break;
else if(choice==1||choice==2)
{

if(choice==1)
{
cout<<"請輸入索要查找的編號:";
cin>>number;
Date*result=p1.find(number);
if(result==0)
{
cout<<"找不到該編號\n";
}
else
result->print();
}
else if(choice==2)
{
cout<<"請輸入索要查找的序號:";
cin>>number;
if(p1[number-1])
{
p1[number-1]->print();
}
else

cout<<"找不到查詢的數據\n";
}
}
else
cout<<"請輸入0-2的數字\n";
}
break;
case 5:
cout<<"該鏈表共有"<<p1.getcount()<<"節點\n";
cin.get();
cin.get();
break;
case 6:
quit=true;
break;
default:
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),‘\n‘);
cout<<"只能輸入1-6\n"<<"請安回車鍵返回重新錄入\n";
cin.get();
break;
}


if(quit)
{
cout<<"程序結束\n";
break;
}
}

return 0;
}



C++鏈表的插入與刪除