Technical Interview

Home
Technical Interview
Interview Process
Introduction Questions
Quantitative Problems
Google & Microsoft
Algorithms
Object Oriented
C/C++ Questions
Java Questions
Data Structures
Fundamental Questions
Puzzles
Resume Tips
Added Recently
Links
Contact Us
Submit Question/Answer

 

   
Sort a linked list. Write a C program.




   

Solutions:

 

//sorting in descending order

struct node

{

int value;

node* NEXT;

}

//Assume HEAD pointer denotes the first element in the //linked list

// only change the values…don’t have to change the //pointers

 

Sort( Node *Head)

{

node* first,second,temp;

first= Head;

while(first!=null)

{

second=first->NEXT;

while(second!=null)

{

if(first->value < second->value)

{

temp = new node();

temp->value=first->value;

first->value=second->value;

second->value=temp->value;

delete temp;

}

second=second->NEXT;

}

 

first=first->NEXT;

}

}