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
Conceptually, the stack abstract data type mimics the information kept into a pile on a desk. Informally, first we consider a material on a desk, where we might keep separate stack

Elaborate the symbols of abstract data type length(a)-returns the number of characters in symbol a. capitalize(a)-returns the symbol generated from a by making its first cha


In this unit, we learned the data structure arrays from the application point of view and representation point of view. Two applications that are representation of a sparse matrix

Q. What do you understand by the term sparse matrix? How sparse matrix is stored in the memory of a computer? Write down the function to find out the transpose of a sparse matrix u

Spanning Trees: A spanning tree of a graph, G, refer to a set of |V|-1 edges which connect all vertices of the graph. There are different representations of a graph. They are f

Varieties of Arrays In some languages, size of an array should be established once and for all at program design time and can't change during execution. Such arrays are known a

WHAT IS THE PURPOSE OF STACK IN C

Write a function that performs the integer mod function. Given the previous functions you have implemented already, this one should be a piece of cake. This function will find the

This question is based on the requirements of a system to record band bookings at gigs. (A 'gig' is an event at which one or more bands are booked to play). You do not need to know