Menu Driven Singly Linked List
CODE
👇
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int info;
- struct node *next;
- }NODE;
- NODE* createlist(NODE *list);
- void Display (NODE *list);
- void search (NODE *list);
- NODE* insertbeg(NODE *list);
- NODE* insertbetween(NODE *list);
- NODE* insertlast(NODE *list);
- NODE* Delpos(NODE *list);
- NODE* Delvalue(NODE *list);
- //MAIN FUNCTION
- void main ()
- {
- printf(">>Singly Linked List<<");
- NODE*list=NULL, *temp;
- int ch,n,pos;
- do
- {
- printf("\n1. Create:");
- printf("\n2. Display");
- printf("\n3.Insert AtFirst");
- printf("\n4.Insert AtMiddle");
- printf("\n5.Insert AtLast");
- printf("\n6. Delete by position");
- printf("\n7. Delete by value");
- printf("\n8. Search");
- printf("\n9.Reverse");
- printf("\n10.Count");
- printf("\n11. Exit\n");
- printf("\n Enter your choice:");
- scanf("%d", &ch);
- switch(ch)
- {
- case 1:
- list=createlist (list);
- break;
- case 2:
- Display (list);
- break;
- case 3:
- list=insertbeg (list);
- break;
- case 4:
- list=insertbetween (list);
- break;
- case 5:
- list=insertlast (list);
- break;
- case 6:
- break;
- case 7:
- break;
- case 8:search (list);
- break;
- case 9:
- break;
- case 10:
- break;
- case 11:printf("_END_");
- break;
- }
- }while(ch != 11);
- }
- //CREATE FUNCTION
- NODE* createlist (NODE *list)
- {
- int n,count;
- NODE *temp, *newnode;
- printf ("\nHow many nodes you want to enter ? \n");
- scanf("%d" ,&n);
- for(count=1 ; count<=n; count++)
- {
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->next=NULL;
- printf( "Enter the node data:- " );
- scanf ("%d",&newnode->info);
- if (list==NULL)
- {
- list=temp=newnode;
- }
- else
- {
- temp->next=newnode;
- temp=newnode;
- }
- }
- return list;
- }
- void Display (NODE *list)
- {
- NODE *temp=list;
- while(temp!=NULL)
- {
- printf ("%d",temp->info);
- printf("-->");
- temp=temp->next ;
- }
- printf("NULL \n");
- }
- //Insert AT BEGINING
- NODE * insertbeg(NODE *list)
- {
- int n;
- printf("Enter the node you want to insert at first position:");
- scanf("%d", &n);
- NODE *newnode;
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->info=n;
- newnode->next=list;
- list=newnode;
- return list;
- }
- //INSERT IN BETWEEN
- NODE * insertbetween(NODE *list)
- {
- NODE *newnode, *temp=list;
- int n,i,pos;
- printf("Enter the node data and position you want to insert between the node: \t");
- scanf("\n%d", &n);
- scanf("\n%d", &pos);
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->next=NULL;
- newnode->info=n;
- for(i=1; i<pos-1&&temp->next!=NULL; i++)
- temp=temp->next;
- newnode->next=temp->next;
- temp->next=newnode;
- return list;
- }
- //INSERT AT LAST
- NODE * insertlast(NODE *list)
- {
- int n;
- printf("Enter the node data you want to insert at last position: \t");
- scanf("%d", &n);
- NODE *newnode, *temp;
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->info=n;
- newnode->next=NULL;
- for(temp=list; temp->next!=NULL; temp=temp->next);
- temp->next=newnode;
- return list;
- }
- //SEARCH
- void search (NODE *list)
- {
- int num, flag=0;
- NODE *temp;
- printf("Enter the element to be search:");
- scanf("%d", &num);
- for(temp=list; temp!=NULL; temp=temp->next)
- {
- if(temp->info==num)
- {
- printf(">>%d is Found<<\n",num);
- flag=1;
- exit;
- }
- }
- if(flag==0)
- printf(">>%d is not found<<\n", num);
- }
- //DELETE BY POSITION
- NODE * Delpos(NODE *list)
- {
- NODE *temp=list, *temp1;
- int i, pos;
- printf("Enter node position to delete the node: \t");
- scanf("%d", &pos);
- if(pos==1)
- {
- list=temp->next;
- free (temp);
- return list;
- }
- for(i=1, temp=list; i<=pos-1 && temp!=NULL; i++)
- temp=temp->next;
- if(temp==NULL)
- {
- printf(">>position is out of range<<");
- return list;
- }
- temp1=temp->next;
- temp->next=temp1->next;
- free (temp1);
- return list;
- }
- //DELETE BY VALUE
- NODE* Delvalue(NODE *list)
- {
- NODE *temp=list, *temp1;
- int num;
- printf("Enter node data to delete that node \t");
- scanf("%d", &num);
- if(list->info==num)
- {
- list=list->next;
- free (temp);
- return list;
- exit;
- }
- for(temp=list; temp->next!=NULL; temp=temp->next)
- if(temp->next->info==num)
- {
- temp1=temp->next;
- temp->next=temp1->next;
- free (temp1);
- return list;
- exit;
- }
- printf(">>Element is not found<<\n");
- return list;
- }
Comments
Post a Comment