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

Assignment Help:

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);

}

}


Related Discussions:- Functions for inserting and deleting at either end of deque

Characteristics of good algorithms, What do we mean by algorithm? What are ...

What do we mean by algorithm? What are the characteristics of a good and relevant algorithm? An algorithm is "a step-by-step procedure for finishing some task'' An algorithm c

State z-buffer algorithm, Z-Buffer Algorithm Also known as the Depth-Bu...

Z-Buffer Algorithm Also known as the Depth-Buffer algorithm, this image-space method simply selects for  display the polygon or portion of a polygon that is nearest to the view

Draws a rectangular grid algorithms, Prepare a GUI called Hotplate GUI that...

Prepare a GUI called Hotplate GUI that holds a central panel that draws a rectangular grid that represents Element objects which should be held in a 2-dimensional array. The applic

What is the best case complexity of quick sort, What is the best case compl...

What is the best case complexity of quick sort In the best case complexity, the pivot is in the middle.

How does an array differ from an ordinary variable, Normal 0 fa...

Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

Sort the Sequence Using Merge Sort, Q. Sort the sequence written below of k...

Q. Sort the sequence written below of keys using merge sort. 66, 77, 11, 88, 99, 22, 33, 44, 55                                                                      Ans:

Travelling salesman problem, Example 3: Travelling Salesman problem G...

Example 3: Travelling Salesman problem Given: n associated cities and distances among them Find: tour of minimum length that visits all of city. Solutions: How several

How to measure the algorithm efficiency, How to measure the algorithm's eff...

How to measure the algorithm's efficiency? It is logical to examine the algorithm's efficiency as a function of some parameter n showing the algorithm's input size. Instance

How to construct binary tree, Q. A Binary tree comprises 9 nodes. The preor...

Q. A Binary tree comprises 9 nodes. The preorder and inorder traversals of the tree yield the given sequence of nodes: Inorder :          E     A    C    K    F     H    D

Insertion of a key into a b-tree, Example: Insertion of a key 33 into a B-...

Example: Insertion of a key 33 into a B-Tree (w/split) Step 1: Search first node for key closet to 33. Key 30 was determined. Step 2: Node pointed through key 30, is se

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd