Access over 35 million academic & study documents

A deque is a data structure consisting of a list of items, on which

Content type
User Generated
Rating
Showing Page:
1/6
A deque is a data structure consisting of a list of items, on
which the following operations are possible:
push(x): Insert item x on the front end of the deque.
pop(): Remove the front item from the deque and return it.
inject(x): Insert item x on the rear end of the deque.
eject(): Remove the rear item from the deque and return it.
Write routines to support the deque that take O(1) time per
operation.
Solution
#ifndef _deque _h
#define _deque _h
struct Node;
typedef struct Node *PtrToNode;
struct DequeRecord
{
PtrToNode Front, Rear;
};
typedef struct DequeRecord *Deque;
void Push( ElementType X, Deque D );
ElementType Pop( Deque D );
void Inject( ElementType X, Deque D );
ElementType Eject( Deque D );

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/6
#endif /* _deque_h */
struct Node
{
ElementType Element;
PtrToNode Next, Last;
};
/* Implementation of a deque using a doubly linked list with
a header
/* front and rear point to two ends of the deque
respectively
/* front always points to the header
/* deque is empty when front == rear
*/
void Push( ElementType X, Deque D )
{ /* Insert item X on the front end of deque D */
PtrToNode Newnode;
Newnode = malloc( sizeof ( Node ) ); /* create a new node
*/
if ( Newnode == Null )
FatalError (\"The memory is full\");
Newnode->Element = X;
Newnode->Next = D->Front->Next;
Newnode->Last = D->Front; /* Newnode is all set now */

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/6

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 6 pages?
Access Now
Unformatted Attachment Preview
A deque is a data structure consisting of a list of items, on which the following operations are possible: push(x): Insert item x on the front end of the deque. pop(): Remove the front item from the deque and return it. inject(x): Insert item x on the rear end of the deque. eject(): Remove the rear item from the deque and return it. Write routines to support the deque that take O(1) time per operation. Solution #ifndef _deque _h #define _deque _h struct Node; typedef struct Node *PtrToNode; struct DequeRecord { PtrToNode Front, Rear; }; typedef struct DequeRecord *Deque; void Push( ElementType X, Deque D ); ElementType Pop( Deque D ); void Inject( ElementType X, Deque D ); ElementType Eject( Deque D ); #endif /* _deque_h */ struct Node { ElementType Element; PtrToNode Next, Last; }; /* Implementation of a deque using a doubly linked list with a header /* front and rear point to two ends of the deque respectively /* front always points to the header /* deque is empty when front == rear */ void Push( ElementType X, Deque D ) { /* Insert item X on the front end of deque D */ PtrToNode Newnode; Newnode = malloc( sizeof ( Node ) ); /* create a new node */ if ( Newnode == Null ) FatalError (\"The memory is full\"); Newnode->Element = X; Newnode->Next = D->Front->Next; Newnode->Last = D->Front; /* Newnode is all set now */ if ( D->Front == D->Rear ) /* if deque was originally empty */ D->Rear = Newnode; /* reset rear */ else /* if deque was not empty */ D->Front->Next->Last ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Just the thing I needed, saved me a lot of time.

Studypool
4.7
Indeed
4.5
Sitejabber
4.4