#include <iostream>
#include <cstring>
#include <cctype>
static int length=0;
static int position=0;
class Pllink
{
char* name;
int pos;
Pllink* later;
// Pllink* preventry;
public:
Pllink(char*,int);
~Pllink()
{delete [] name;}
Pllink* LatLink() const
{return later;}
int PosLink() const
{return pos;}
void AddPos()
{pos+=1;}
void SubPos()
{pos-=1;}
void AddLink();
void SetLater(Pllink& now)
{later=&now;}
void DelLink(int);
void ViewLink();
Pllink* CreateLink();
void display() const
{std::cout<<std::endl<<pos<<':'<<name;}
}*list,*last;
Pllink::Pllink(char* na,int p)
{
name=new char[strlen(na)+1];
strcpy(name,na);
pos=p;
length++;
later=0;
}
Pllink* Pllink::CreateLink()
{
static int flag=1;
Pllink* head;
list=0;
while(1)
{
std::cout<<std::endl
<<"Enter your name ('end' is over):"<<std::endl;
char name1[25];
std::cin>>name1;
if(0==strncmp(name1,"end",3))
break;
Pllink* now;
Pllink* first;
if(flag)
{
flag=0;
head=new Pllink(name1,position);
first=head;
}
else
{
now=new Pllink(name1,position);
first->SetLater(*now);
first=now;
}
position++;
last=now;
}
list=head;
return list;
}
void Pllink::AddLink()
{
Pllink* head;
head=list;
if(-1==this->PosLink())
{
this->AddPos();
SetLater(*head);
list=this;
while(head)
{
head->AddPos();
head=head->LatLink();
}
}
else if(length-1==this->PosLink())
{
last->later=this;
last=this;
}
else
{
Pllink* head;
head=list;
while(head)
{
if(0==this->PosLink())
;
else
head=head->LatLink();
if(this->PosLink()==head->PosLink())
{
Pllink* temp;
temp=head->LatLink();
head->SetLater(*this);
later=temp;
this->AddPos();
head=later;
while(head)
{
head->AddPos();
head=head->LatLink();
}
break;
}
}
}
}
void Pllink::DelLink(int nu)
{
if(0==nu)
{
list=list->LatLink();
Pllink* head;
head=list;
while(head)
{
head->SubPos();
head=head->LatLink();
}
length--;
}
else
{
Pllink* head;
head=list;
while(head)
{
if(1==nu)
;
else
head=head->LatLink();
if(nu-1==head->PosLink())
{
Pllink* tem;
tem=head->LatLink();
head->SetLater(*(tem->LatLink()));
tem=tem->LatLink();
while(tem)
{
tem->SubPos();
tem=tem->LatLink();
}
length--;
break;
}
}
}
}
void Pllink::ViewLink()
{
Pllink* head;
head=list;
while(head)
{
head->display();
head=head->LatLink();
}
}
int main()
{
Pllink* link;
link->CreateLink();
while(1)
{
char sign[10];
std::cout<<"What do you do?(add or del) view is allowed and 'end' is over:"<<std::endl;
std::cin>>sign;
if(0==strncmp(sign,"add",3))
{
std::cout<<std::endl
<<"Enter your name (the data is dispart by a '#'):"<<std::endl;
char data[30];
char name1[25];
char* num=0;
int pos1=0,i=0;
std::cin>>data;
if(0==strncmp(data,"end",3))
break;
while(isalpha(data[i++]))
;
num=&data[i];
strncpy(name1,data,i);
name1[i-1]='\0';
pos1=atoi(num);
Pllink *adlink=new Pllink(name1,pos1);
adlink->AddLink();
}
else if(0==strncmp(sign,"del",3))
{
std::cout<<"Enter the position of the note:";
int nu;
std::cin>>nu;
if(nu<length)
link->DelLink(nu);
else
std::cout<<"out of cope"<<std::endl;
}
else
{
std::cout<<"the count is "<<length<<std::endl;
link->ViewLink();
std::cout<<std::endl;
}
}
return 0;
} |