GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Placement Papers  >  Aztec Systems  >  C

 Print  |  
Question:  Write a program to insert a node in a sorted linked list.




October 10, 2005 06:08:29 #1
 Sunil Basavaraj.Kamatagi   Member Since: Visitor    Total Comments: N/A 

RE: Write a program to insert a node in a sorted linke...
 

/*Function to insert a node in singly sorted linked list */

/*pass the header of the list and the element to be inserted */

/*header node i,e head is declared as global */

/*node is a structure which contains int info; and a pointer of node    type i,e next*/

node * insert_in_sort(int ele){

        node *p,*q=NULL;

        node *newnode=(node *) malloc(sizeof(node));

        newnode->info=ele;

        newnode->next=NULL;

        for(p=head;p!=NULL && p->info<ele;p=p->next)

        q=p;

        if(q==NULL) {    /*the node should be inserted first */

              newnode->next=p;

              head=newnode;

       }

      else{

        q->next=newnode;

        newnode->next=p;

    }

 return head;

   

     

 

Back To Question