Hi GuyZ,
This is another important question you have to solve in Linked List. This may be your work in Data Structures and Algorithms syllabus of MCA/BCA/BTech.
This is another important question you have to solve in Linked List. This may be your work in Data Structures and Algorithms syllabus of MCA/BCA/BTech.
Source Code
/*
Coded By Ajith Kp[ajithkp560]
http://www.terminalcoders.blogspot.com
*/
#include <iostream>
using namespace std;
struct node
{
int data;
node *link=NULL;
}*root;
int main(){
int n, data, i;
node *nn, *tmp, *pn;
root = new node;
cout<<"Enter size of array to sort: ";
cin>>n;
//arr = new int[n];
cout<<"Enter "<<n<<" items: ";
cin>>data;
nn = new node;
nn->data = data;
root = nn;
for(i=1;i<n;i++){
cin>>data;
nn = new node;
tmp = root;
nn->data = data;
if(tmp->data>nn->data){
nn->link = tmp;
root = nn;
}
else{
while(tmp && tmp->data<nn->data){
pn = tmp;
tmp = tmp->link;
}
pn->link = nn;
nn->link = tmp;
}
}
cout<<"Sorted linked list: ";
tmp = root;
while(tmp){
cout<<tmp->data<<" ";
tmp=tmp->link;
}
cout<<endl;
return 0;
}
