这个是链表的:
#include
class Link
{
public:
int operator ==(Link* k)
{
if(this->next==k->next&&this->element==k->element)
return 1;
return 0;
}
int element;
Link *next;
Link(const int eleva,Link *nextval=NULL)
{
element=eleva;
next=nextval;
}
Link(Link *nextval=NULL)
{
next=nextval;
}
};
class List
{
private:
Link *tail;
Link *head;
Link *fence;
int leftcnt;
int rightcnt;
void init()
{
head=fence=tail=new Link;
leftcnt=rightcnt=0;
}
void removeall()
{
while(head!=NULL)
{
fence=head;
head=head->next;
delete fence;
}
}
public:
List(){init();}
void clear(){removeall();init();}
void insert(const int&);
void append(const int&);
void remove();
void prev();
void setPos(int pos);
void print();
void setStart()
{
fence=head;
rightcnt+=leftcnt;
leftcnt=0;
}
void setEnd()
{
fence=tail;
leftcnt+=rightcnt;
rightcnt=0;
}
void next()
{
if(fence!=tail)
{
fence=fence->next;
rightcnt--;
leftcnt++;
}
}
int leftLength()const{return leftcnt;}
int rightLength()const {return rightcnt;}
int getValue()const
{
if(rightLength()!=0)
return fence->next->element;
}
};
void List::insert(const int &item)
{
fence->next=new Link (item,fence->next);
if(tail==fence) tail=fence->next;
rightcnt++;
}
void List::append(const int &item)
{
tail=tail->next=new Link(item,NULL);
rightcnt++;
}
void List::remove()
{
if(fence->next!=NULL)
{
Link *ltemp=fence->next;
fence->next=ltemp->next;
if(tail==ltemp) tail=fence;//
delete ltemp;
rightcnt--;
}
}
void List::prev()
{
Link *temp=head;
if(fence!=head)
{
while(temp->next!=fence)
temp=temp->next;
fence=temp;
leftcnt--;
rightcnt++;
}
}
void List::setPos(int pos)
{
if(pos>=0&&(pos<=rightcnt+leftcnt))
fence=head;
for(int i=0;i
}
void List::print()
{
Link *temp=head;
cout<<"<";
while(temp!=fence)
{
cout<
temp=temp->next;
}
cout<<"|";
while(temp->next!=NULL)
{
cout<
temp=temp->next;
}
cout<<">"<
void main()
{
List k;
for(int i=1;i<20;i++)
k.append(i);
k.print();
k.setPos(6);
cout<
这个是队列的:
#include
class Link
{
public:
int operator ==(Link* k)
{
if(this->next==k->next&&this->element==k->element)
return 1;
return 0;
}
int element;
Link *next;
Link(const int eleva,Link *nextval=NULL)
{
element=eleva;
next=nextval;
}
Link(Link *nextval=NULL)
{
next=nextval;
}
};
class Queue
{
private:
Link *front;
Link *rear;
int size;
public:
Queue()
{
front=rear=NULL;
size=0;
}
~Queue()
{
clear();
}
void clear()
{
while(front!=NULL)
{
rear=front;
front=front->next;
delete rear;
}
rear=NULL;
size=0;
}
void enqueue(const int &item)
{
if(rear==NULL)
front=rear=new Link(item,NULL);
else
{
rear->next=new Link(item,NULL);
rear=rear->next;
}
size++;
}
void enqueue()
{
if(size!=0)
{
Link *ltemp=front;
front=front->next;
delete ltemp;
if(front==NULL)
rear=NULL;
size--;
}
}
int length()const
{
return size;
}
};
void main()
{
Queue k;
}