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. 


/* 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 ;




/* 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
What will be depth do , of complete binary tree of n nodes, where nodes are labelled from 1 to n with root as node and last leaf node as node n

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

Q. Give the adjacency matrix for the graph drawn below:                                                 Ans: Adjacency matrix for the graph given to us

Which data structure is used for implementing recursion Stack.

explain quick sort algorithm

explain collision resloving techniques in hasing

In the present scenario of global warming, the computer hard ware and software are also contributing for the increase in the temperature in the environment and contributing for the

b) The user will roll two (six-sided) dices and the user will lose the game if (s)he gets a value 1 on either any of the two dices & wins otherwise. Display a message to the user w

A connected graph is a graph wherein path exists among every pair of vertices. A strongly connected graph is a directed graph wherein every pair of distinct vertices is connecte

Warnock's Algorithm An interesting approach to the hidden-surface problem was presented by Warnock. His method does not try to decide exactly what is happening in the scene but