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
What is an algorithm?  What are the characteristics of a good algorithm? An algorithm is "a step-by-step process for accomplishing some task'' An algorithm can be given in many

Count Scorecards(30 points) In a tournament, N players play against each other exactly once. Each game results in either of the player winning. There are no ties. You have given a

Using stacks, write an algorithm to determine whether the infix expression has balanced parenthesis or not Algorithm parseparens This algorithm reads a source program and

Part1: Deque and Bag Implementation First, complete the Linked List Implementation of the Deque (as in Worksheet 19) and Bag ADTs (Worksheet 22). Files Needed: linkedList.c Linke

You have to design a framework of a Genetic Algorithm (GA) with basic functionality. The basic functionality includes representation, recombination operators, tness function and se

What is the difference between a grounded header link list and a circular header link list? A header linked list is a linked list which always having a special node, known as t

Binary Search Tree let three types of traversals by its nodes. They are: Pre Order Traversal In Order Traversal Post Order Traversal In Pre Order Traversal, we ca

Write an algorithm to test whether a Binary Tree is a Binary Search Tree. The algorithm to test whether a Binary tree is as Binary Search tree is as follows: bstree(*tree) {

a) Find the shortest paths from r to all other nodes in the digraph G=(V,E) shown below using the Bellman-Ford algorithm (as taught in class). Please show your work, and draw the f

In assignment, you have already started the process of designing a database for the Beauty Salon mini-case (enclosed again below), mainly in the phase of conceptual database design