谁会做双端队列(deque)啊??帮帮我

来源:百度知道 编辑:UC知道 时间:2024/06/02 05:04:34
A deque is a data structure consisting of a list of items, on which the following operations are possible.
* push(D,X) -- insert item X on the rear end of deque D.
* pop(D) -- remove the front item from the deque D and return it.
* inject(D,X) -- insert item X on the front end of deque D.
* eject(D) -- remove the rear item from the deque D and return it.
Write routines to support the deque that take O(1) time per operation.

翻译:双端队列(deque)是由一些项的表组成的数据结构,对该数据结构可以进行下列操作:
push(D,X) 将项X 插入到双端队列D的前端
pop(D) 从双端队列D中删除前端项并将其返回
inject(D,X) 将项X插入到双端队列D的尾端
eject(D) 从双端队列D中删除尾端项并将其返回
编写支持双端队伍的例程,每种操作均花费O(1)时间

#include <iostream>
using namespace std;

struct node
{
int value;
node *last,*next;
};

class deque
{
public:
deque()
{
front=NULL;
rear=NULL;
length=0;
}
void push(deque &D,int X);
int pop(deque &D);
void inject(deque &D,int X);
int eject(deque &D);
void display(deque &D);
private:
int length;
node *front,*rear;
};

void deque::push(deque &D,int X)
{
node *p=new node;
p->value=X;
p->last=NULL;
if(D.length==0)
{
p->next=NULL;
D.rear=p;
}
else if(D.length>0)
{
p->next=D.front;
D.front->last=p;
}
D.front=p;
D.length++;
}
int deque::pop(deque &D)
{
if(D.length==0)
{
cout<<"deque is empty"<<endl;
return -1;
}
else
{
node *p;
p=D.front;
D.front=p->next;