#include <stdio.h>
class Node
{
public:
Node* pre;
int num;
Node* next;
Node()
{
pre=NULL;
next=NULL;
}
Node(int num):num(num)
{
pre=NULL;
next=NULL;
}
~Node()
{
}
};
class LinkedList
{
public:
Node* phead;
LinkedList()
{
phead=new Node();
}
~LinkedList()
{
}
void Add(int num)
{
Node* pcur=phead;
while(pcur->next!=NULL)
{
pcur=pcur->next;
}
Node* pnew=new Node(num);
pnew->pre=pcur;
pcur->next=pnew;
}
void Add(int num,int index)
{
Node* pcur=phead;
int i=0;
while(pcur->next!=NULL&&i<index)
{
pcur=pcur->next;
i++;
}
Node* pnew=new Node(num);
pnew->pre=pcur;
pnew->next=pcur->next;
pcur->next->pre=pnew;
pcur->next=pnew;
}
int Remove(int index)
{
Node* pcur=phead;
int i=0;
while(pcur->next!=NULL&&i<=index)
{
pcur=pcur->next;
i++;
}
if(pcur!=NULL&&pcur->pre!=NULL) pcur->pre->next=pcur->next;
if(pcur!=NULL&&pcur->next!=NULL) pcur->next->pre=pcur->pre;
int temp=pcur->num;
delete pcur;
return temp;
}
int GetElement(int index)
{
Node* pcur=phead;
int i=0;
while(pcur->next!=NULL&&i<=index)
{
pcur=pcur->next;
i++;
}
return pcur->num;
}
int GetSize()
{
int i=0;
Node* pcur=phead;
while(pcur->next!=NULL)
{
pcur=pcur->next;
i++;
}
return i;
}
};
class Queue:public LinkedList
{
public:
void AddLast(int num)
{
Add(num);
}
int RemoveFirst()
{
return Remove(0);
}
};
int main()
{
Queue q;
q.AddLast(1);
q.AddLast(2);
q.AddLast(3);
q.AddLast(4);
q.AddLast(5);
q.AddLast(6);
while(q.GetSize()>0)
{
printf("%d\n",q.RemoveFirst());
}
return 0;
}
类实现只Queue
最新推荐文章于 2023-05-23 14:04:09 发布