Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node_tag
- {
- int Data;
- struct node_tag *link;
- }node;
- node *GetNode()
- {
- node *New;
- New=(node*)malloc(sizeof(node));
- New->link=NULL;
- return New;
- }
- void Display(node *Last)
- {
- node *x;
- x=Last->link;
- if(x==NULL){
- printf("empty");
- }
- while(x!=Last)
- {
- printf("%d,",x->Data);
- x=x->link;
- }
- printf("%d,",Last->Data);
- }
- /*the below function checks 3 conditions except NULL
- >>that are if the data is more than last it will insert at last
- >>if the data is smaller than 1st node then insert at begin
- >> else find a value that is equal to or greater than a
- and deploy a pointer that is just one node ahead
- now place the new link in between the two pointers*/
- node *Sortedlist(node *last,int a)
- {
- node *y,*x,*New;
- New=GetNode();
- New->Data=a;
- x=last;
- if(last==NULL)
- {
- last=New;
- last->link=last;
- }
- else if(last->Data<a)
- {
- New->link=last->link;
- last->link=New;
- last=New;
- }
- else if((last->link)->Data >= a)
- {
- New->link=last->link;
- last->link=New;
- }
- else
- {
- x=last->link;
- while(1)
- {
- y=x;
- x=x->link;
- if(x->Data>=a)
- {
- break;
- }
- }
- y->link=New;
- New->link=x;
- }
- return last;
- }
- node *CreateList()
- {
- int ch,x,s,p1;
- node *last=NULL;
- while(1)
- {
- printf("enter 1 to continue and 0 to exit:");
- scanf("%d",&ch);
- if(ch==0){
- break;}
- else
- {
- printf("enter Data:");
- scanf("%d",&x);
- last=Sortedlist(last,x);
- Display(last);
- printf("\n");
- }
- }
- return last;
- }
- int main()
- {
- node *last;
- last=CreateList();
- Display(last);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement