Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int a;
- char ch;
- struct node *next;
- } node;
- node *head;
- void display()
- {
- node *list=head;
- if(head==NULL)
- {
- printf("\nNode is empty.\n\n");
- printf("Enter your data first...\n");
- MENU();
- }
- while(list !=NULL)
- {
- printf("\n\nNum= %d ,Chr= %c\n",list->a,list->ch);
- list=list->next;
- }
- }
- void create()
- {
- int value;
- char ch;
- while(1)
- {
- printf(" \nEnter a integer Number & Character (int,char) (-1 for exit)\n");
- printf("\n");
- scanf("%d",&value);
- if(value==-1)
- {
- display();
- printf("\n\nData Saved\n\n");
- MENU();
- }
- scanf(" %c",&ch);
- insert_end(value,ch);
- }
- }
- void insert_first(int aN,char chN)
- {
- node*p=(node*)malloc(sizeof(node));
- p->a=aN;
- p->ch=chN;
- p->next=NULL;
- if(head==NULL)
- {
- head=p;
- return;
- }
- else
- {
- p->next=head;
- head=p;
- }
- }
- void insert_nth(int n,int aN,char chN)
- {
- node *N=(node*)malloc(sizeof(node));
- N->a=aN;
- N->ch=chN;
- N->next=NULL;
- if(n==1)
- {
- N->next=head;
- head=N;
- return;
- }
- else
- {
- node*list=head;
- n=n-2;
- while(n!=0&&list->next!=NULL)
- {
- list=list->next;
- n--;
- }
- N->next=list->next;
- list->next=N;
- }
- }
- void insert_end(int aN,char chN)
- {
- node *N=(node*)malloc(sizeof(node));
- N->a=aN;
- N->ch=chN;
- N->next=NULL;
- node *list=head;
- if(head==NULL)
- {
- head=N;
- list=N;
- }
- else
- {
- while(list->next!=NULL)
- {
- list=list->next;
- }
- list->next=N;
- }
- }
- int search()
- {
- int n;
- printf(" \nSearch Value\n\n");
- printf("Enter any value for search:");
- scanf("%d",&n);
- node*list=head;
- int x=0;
- while(list!=NULL)
- {
- if(list->a==n)
- {
- printf("\n%d is Found\n\n",n);
- x=1;
- break;
- }
- else
- list=list->next;
- }
- if(x==0)
- {
- printf("\n%d is Not Found\n\n",n);
- }
- MENU();
- }
- void addnode()
- {
- int af,y;
- char cf;
- printf("\n\n\n");
- printf("1.Insert at First.\n");
- printf("2.Insert at Last.\n");
- printf("3.Insert at Nth Position.\n");
- printf("4.Return to Menu\n");
- printf("\nChoose Any Option (1-4):");
- int x;
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- {
- printf("\nInsert at First(int,char)\n");
- printf("\n");
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_first(af,cf);
- display();
- addnode();
- }
- case 2:
- {
- printf("\nInsert at Last(int,char)\n");
- printf("\n");
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_end(af,cf);
- display();
- addnode();
- }
- case 3:
- {
- printf("\nInsert at Nth Position(pos,int,char)\n");
- printf("\n");
- scanf("%d",&y);
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_nth(y,af,cf);
- display();
- addnode();
- }
- case 4:
- {
- MENU();
- break;
- }
- default:
- {
- printf("Error!\nChoose Right Option\n");
- addnode();
- }
- }
- }
- void delete()
- {
- printf("\nDelete Node\n");
- printf("1.Delete by Position \n");
- printf("2.Delete by Value \n");
- printf("3.Return to Menu \n\n");
- printf("Choose Any Option (1-3):");
- int x,s,l;
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- {
- printf(" \nDelete by Position\n");
- printf("\n");
- printf("Enter Position:");
- scanf("%d",&s);
- delete_pos(s);
- delete();
- }
- case 2:
- {
- printf(" \nDelete by Value\n");
- printf("\n");
- printf("Enter Value:");
- scanf("%d",&l);
- delete_item(l);
- delete();
- }
- case 3:
- {
- MENU();
- break;
- }
- default:
- {
- printf("Error!\nChoose Right Option\n");
- delete();
- }
- }
- }
- void delete_pos(int pos)
- {
- node *list=head,*temp=NULL;
- int x=0;
- if(pos==1)
- {
- head=head->next;
- free(list);
- return;
- }
- pos=pos-2;
- while(pos!=0)
- {
- list=list->next;
- pos--;
- if(list==NULL)
- return;
- }
- temp=list->next;
- list->next=temp->next;
- x=1;
- free(temp);
- if(x==1)
- {
- printf("Key is deleted\n\n");
- }
- else
- {
- printf("Key is not found\n");
- }
- }
- void delete_item(int value)
- {
- node *list = head, *temp=NULL;
- int x = 0;
- while(list!=NULL)
- {
- if(list->a==value)
- {
- if(temp==NULL)
- head = list->next;
- else
- temp->next = list->next;
- printf("%d is deleted from list\n", value);
- x = 1;
- free(list);
- break;
- }
- temp = list;
- list = list->next;
- }
- if(x==0)
- printf("Key not found!\n");
- }
- void MENU()
- {
- printf("\n\nSingly Linked List\n\n");
- printf("1.Create Nodes\n");
- printf("2.Add Nodes\n");
- printf("3.Search Nodes\n");
- printf("4.Delete Nodes\n");
- printf("5.Display\n");
- printf("6.Exit \n\n\n");
- printf("Choose Any Option (1-5):");
- int x;
- scanf("%d",&x);
- switch(x)
- {
- case 1:
- {
- create();
- break;
- }
- case 2:
- {
- addnode();
- break;
- }
- case 3:
- {
- search();
- break;
- }
- case 4:
- {
- delete();
- break;
- }
- case 5:
- {
- dis();
- break;
- }
- case 6:
- {
- break;
- }
- default:
- {
- printf("Error!\nChoose Right Option\n");
- MENU();
- }
- }
- }
- void dis()
- {
- node *list=head;
- if(head==NULL)
- {
- printf("\nNode is empty.\n");
- printf("Enter your data first...\n");
- MENU();
- }
- while(list !=NULL)
- {
- printf("\n\nNum= %d ,Chr= %c\n",list->a,list->ch);
- list=list->next;
- }
- MENU();
- }
- int main()
- {
- head=NULL;
- MENU();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement