4-1 Deque (25 min)

Source: Internet
Author: User
4-1 Deque (25 min)

A "deque" is a data structure consisting of a list of items, on which the following operations be Possible:push (X,D): In SERT item X on the front end of Deque D. Pop (d): Remove the front item from Deque D and return it. Inject (x,d): Insert item X on the rear end of Deque D. Eject (d): Remove the rear item from Deque D and return it. Write routines to support the deque, which take O (1) time per operation. Format of functions:

Deque Createdeque ();
int Push (ElementType X, Deque D);
ElementType Pop (Deque D);
int Inject (ElementType X, Deque D);
ElementType Eject (Deque D);

Where Deque is defined as the following:

typedef struct NODE *ptrtonode;
struct Node {
    ElementType Element;
    Ptrtonode Next, last;
};
typedef struct DEQUERECORD *deque;
struct Dequerecord {
    ptrtonode Front, Rear;
};

Here the deque is implemented by a doubly linked list with a header. Front and Rear point to the ends of the deque respectively. Front always points to the header. The deque is empty while Front and Rear both point to the same dummy header. Note:push and Inject is supposed to return 1 if the operations can is done successfully, or 0 if fail. If the deque is empty, Pop and Eject must return ERROR which are defined by the Judge program. Sample Program of judge:

#include <stdio.h> #include <stdlib.h> #define ElementType int #define ERROR 1e5 typedef enum {Push, pop, in

Ject, eject, end} operation;
typedef struct NODE *ptrtonode;
    struct Node {ElementType Element;
Ptrtonode Next, last;
};
typedef struct DEQUERECORD *deque;
struct Dequerecord {ptrtonode Front, Rear;};
Deque Createdeque ();
int Push (ElementType X, Deque D);
ElementType Pop (Deque D);
int Inject (ElementType X, Deque D);

ElementType Eject (Deque D);          Operation Getop (); /* Details omitted */void Printdeque (Deque D);
    /* Details omitted */int main () {ElementType X;
    Deque D;

    int done = 0;
    D = Createdeque ();
            while (!done) {switch (Getop ()) {case push:scanf ("%d", &x); if (!
            Push (X, D)) printf ("Memory is full!\n");
        Break
            Case pop:x = pop (D);
            if (x==error) printf ("Deque is empty!\n");
        Break 
   Case inject:         scanf ("%d", &x); if (!
            Inject (X, D)) printf ("Memory is full!\n");
        Break
            Case eject:x = eject (D);
            if (x==error) printf ("Deque is empty!\n");
        Break
            Case End:printdeque (D);
            Done = 1;
        Break
}} return 0;
 }/* Your function would be put here */
Sample Input:
Pop
Inject 1
pop
Eject
push 1
push 2
Eject
Inject 3
End
Sample Output:
Deque is empty!
Deque is empty!
Inside Deque:2 3

Double-ended queue//initial state: Front=rear (front is the head node)//Join a node after:front-> element (update to rear)//plus element with one:front-> element (new rear) Deque
    Createdeque () {Deque p;
    p= (Deque) malloc (sizeof (struct dequerecord));
    P->front= (Ptrtonode) malloc (sizeof (struct Node));
    p->front->last=null;
    p->rear=p->front;
    p->rear->next=null;
return p;
    } int Push (ElementType X, Deque D) {struct node* temp;
    temp= (struct node*) malloc (sizeof (struct Node));
    if (!temp) return 0;
    temp->element=x;
        if (d->front==d->rear) {//null double-ended queue d->front->next=temp;
        temp->last=d->front;
        d->rear=temp;
        temp->next=null;
    return 1;
    } temp->next=d->front->next;
    d->front->next->last=temp;
    d->front->next=temp;
    temp->last=d->front;
return 1;
    } ElementType Pop (Deque D) {if (d->front==d->rear)//Empty double-ended queue return ERROR;
 int temp=d->front->next->element;   struct node* t=d->front->next;
        if (d->front->next==d->rear) {//A special case of an element d->rear=d->front;
        d->rear->next=null;
        Free (t);
    return temp;
    } d->front->next->next->last=d->front;
    d->front->next=d->front->next->next;
    Free (t);
return temp;
    } int Inject (ElementType X, Deque D) {struct node* temp;
    temp= (struct node*) malloc (sizeof (struct Node));
    if (!temp) return 0;
    temp->element=x;
        if (d->front==d->rear) {//null double-ended queue d->front->next=temp;
        temp->last=d->front;
        d->rear=temp;
    return 1;
    } d->rear->next=temp;
    temp->last=d->rear;
    temp->next=null;
    d->rear=temp;
return 1;
    } ElementType Eject (Deque D) {if (d->front==d->rear) return ERROR;
    int temp=d->rear->element;
    struct node* t=d->rear;
    d->rear=d->rear->last; D->rear->next=null;
    Free (t);
return temp;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.