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
In this sorting algorithm, multiple swapping occurs in one pass. Smaller elements move or 'bubble' up to the top of the list, so the name given to the algorithm. In this method,

Task If quicksort is so quick, why bother with anything else? If bubble sort is so bad, why even mention it? For that matter, why are there so many sorting algorithms? Your

Implement multiple stacks in a single dimensional array. Write algorithms for various stack operations for them.

If quicksort is so quick, why bother with anything else? If bubble sort is so bad, why even mention it? For that matter, why are there so many sorting algorithms? Your mission (sho

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 fu

Unlike a binary-tree, each node of a B-tree may have a number of keys and children. The keys are stored or saved in non-decreasing order. Each key has an related child that is the

Question 1 Write the different characteristics of an algorithm Question 2 Explain in brief the asymptotic notations Question 3 Write an algorithm of insertion sort and e

an electrical student designed a circuit in which the impedence in one part of a series circuit is 2+j8 ohms and the impedent is another part of the circuit is 4-j60 ohm mm program

Use a random number generator to create 10 numbers between 1 and 1000 and store them in 2 different arrays.  The first array should contain the numbers as they are generated.  The

Q. Write down the algorithm which does depth first search through an un-weighted connected graph. In an un-weighted graph, would breadth first search or depth first search or neith