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,

Preorder traversal of a binary tree struct NODE { struct NODE *left; int value;     /* can take any data type */ struct NODE *right; };   preorder(struct N

Question 1 What is a data structure? Discuss briefly on types of data structures Question 2 Explain the insertion and deletion operation of linked list in detail Question

Explain Floyd's algorithm It is convenient to record the lengths of shortest paths in an n by n matrix D known as the  distance matrix: the element d ij   in the i th   row an


Q. Explain the result of inserting the keys given. F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B, X, Y, D, Z, E  in an order to an empty B-tree of degree-3.

Q. Which are the two standard ways of traversing a graph?  Explain them with an example of each.  Ans:   T he two ways of traversing a graph are written below

Draw the process flow diagram: Anand   Dairy (AD) sources 150,000 litres of milk daily from large number of local villagers .The milk is collected from 4:00 AM to 6:00 am and

Q. Explain the technique to calculate the address of an element in an array. A  25 × 4  matrix array DATA is stored in memory in 'row-major order'. If base  address is 200 and

Problem Your LC code is stored in a memory location as shown and the variable name is LC                  LC Memory address       Content(LC code)