Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Insert Node in a Sorted Order.
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node* link;
- };
- struct node* head;
- void print()
- {
- struct node* temp;
- temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->data);
- temp=temp->link;
- }
- }
- void insert(int value)
- {
- struct node* temp1=(struct node*)malloc(sizeof (struct node));
- temp1->data=value;
- if(head==NULL)
- {
- temp1->link=head;
- head=temp1;
- }
- if(temp1->data<head->data)
- {
- temp1->link=head;
- head=temp1;
- }
- else
- {
- struct node* pred=head;
- struct node* p=pred->link;
- while(p!=NULL&&temp1->data>p->data)
- {
- pred=p;
- printf("%d\n",p->data);
- p=p->link;
- }
- pred->link=temp1;
- temp1->link=p;
- }
- }
- int main()
- {
- head=NULL;
- insert(6);
- insert(7);
- insert(5);
- insert(9);
- insert(10) ;
- insert(1);
- insert(3);
- insert(20);
- print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement