Functions for inserting and deleting at either end of deque, Data Structure & Algorithms

Q. Devise a representation for a given list where insertions and deletions can be made at both the ends. Such a structure is called Deque (which means Double ended queue). Write functions for inserting and deleting at both the ends. 

Ans:

struct Deque

{

int info;

struct Deque *left, *right;

}*head, *tail;

add(struct Deque *p, int x)

{

struct Deque *temp;

temp = malloc (sizeof(struct Deque), 1);

temp -> info = x;

if(p == head)

{

temp->right = head; temp->left = NULL; head->left = temp;

head = temp;

}

else if(p == tail)

{

temp->left = tail; temp->right = NULL; tail->right = temp; tail = temp;

}

}

delete(struct Deque *p, int x)

{

struct Deque *temp;

if(p == head)

{

temp = head;

head = head->right;

head->left = NULL; temp->right = NULL; free(temp);

}

else if(p == tail)

{

temp = tail;

tail = tail->left; tail->right = NULL; temp->left = NULL; free(temp);

}

}

Posted Date: 7/10/2012 7:16:06 AM | Location : United States







Related Discussions:- Functions for inserting and deleting at either end of deque, Assignment Help, Ask Question on Functions for inserting and deleting at either end of deque, Get Answer, Expert's Help, Functions for inserting and deleting at either end of deque Discussions

Write discussion on Functions for inserting and deleting at either end of deque
Your posts are moderated
Related Questions
Like general tree, binary trees are implemented through linked lists. A typical node in a Binary tree has a structure as follows struct NODE { struct NODE *leftchild; i

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Advantages of dry running a flowchart When dry running a flowchart it's advisable to draw up a trace table illustrating how variables change their values at every stage in the

Example: (Double left rotation while a new node is added into the AVL tree (RL rotation)) Figure: Double left rotation when a new node is inserted into the AVL tree A

Do you have a library solution for this problem?

State in detail about the Integer Carrier set of the Integer ADT is the set {..., -2, -1, 0, 1, 2, ...}, and  operations on these values are addition, multiplication, subtrac

This unit discussed about data structure called Arrays. The easiest form of array is a one-dimensional array which may be described as a finite ordered set of homogeneous elements

Explain Division Method Division Method: - In this method, key K to be mapped into single of the m states in the hash table is divided by m and the remainder of this division

Write a program to create a hashed file that stores the records currently in the file " data_2013 ". Records should use the same fixed-length schema given previously, and should ag

Hubs - In reality a multiport repeater - Connects stations in a physical star topology - As well may create multiple levels of hierarchy to remove length limitation of 10