Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node{
- int info;
- struct node* next;
- };
- typedef struct node node;
- //initalisation of the start to NULL
- node* start=NULL;
- // function that will return the available node.
- node* getnode()
- {
- node* temp=(node*)malloc(sizeof(node));
- return temp;
- }
- //function that will free the memory allocated to node passed as an argument
- void freenode(node* p)
- {
- free(p);
- }
- //function to insert the node at the beginning
- void insert_at_the_beginning(int data)
- {
- node* temp1=getnode();
- temp1->info=data;
- temp1->next=start;
- start=temp1;
- }
- //function to display the node's info
- void display()
- {
- node* temp1=start;
- while(temp1!=NULL)
- {
- printf("%d ",temp1->info);
- temp1=temp1->next;
- }
- }
- //function to search a data in a linked list.
- int Search_for_element(int data)
- { int count=0;
- node* temp=start;
- while(temp!=NULL)
- {
- if(temp->info==data)
- {
- return count+1;
- }
- count=count+1;
- temp=temp->next;
- }
- return -1;
- }
- //finding a node previous to the node whose data is passed as an argument
- node* findNode_forInsertion(int data) //valid for sorted list
- { node* loc;
- node* save;
- node* ptr;
- save=start;
- ptr=start->next;
- if(start==NULL) //empty list
- {
- loc=NULL;
- return loc;
- }
- if(data<start->info)
- {
- loc=NULL;
- return loc;
- }
- while(ptr!=NULL)
- {
- if(data<ptr->info)
- {
- loc=save;
- return loc;
- }
- save=ptr;
- ptr=ptr->next;
- }
- loc=save;
- return loc;
- }
- //function to insert the node anywhere in a linked list
- void insert_anywhere()
- { int dp;
- printf("enter the element that should be contained in a node\n");
- scanf("%d",&dp);
- struct node* loc=findNode_forInsertion(dp);
- if(loc==NULL)
- {
- insert_at_the_beginning(dp);
- return;
- }
- struct node* temp=(node*)malloc(sizeof(node));
- temp->info=dp;
- temp->next=loc->next;
- loc->next=temp;
- }
- //function to find a node previous to the node whose data is given and the node itself.
- void findNodeInDeleteCase(int data,node** loc,node** locp)
- {
- node* save;
- node* ptr;
- save=start;
- ptr=start->next;
- if(start==NULL)
- {
- *locp=NULL;
- *loc=NULL;
- return;
- }
- if(data==start->info)
- {
- *loc=start;
- *locp=NULL;
- return;
- }
- while(ptr!=NULL)
- {
- if(data==ptr->info)
- {
- *loc=ptr;
- *locp=save;
- return;
- }
- save=ptr;
- ptr=ptr->next;
- }
- *loc=ptr;
- *locp=save;
- return;
- }
- //function to delete the node whose address and the previous node's address is given.
- void deleteNode(node* locp,node* loc)
- {
- if(loc==NULL)
- printf("No node available to delete\n");
- else if(locp==NULL)
- {
- start=start->next; //deleting a first node
- freenode(loc); //call to fucnction
- }
- else
- {
- locp->next=loc->next;
- freenode(loc); //deleting a node
- }
- }
- int main()
- { int choice;
- for(;;)
- { printf("\n");
- printf("1. Insert at the beginning\n");
- printf("2. Display the current list\n");
- printf("3. Search for a element by data in the linked list\n");
- printf("4.Insert Anywhere\n");
- printf("5. Delete node\n");
- printf("Enter your choice:\n");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- int data;
- printf("Enter the data\n");
- scanf("%d",&data);
- insert_at_the_beginning(data);
- break;
- case 2:
- printf("The contents till now\n");
- display();
- break;
- case 3:{
- int d;
- printf("Enter the element that you want to search\n");
- scanf("%d",&d);
- int x;
- x=Search_for_element(d);
- if(x==-1)
- printf("Element not found in the linked list\n");
- else
- printf("Element found at: %d position\n",x+1);
- break;
- }
- case 4:{
- insert_anywhere();
- break;
- }
- case 5:{
- node* locp=NULL;
- node* loc=NULL;
- int p;
- printf("enter the node's data that need to be deleted\n");
- scanf("%d",&p);
- findNodeInDeleteCase(p,&loc,&locp);
- deleteNode(locp, loc);
- break;
- }
- case 6:
- exit(0);
- default:
- printf("entered choice is not correct.\n");
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment