資源簡介
數據結構上機作業,鏈表寫列車時刻表管理系統,cpp文件
代碼片段和文件信息
#include?
using?namespace?std;
template?
struct?linkNode
{
????T?TrainNumStartTimeEndTimeStartPointEndPoint;
????linkNode?*link;
????linkNode(linkNode?*ptr=NULL)
????{
????????link=ptr;
????}
????linkNode(const?T?&item1const?T?&item2const?T?&item3const?T?&item4const?T?&item5?linkNode?*ptr?=?NULL)
????{
????????TrainNum=item1;
????????StartTime=item2;
????????EndTime=item3;
????????StartPoint=item4;
????????EndPoint=item5;
????????link=ptr;
????}
};
template
class?List
{
protected:
????linkNode?*first;
public:
????List(){first=new?linkNode;}
????List(const?T&?x){first=new?linkNode?(x);}
????~List(){makeEmpty();}
????void?makeEmpty()
????{
????????linkNode?*p=new?linkNode();
????????p->link=NULL;
????}
????int?Length()const
????{
????linkNode?*p=first->link;
????int?count=0;
????while(p!=NULL)
????{p=p->link;count++;}
????return?count;
????}
????linkNode?*Search(T?x)
????{
????linkNode?*current=first->link;
????while(current!=NULL)
????????if(current->TrainNum==x)break;
????????else?current=current->link;
????return?current;
????}
????int?Searchloc(T?x)
????{
????????int?i=0;
????????linkNode?*current=first->link;
????????while(current!=NULL)
????????{
????????????i++;
????????????if(current->TrainNum==x)
????????????????break;
????????????else?current=current->link;
????????}
????????return?i;
????}
????linkNode?*Locate(int?i)
????{
????????if(i<0)return?NULL;
????????linkNode?*current=first;
????????int?k=0;
????????while(current!=NULL&&k????????{
????????????current=current->link;
????????????k++;
????????}
????????return?current;
????}
????bool?getData(int?iT&?x)const
????{
????????if(i<=0)return?NULL;
????????linkNode?*current=Locate(i);
????????if(current==NULL)return?false;
????????else{x=current->data;return?true;}
????}
????void?setData(int?iT&?x)
????{
????????if(i<=0)return;
????????linkNode?*current=Locate(i);
????????if(current==NULL)return;
????????else?current->data=x;
????}
????bool?Insert(int?iT&?x1T&?x2T&?x3T&?x4T&?x5)
????{
????????linkNode?*current=Locate(i);
????????if(current==NULL)return?false;
????????linkNode?*newNode=new?linkNode(x1x2x3x4x5);
????????if(newNode==NULL){cout<<“存儲分配錯誤“< ????????newNode->link=current->link;
????????current->link=newNode;
????????return?true;
????}
????bool?Delete(int?iT&?x)
????{
????????linkNode?*del*current;
????????if(i<=1){del=first;first=first->link;}
????????else
????????{
????????????current=first;
????????????for(int?k=1;k ????????????{
????????????????if(current==NULL)break;
????????????????else?current=current->link;
????????????}
????????????if(current==NULL||current->link==NULL)
????????????{
????????????????cout<<“無效“< ????????????????return?false;
????????????}
????????????del=current->link;
????????????current->link=del->li
評論
共有 條評論