📄 dlist.cpp
字号:
//dlist.cpp
//By yangguang96@student.bjpeu.edu.cn
//2006.6.26
//Double direction list
#include "dlist.h"
#include "node.h"
#include <iostream>
#include <string>
using namespace std;
dlist::dlist()
:amount(0),head(NULL),tail(NULL)
{
cout<<"Class DoubleList Created!\n";
}
dlist::~dlist()
{
node * d=NULL;
for(int i=0;;i++)
{
d=head;
head=head->getNext();
delete d;
if(head==NULL) break;
}
}
void dlist::creat(void) //to creat a new list
{
node * freePtr=NULL;
node * head1=NULL;
node * tail1=NULL;
int i(0);
freePtr=new(node);
cout<<"** TIP:If there is no information to enter,please set the person's name as 0 **"<<endl;
freePtr->setDate();
amount=i+1;
head=freePtr;
tail=freePtr;
if(freePtr->getName()!="0")
for(i=0;;i++)
{
head1=freePtr;
freePtr=new(node);
cout<<"** TIP:If there is no information to enter,please set the person's name as 0 **"<<endl;
freePtr->setDate();
amount=i+1;
if(freePtr->getName()=="0")
{
tail=head1;
return;
}
head1->setNext(freePtr);
freePtr->setPre(head1);
}
else return;
}
void dlist::printAll(void) const
{
if(head==NULL)
{
cout<<"You haven't creat a list!"<<endl;
return;
}
if(head->getName()=="0")
{
cout<<endl<<"The list is empty!"<<endl;
return;
}
cout<<endl
<<"*** Print from head to tail or in the other direction? ***"<<endl
<<"*** ***"<<endl
<<"****************** 1.From head to tail ******************"<<endl
<<"****************** 2.from tail to head ******************"<<endl<<endl
<<"CHOOSE: ";
int t(0);
unsigned long i(0);
node * freePtr=NULL;
cin>>t;
if(t==1)
{
cout<<"\nALL INFORMATION:"<<endl;
freePtr=head;
for(i=1;;i++)
{
cout<<"ORDER "<<i<<":"<<endl;
freePtr->printOne();
if(freePtr->getNext()==NULL)
break;
freePtr=freePtr->getNext();
}
}
else if(t==2)
{
cout<<"\nALL INFORMATION:"<<endl<<endl;
freePtr=tail;
for(i=amount;;i--)
{
cout<<"ORDER "<<i<<":"<<endl;
freePtr->printOne();
if(freePtr->getPre()==NULL)
break;
freePtr=freePtr->getPre();
}
}
else
{
cout<<"ERROR!";
return ;
}
cout<<endl<<"Total Amount: "<<amount<<endl;
}
void dlist::insert(void)
{
if(head==NULL)
{
cout<<"You haven't creat a list!"<<endl;
return;
}
int t,order;
do{
cout<<"**************** After the newer is inserted ***************"<<endl
<<"********* what order number do you want he/she is? *********"<<endl
<<"********* *********"<<endl
<<"********* 1.Print All Data To Check The Order Number *********"<<endl
<<"********* 2.Enter The New Order Number *********"<<endl
<<"********* *********"<<endl
<<"**************************************************************************"<<endl;
cout<<"CHOOSE:";
cin>>t;
if(t==1)
this->printAll();
else if(t==2)
{
cout<<"Order Number: ";
cin>>order;
if(order<1||order>amount+1)
{
cout<<"ERROR!"<<endl;
return;
}
}
else
{
cout<<"ERROR!"<<endl;
return ;
}
}while(t!=2);
node * p1=NULL;
node * p2=NULL;
node * p3=NULL;
switch(order)
{
case 1:
p1=new(node);
p1->setDate();
p1->setNext(head);
head->setPre(p1);
head=p1;
amount++;
break;
default:
if(order==amount+1)
{
p1=new(node);
p1->setDate();
p1->setPre(tail);
tail->setNext(p1);
tail=p1;
amount++;
break;
}
else
{
p1=head;
for(int i=2;i<order;i++)
{
p1=p1->getNext();
}
p3=p1->getNext();
p2=new(node);
p2->setDate();
p2->setNext(p3);
p2->setPre(p1);
p1->setNext(p2);
p3->setPre(p2);
amount++;
}
}
cout<<"********************** INSERTION COMPLETED ********************"<<endl;
}
void dlist::search(void) const
{
if(head==NULL)
{
cout<<"You haven't creat a list!"<<endl;
return;
}
int i(0);
do
{
cout<<"********* ??? SEARCH BY WHAT ??? *********"<<endl
<<"********* *********"<<endl
<<"********* 1.Name *********"<<endl
<<"********* 2.Number *********"<<endl
<<"********* *********"<<endl
<<"**************************************************************************"<<endl;
cout<<"CHOOSE:";
int judge(0);
cin>>i;
if(i==1) //search by name
{
string t="unknown";
node * p1=head;
cout<<"Name: ";
cin>>t;
for(int j=0;j<amount;j++)
{
if(p1->getName()==t)
{
p1->printOne();
judge++;
}
p1=p1->getNext();
}
if(judge==0)
{
cout<<"No member matched!"<<endl;
}
else
{
cout<<"There're "<<judge<<" matched!"<<endl;
}
}
else if(i==2) //search by number
{
unsigned long t=0;
node * p1=head;
cout<<"Number: ";
cin>>t;
for(int j=0;j<amount;j++)
{
if(p1->getNum()==t)
{
p1->printOne();
judge++;
}
p1=p1->getNext();
}
if(judge==0)
{
cout<<"No member matched!"<<endl;
}
else
{
cout<<"There're "<<judge<<" matched!"<<endl;
}
}
}while(i!=1&&i!=2);
}
void dlist::nodeDel(void)
{
if(head==NULL)
{
cout<<"You haven't creat a list!"<<endl;
return;
}
cout<<"********************** Enter the member's order number ********************"<<endl
<<"Order Number: ";
unsigned long i(0);
cin>>i;
if(i<1&&i>amount) //error
{
cout<<"ERROR!"<<endl;
return;
}
else if(i==1)
{
node * p1=head;
p1->getNext()->setPre(NULL);
head=p1->getNext();
delete p1;
amount--;
}
else if(i==amount)
{
node * p1=tail;
p1->getPre()->setNext(NULL);
tail=p1->getPre();
delete p1;
amount--;
}
else
{
node * p1=head;
for(int j=1;j<i;j++)
{
p1=p1->getNext();
}
p1->getPre()->setNext(p1->getNext());
p1->getNext()->setPre(p1->getPre());
delete p1;
amount--;
}
}
void dlist::sort(void) const
{
if(head==NULL)
{
cout<<"You haven't creat a list!"<<endl;
return;
}
cout<<"Really sort?(y/n) ";
char t='n';
cin>>t;
if(t=='n') return;
else if(t=='y')
{
node * p1=head;
node * p2=NULL;
string temp1="unknown";
unsigned long temp2=0;
for(int j=0;j<amount-1;j++)
{
p1=head;
for(int i=1;i<amount-j;i++)
{
if(p1->getNext()->getNum()>p1->getNum())
{
p2=p1->getNext();
temp1=p2->getName();
temp2=p2->getNum();
p2->setName(p1->getName());
p2->setNum(p1->getNum());
p1->setName(temp1);
p1->setNum(temp2);
}
p1=p1->getNext();
}
}
}
else
{
cout<<"ERROR!"<<endl;
return;
}
}
void dlist::addList(node * head2)
{
node * t=NULL;
for(int i=0;;)
{
if(head2=NULL) break;
t=new(node);
t->setName(head2->getName());
t->setNum(head2->getNum());
tail->setNext(t);
t->setPre(tail);
tail=t;
head2=head2->getNext();
}
this->addAmount(i);
}
unsigned long dlist::getAmount(void) const
{
return amount;
}
node * dlist::getHead(void) const
{
return head;
}
node * dlist::getTail(void) const
{
return tail;
}
void dlist::setTail(node * t)
{
tail=t;
}
void dlist::addAmount(unsigned long t)
{
amount=amount+t;
}
void dlist::initialize()
{
head=NULL;
tail=NULL;
amount=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -