Linked List : Add, modify ,deletion,search

L4 – Linked List :All operations

Write a C program to add a node after a particular position in the Linked List.
Define a structure
struct node
{
int data;
struct node * link;
}
Dont use a global variable for storing count … Each function will be checked individually….
Include functions
  • append — to add data at the end of the linked list.
  • addafter — to add data after a particular position. The second parameter is position and third parameter is number(dataitem) . The first element in the linked list is at position 0. If the position p is invalid, the function should print — There are less than p elements in the list.
  • display — to display all the data in the linked list.
Refer function specifications for further details.
Input and Output Format:
Refer sample input and output for formatting specifications.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output.]
Enter the value
10
Do you want to append another node? Type Yes/No
Yes
Enter the value
17
Do you want to append another node? Type Yes/No
Yes
Enter the value
11
Do you want to append another node? Type Yes/No
Yes
Enter the value
28
Do you want to append another node? Type Yes/No
No
The elements in the linked list are 10 17 11 28
Enter the position after which you want to add another node
2
Enter the value
8
The elements in the linked list are 10 17 11 8 28
Do you want to add another node after a certain position? Type Yes/No
Yes
Enter the position after which you want to add another node
8
Enter the value
15
There are less than 8 elements in the linked list
The elements in the linked list are 10 17 11 8 28
Do you want to add another node after a certain position? Type Yes/No

No

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node {
          int data;
          struct node * link;
};
void append ( struct node **, int ) ;
void display ( struct node * ) ;
void addafter ( struct node *, int, int ) ;
int main() {
          struct node *p ;
          p=NULL;
          int n,l;
          char ch[10];
          do {
                   printf(“Enter the valuen”);
                   scanf(“%d”,&n);
                   append(&p,n);
                   printf(“Do you want to append another node? Type Yes/Non”);
                   scanf(“%s”,ch);
          }while(!strcmp(ch,”Yes”));
          printf(“The elements in the linked list are”);
          display(p);
          printf(“n”);
          do {
                   printf(“Enter the position after which you want to add another noden”);
                   scanf(“%d”,&l);
                   printf(“Enter the valuen”);
                   scanf(“%d”,&n);
                   addafter(p,l,n);
                   printf(“The elements in the linked list are”);
                   display(p);
                   printf(“n”);
                   printf(“Do you want to add another node after a certain position? Type Yes/Non”);
                   scanf(“%s”,ch);
          }while(!strcmp(ch,”Yes”));
          return 0;
}
void append ( struct node **q, int num ) {
   
          struct node *nn=(struct node *)malloc(sizeof(struct node));
          nn->data=num;
          nn->link=NULL;
          if(*q==NULL)
              *q=nn;
           else
           {
               struct node *temp=*q;
               while(temp->link!=NULL)
               {
                   temp=temp->link;
               }
               temp->link=nn;
           }
}
void display ( struct node *q ) {
    struct node *temp=q;
    while(temp!=NULL)
        {
            printf(” %d”,temp->data);
            temp=temp->link;
        }
}
void addafter ( struct node *q, int loc, int num ) {
                   struct node *nn=(struct node *)malloc(sizeof(struct node));
                   nn->data=num;
                   int count=0,c=0;
                   struct node *temp=q;
                   while(temp!=NULL)
                       {
                           c++;
                           temp=temp->link;
                       }
                       temp=q;
                       if(loc>=c)
                           {
                               printf(“There are less than %d elements in the linked listn”,loc);
                           }
                         else{
                            
                    while(count<=loc-1)
                       {
                           temp=temp->link;
                           count++;
                       }
                       nn->link=temp->link;
                       temp->link=nn;
        }}
Add a node at Beginning:
void display ( struct node *q )
{
          struct node *temp=q;
          while(temp!=NULL)
              {
                  printf(” %d”,temp->data);
                  temp=temp->link;
              }
}
                                                              
To count the Number of  Nodes:
int count ( struct node * q ) {
          struct node *temp=q;
          int c=0;
          while(temp!=NULL)
             {
                 c++;
             temp=temp->link;
                
             }
            return c;
}
Searching a node:
int search ( struct node *q, int e ) {
          struct node *temp=q;
          int flag=0;
          while(temp!=NULL)
          {
              if(temp->data==e)
                  {flag=1;break;}
                  temp=temp->link;
          }
          if(flag==1)
              return 1;
            else
              return 0;
}
Deletion :
void delete_end(struct node **q) {
          struct node *temp=*q;
          while(temp->link->link!=NULL)
          {
              temp=temp->link;
          }
          temp->link=NULL;
         
}
void delete _begining(struct node **q) {
          struct node *temp=*q;
          *q=temp->link;
}
Find Maximum in a list
int findmax ( struct node *q ) {
          struct node *temp=q;
          int max=-999;
          while(temp!=NULL)
          {
              if(max<temp->data)
                  max=temp->data;
             temp=temp->link;
          }
          return max;
}

Please do comment If u have any Queries!

Add a Comment

Your email address will not be published. Required fields are marked *