#include #include struct node { int data; struct node* link; }*head=NULL,*temp=NULL,*last=NULL; int len,takenode=1; int main() { int ch; while(1) { printf("-----------------------------\n"); printf("Single Linked List Operations : \n"); printf("-----------------------------\n"); printf("1. Node Creation \n"); printf("2. Add at beginning\n"); printf("3. Add at Last \n"); printf("4. Add at any point\n"); printf("5. Update any node\n"); printf("6. Delete any node\n"); printf("7. Search any node \n"); printf("8. Display all elements\n"); printf("9. Length\n"); printf("10. Quit \n"); printf("-----------------------------\n"); printf("Enter your choice : "); scanf("%d",&ch); switch (ch) { case 1: node_creation(); break; case 2: first_insert(); break; case 3: last_insert(); break; case 4 : addatanypoint(); break; case 5 : update(); break; case 6 : deleteanynode(); break; case 7 : search(); break; case 8: display(); break; case 9 : len = length(); printf("Length is : %d\n\n",len); break; case 10 : exit(0); break; default: printf("Invalid Input.\n\n"); } } return 0; } void node_creation() { int num; printf("\nValue for Node %d--> :",takenode); takenode++; if(temp==NULL) { temp = (struct node*)malloc(sizeof(struct node)); scanf("%d",&temp->data); head=temp; } else { temp = (struct node*)malloc(sizeof(struct node)); scanf("%d",&temp->data); last->link=temp; } last=temp; last->link = NULL; printf("Node created successfully\n"); } void first_insert() { temp = (struct node*)malloc(sizeof(struct node)); printf("Enter your Value :"); scanf("%d",&temp->data); temp->link=head; head=temp; printf("Value successfully Inserted at the Beginning\n"); } void last_insert() { temp = (struct node*)malloc(sizeof(struct node)); printf("Enter value "); scanf("%d",&temp->data); last->link=temp; temp->link=NULL; last=temp; printf("Value successfully Inserted at the End\n"); } void addatanypoint() { int loc; struct node* temp; struct node* p; printf("\nEnter location after which, the node will be inserted : \n"); scanf("%d",&loc); len = length(); if(loc>length()) { printf("\nInvalid input position... :( \nCurrent list is having %d nodes only\n\n",length()); } else { int i=1; p = head; while(ilink; i++; } temp = (struct node*)malloc(sizeof(struct node)); printf("enter new node data: \n"); scanf("%d",&temp->data); temp->link=p->link; //right connection p->link=temp; // left connection } } void update() { temp=head; int value,check=0; printf("Enter which value you want to update :\n"); scanf("%d",&value); while(temp!=NULL) { if(temp->data == value){ check++; break; } temp=temp->link; } if(check==0) printf("Sorry, your value is not found\n"); else{ printf("Value is found. Enter the new Value : \n"); scanf("%d",&temp->data); printf("Value updated successfully\n"); } } void search() { struct node* temp; temp=head; int value,check=0; printf("\nEnter which value you want to search :\n"); scanf("%d",&value); while(temp!=NULL) { if(temp->data == value){ check++; } temp=temp->link; } if(check==0) printf("\nValue not found\n"); else printf("\nValue found %d times :)\n",check); } void display() { temp = head; if(temp==NULL) { printf("\nNo value in the list\n\n"); } else{ while(temp!=NULL) { printf("%d\t",temp->data); temp = temp->link; } printf("\n\n"); } } int length() { int count = 0; struct node* temp; temp = head; if(temp==NULL) { printf("\nNo value in the list\n\n"); } while(temp!=NULL) { count++; temp = temp->link; } return count; } void deleteanynode() { int loc; printf("Enter the node number you want to delete : "); scanf("%d",&loc); if(loc>length()) printf("Invalid position input\n"); else if (loc==1) { temp=head; head =temp->link; temp->link=NULL; free(temp); } else { int i=1; struct node* p=head; struct node* q; while(ilink; i++; } q=p->link; p->link=q->link; q->link=NULL; free(q); } }