Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- struct node{
- int data;
- node* next;
- };
- node* insert (node*list, int x)
- {node *m=new(node);
- node*q=list;
- node*p=new(node);
- p->data=x;
- if(q==NULL)
- {p->next=list;
- list=p;
- return list;}
- else if(p->data > q->data)
- { while(q!=NULL){
- m=q->next;
- q->next=p;
- p->next=m;
- q=q->next;
- }
- }
- else
- {
- p->next=q;
- list=p;
- q=q->next;
- return list;
- }
- return list;}
- void display (node*list)
- {node*p=list;
- while (p!=NULL)
- {printf("%d ",p->data);
- p=p->next;
- }}
- int main()
- {node*list=NULL;
- list=insert(list,5);
- list=insert(list,4);
- list=insert(list,3);
- list=insert(list,9);
- printf("The sorted linked list=\n");
- display(list);
- return 0;
- }