Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- menu();
- add();
- addpos();
- search();
- searchpos();
- searchval();
- Delete();
- deletepos();
- deleteval();
- insertfirst();
- insertlast();
- display();
- typedef struct node
- {
- int a;
- char s[20];
- struct node *next;
- } node;
- node *head=NULL;
- int searchval()
- {
- int b,v;
- node *list=head;
- printf("Enter Your Age:");
- scanf("%d",&v);
- while(1)
- {
- if(list->a==v)
- {
- printf("Name:%s\n",list->s);
- printf("Age:%d\n",list->a);
- break;
- }
- list=list->next;
- }
- printf("\n1.Menu\n");
- printf("2.Exit\n");
- printf("Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- }
- default:
- {
- printf("You Choose Wrong Option!!\a\nPlease Choose Correct Option\n");
- menu();
- }
- }
- }
- int searchpos()
- {
- int p,v,b;
- node *list=head;
- printf("Enter Your Position:");
- scanf("%d",&p);
- if(p==1)
- {
- printf("Name:%s\n",list->s);
- printf("Age:%d\n\n",list->a);
- }
- else
- {
- p=p-2;
- while(p--)
- {
- list=list->next;
- }
- printf("Name:%s\n",list->next->s);
- printf("Age:%d\n",list->next->a);
- }
- printf("\n1.Menu\n");
- printf("2.Exit\n");
- printf("Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- }
- default:
- {
- printf("You Choose Wrong Option!!\a\nPlease Choose Correct Option\n");
- menu();
- }
- }
- }
- int search()
- {
- int b;
- printf("\n1.Search By position\n");
- printf("2.Search By Value\n");
- printf("3.Menu\n");
- printf("4.Exit\n");
- printf("Choose a Option");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- searchpos();
- break;
- }
- case 2:
- {
- searchval();
- break;
- }
- case 3:
- {
- menu();
- break;
- }
- case 4:
- {
- exit(0);
- }
- default:
- {
- printf("You Choose Wrong Option!!\a\nPlease Choose Correct Option\n");
- search();
- }
- }
- }
- int deleteval()
- {
- int b,v;
- node *list=head,*temp;
- printf("Enter Your Value:");
- scanf("%d",&v);
- if(list->a==v)
- {
- head=list->next;
- free(list);
- }
- else
- {
- while(1)
- {
- if(list->next->a==v)
- {
- temp=list->next;
- break;
- }
- else
- {
- list=list->next;
- }
- }
- list->next=temp->next;
- free(temp);
- }
- printf("\n\n1.Menu\n");
- printf("2.Exit\n");
- printf("Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- }
- default:
- {
- printf("You Choose Wrong Option!!\a\nPlease Choose Correct Option\n");
- menu();
- }
- }
- }
- int deletepos()
- {
- int p,b;
- node *list=head;
- node *temp;
- printf("Enter Your Position:");
- scanf("%d",&p);
- if(p==1)
- {
- head=list->next;
- free(list);
- }
- else
- {
- p=p-2;
- while(p--)
- {
- list=list->next;
- }
- temp=list->next;
- list->next=temp->next;
- free(temp);
- }
- printf("\n\n1.Menu\n");
- printf("2.Exit\n");
- printf("Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- }
- default:
- {
- printf("You Choose Wrong Option!!\a\nPlease Choose Correct Option\n");
- menu();
- }
- }
- }
- int Delete()
- {
- int b;
- printf("\n1.Delete By position\n");
- printf("2.Delete By Value\n");
- printf("3.Menu\n");
- printf("4.Exit\n");
- printf("Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- deletepos();
- break;
- }
- case 2:
- {
- deleteval();
- break;
- }
- case 3:
- {
- menu();
- break;
- }
- case 4:
- {
- exit(0);
- }
- default:
- {
- printf("You Choose Wrong Option!!\a\nPlease Choose Correct Option\n");
- Delete();
- }
- }
- }
- int display()
- {
- node *list=head;
- int b;
- if(head==NULL)
- {
- printf("NO Data available!!\n");
- printf("Add Data First!");
- printf("\n\n1.Menu\n");
- printf("2.Exit\n");
- printf(" Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- }
- default:
- {
- printf("Please Choose Correct Option\n");
- main();
- }
- }
- }
- else
- {
- while(list!=NULL)
- {
- printf("Name:%s\n",list->s);
- printf("Age:%d\n",list->a);
- list=list->next;
- }
- printf("\n1.Menu\n");
- printf("2.Exit\n");
- printf(" Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- menu();
- break;
- }
- case 2:
- {
- exit(0);
- }
- default:
- {
- printf("Please Choose Correct Option\n");
- main();
- }
- }
- }
- }
- int addpos()
- {
- int p,b;
- node *list=head;
- node *n=(node*)malloc(sizeof(node));
- printf("Enter student Name:");
- scanf(" %[^\n]",n->s);
- printf("Age:");
- scanf("%d",&n->a);
- printf("Enter Position Number:");
- scanf("%d",&p);
- if(p==1)
- {
- n->next=head;
- head=n;
- }
- else
- {
- p=p-2;
- while(p!=0&& list->next!=NULL)
- {
- list=list->next;
- p--;
- }
- n->next=list;
- list->next=n;
- }
- printf("\n\n1.Add\n");
- printf("2.Menu\n");
- printf("3.Exit\n");
- printf(" Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- add();
- break;
- }
- case 2:
- {
- menu();
- break;
- }
- case 3:
- {
- exit(0);
- }
- default:
- {
- printf("Please Choose Correct Option\n");
- add();
- }
- }
- }
- int insertlast()
- {
- node *list=head;
- int b;
- node *n=(node*)malloc(sizeof(node));
- printf("Enter student Name:");
- scanf(" %[^\n]",n->s);
- printf("Age:");
- scanf("%d",&n->a);
- if(head==NULL)
- {
- head=n;
- n->next=NULL;
- }
- else
- {
- while(1)
- {
- if(list->next==NULL)
- {
- list->next=n;
- n->next=NULL;
- break;
- }
- else
- {
- list=list->next;
- }
- }
- }
- printf("\n1.Add\n");
- printf("2.Menu\n");
- printf("3.Exit\n");
- printf(" Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- add();
- break;
- }
- case 2:
- {
- menu();
- break;
- }
- case 3:
- {
- exit(0);
- }
- default:
- {
- printf("Please Choose Correct Option\n");
- add();
- }
- }
- }
- int insertfirst()
- {
- int b;
- node *n=(node*)malloc(sizeof(node));
- printf("Enter student Name:");
- scanf(" %[^\n]",n->s);
- printf("Age:");
- scanf("%d",&n->a);
- if(head==NULL)
- {
- head=n;
- n->next=NULL;
- }
- else
- {
- n->next=head;
- head=n;
- }
- printf("\n1.Add\n");
- printf("2.Menu\n");
- printf("3.Exit\n");
- printf("Choose a Option:");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- add();
- break;
- }
- case 2:
- {
- menu();
- break;
- }
- case 3:
- {
- exit(0);
- }
- default:
- {
- printf("Please Choose Correct Option\n");
- add();
- }
- }
- }
- int add()
- {
- int a,b;
- printf("\n1.Add At First\n");
- printf("2.Add At Last\n");
- printf("3.Add by position\n");
- printf("Choose a Option:");
- scanf("%d",&a);
- switch(a)
- {
- case 1:
- {
- insertfirst();
- break;
- }
- case 2:
- {
- insertlast();
- break;
- }
- case 3:
- {
- addpos();
- }
- default:
- {
- printf("You Choose Wrong Option!!\a\nPlease Choose Correct Option\n\n");
- printf("1.Add\n");
- printf("2.Menu\n");
- printf("3.Exit\n");
- scanf("%d",&b);
- switch(b)
- {
- case 1:
- {
- add();
- break;
- }
- case 2:
- {
- menu();
- break;
- }
- case 3:
- {
- exit(0);
- }
- default:
- {
- printf("Please Choose Correct Option\n");
- add();
- }
- }
- }
- }
- }
- int menu()
- {
- int m;
- printf("\n 1.Add\n");
- printf(" 2.Search\n");
- printf(" 3.Delete\n");
- printf(" 4.Display\n");
- printf(" 5.Exit\n\n");
- printf("Choose Any option:");
- scanf("%d",&m);
- switch(m)
- {
- case 1:
- {
- add();
- break;
- }
- case 2:
- {
- search();
- break;
- }
- case 3:
- {
- Delete();
- break;
- }
- case 4:
- {
- display();
- break;
- }
- case 5:
- {
- exit(0);
- break;
- }
- default:
- {
- printf("You Choose Wrong Option!!\a\nPlease choose a Correct Option\n");
- menu();
- }
- }
- }
- int main()
- {
- printf(" Singly Link List\n");
- printf(" *____*____*____*_____*\n\n");
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement