Write an algorithm insert, Data Structure & Algorithms

Q. Write an algorithm INSERT which takes a pointer to a sorted list and a pointer to a node and inserts the node into its correct position or place in the list. 

Ans:

/* structure containing  the data part and link part */

struct node

{

int data ;

struct node *link ;

} ;

/* Following the inserts node to an ascending order linked list

*/

void INSERT ( struct node **q, int num )

{

struct node *r, *temp = *q ;

r = malloc ( sizeof ( struct node ) ) ;

r -> data = num ;

/* if list is empty or if new node is to be inserted before the first node */

if ( *q == NULL || ( *q ) -> data > num )

{

*q = r ;

( *q ) -> link = temp ;

}

else

{

/* traverse the entire linked list to search or found the position to insert the

new node */

while ( temp != NULL )

{

if ( temp -> data <= num && ( temp -> link -> data > num ||

temp -> link == NULL ))

{

r -> link = temp -> link ;

temp -> link = r ;

return ;

}

temp = temp -> link ; /* go to the next node */ }

}

}

Posted Date: 7/10/2012 5:54:36 AM | Location : United States







Related Discussions:- Write an algorithm insert, Assignment Help, Ask Question on Write an algorithm insert, Get Answer, Expert's Help, Write an algorithm insert Discussions

Write discussion on Write an algorithm insert
Your posts are moderated
Related Questions
Binary: Each node has one, zero, or two children. This assertion creates many tree operations efficient and simple. Binary Search : A binary tree where each and every left

give any example of page replacement using fifo and use your own reference string

i:=1 while(i { x:=x+1; i:=i+1; }

Algorithm for insertion of any element into the circular queue: Step-1: If "rear" of the queue is pointing at the last position then go to step-2 or else Step-3 Step-2: make

1. In computer science, a classic problem is how to dynamically store information so as to let for quick look up. This searching problem arises frequently in dictionaries, symbol t

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

What is complexity of an algorithm? What is the basic relation between the time and space complexities of an algorithm? Justify your answer by giving an example.

You are given two jugs, a 4-gallon one and a 3-gallon one. Neither has any measuring marker on it. There is a tap that can be used to fill the jugs with water. How can you get exac

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

How do collisions happen during hashing? Usually the key space is much larger than the address space, thus, many keys are mapped to the same address. Assume that two keys K1 an