Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //selection list linked list
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- int val;
- struct sll
- {
- int info;
- struct sll *next;
- }*prev=NULL,*temp=NULL,*temp2=NULL,*start=NULL,*node=NULL;
- void create_node()
- {
- node=(struct sll*)malloc(sizeof(struct sll));
- if(node==NULL)
- {
- printf("\n Node is not allocated in memory");
- return;
- }
- node->next=NULL;
- }
- void disp()
- {
- if(start==NULL)
- {
- printf("\n Sll is empty to display");
- return;
- }
- printf("\n Display SLL\n");
- temp=start;
- while(temp!=NULL)
- {
- printf("\n %d",temp->info);
- temp=temp->next;
- }
- }
- void in_beg()
- {
- create_node();
- printf("\n Enter the value: ");
- scanf("%d",&val);
- node->info=val;
- if(start==NULL)
- {
- start=node;
- return;
- }
- node->next=start;
- start=node;
- }
- void in_end()
- {
- if(start==NULL)
- {
- in_beg();
- return;
- }
- temp=start;
- while(temp->next!=NULL)
- {
- temp=temp->next;
- }
- create_node();
- printf("\n Enter the value: ");
- scanf("%d",&val);
- temp->next=node;
- node->info=val;
- }
- void del_beg()
- {
- if(start==NULL)
- {
- printf("\n Sll Is empty for delete");
- return;
- }
- if(start->next==NULL)
- {
- printf("\n %d is deleted",start->info);
- free(start);
- start=NULL;
- return;
- }
- temp=start->next;
- printf("\n %d is deleted",start->info);
- free(start);
- start=temp;
- }
- void del_end()
- {
- if(start==NULL)
- {
- printf("\n SLl is empty for delete");
- return;
- }
- if(start->next==NULL)
- {
- del_beg();
- return;
- }
- temp=start;
- while(temp->next!=NULL)
- {
- prev=temp;
- temp=temp->next;
- }
- prev->next=NULL;
- printf("\n %d is Deleted",temp->info);
- free(temp);
- disp();
- }
- void selected_sort()
- {
- int i,j;
- if(start==NULL )
- {
- printf("\n Sll is empty for sorting");
- return;
- }
- if(start->next==NULL)
- {
- printf("\n display sorted dll");
- printf("\n %d",start->info);
- return;
- }
- temp=start;
- while(temp!=NULL)
- {
- temp2=temp;
- while(temp2!=NULL)
- {
- if((temp->info)>(temp2->info))
- {
- val=temp2->info;
- temp2->info=temp->info;
- temp->info=val;
- }
- temp2=temp2->next;
- }
- temp=temp->next;
- }
- disp();
- }
- void main()
- {
- int ch;
- do
- {
- printf("\n \t\t\t ===> Menu <===");
- printf("\n 1.insert from begining");
- printf("\n 2.insert from end");
- printf("\n 3.delete from begining");
- printf("\n 4.delete from end");
- printf("\n 5.Sort ascending order");
- printf("\n 6. Display sll");
- printf("\n Enter your choice: ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- in_beg();
- disp();
- break;
- case 2:
- in_end();
- disp();
- break;
- case 3:
- del_beg();
- disp();
- break;
- case 4:
- del_end();
- break;
- case 5:
- selected_sort();
- break;
- case 6:
- disp();
- break;
- }
- }while(ch<=6 && ch>=1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement