Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //shitcodeXD
- #include<stdio.h>
- #include<stdlib.h>
- struct node{
- int number;
- struct node *next;
- };
- typedef struct node nd;
- nd *head=NULL,*last=NULL;
- create()
- {
- while(1)
- {
- int value;
- printf("Enter your number(press -1 to exit)\n");
- scanf("%d",&value);
- if(value==-1)
- {
- main();
- }
- last_insart(value);
- }
- }
- void insert_at_first()
- {
- int value;
- printf("\nEnter the value u want to add in list:");
- scanf("%d",&value);
- nd *temp_nd=(nd*)malloc(sizeof(nd));
- temp_nd->number=value;
- temp_nd->next=head;
- head=temp_nd;
- print();
- main();
- }
- last_insart(int value)
- {
- nd *temp_data;
- temp_data=(nd*)malloc(sizeof(nd));
- temp_data->number=value;
- temp_data->next=NULL;
- if(head==NULL)
- {
- head=temp_data;
- last=temp_data;
- }
- else
- {
- last->next=temp_data;
- last=temp_data;
- }
- }
- void insert_at_after()
- {
- int key,value;
- printf("\nEnter a KEY(existing item of list),after that you want to insert a value");
- scanf("%d",&key);
- printf("\nInsert new item after %d KEY",key);
- scanf("%d",&value);
- nd*mynd;
- mynd=head;
- int flag=0;
- while(mynd!=NULL)
- {
- if(mynd->number==key)
- {
- nd *newnd=(nd*)malloc(sizeof(nd));
- newnd->number=value;
- newnd->next=mynd->next;
- mynd->next=newnd;
- printf("%d is inserted after%d\n",value,key);
- flag=1;
- break;
- }
- mynd=mynd->next;
- }
- if(flag==0)
- printf("Key not found\n");
- print();
- main();
- }
- last_at_insert()
- {
- int x;
- printf("\n Enter the value u want to add in list:");
- scanf("%d",&x);
- last_insart(x);
- print();
- main();
- }
- void search()
- {
- int value;
- printf("\nInsert Search item :");
- scanf("%d",&value);
- nd*mynd;
- mynd=head;
- int flag=0;
- while(mynd!=NULL)
- {
- if(mynd->number==value)
- {
- printf("%d is founded",value);
- flag=1;
- break;
- }
- mynd=mynd->next;
- }
- if(flag==0)
- printf("Key not found\n");
- main();
- }
- void delete()
- {
- int value;
- printf("\nInsert Search item to delete:");
- scanf("%d",&value);
- nd*mynd=head;
- nd *previous=NULL;
- mynd=head;
- int flag=0;
- while(mynd!=NULL)
- {
- if(mynd->number==value)
- {
- if(previous==NULL){
- head=mynd->next;
- }
- else
- {
- previous->next = mynd->next;
- printf("%d is deleted\n",value);
- flag=1;
- break;
- }}
- previous=mynd;
- mynd=mynd->next;
- }
- if(flag==0)
- printf("Key not found\n");
- main();
- }
- print()
- {
- nd *mylist;
- mylist=head;
- while(mylist!=NULL)
- {
- printf("%d ",mylist->number);
- mylist=mylist->next;
- }
- main();
- }
- int main()
- {
- int a;
- printf("\n\nWelcome to linked list........\n");
- printf("press 0 for Exit\n");
- printf("Press 1 for Create\n");
- printf("Press 2 for print\n");
- printf("Press 3 for first insert\n");
- printf("press 4 for insert after\n");
- printf("press 5 for search\n");
- printf("Press 6 for last insert\n");
- printf("Press 7 for delete\n");
- int n;
- scanf("%d",&n);
- switch(n)
- {
- case 0:
- //exit();
- break;
- case 1:
- create();
- break;
- case 2:
- print();
- break;
- case 3:
- insert_at_first();
- break;
- case 4:
- insert_at_after();
- break;
- case 5:
- search();
- break;
- case 6:
- last_at_insert();
- break;
- case 7:
- delete();
- break;
- default:
- printf("invalid option\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement