Linked list C program

#include
#include
struct node{
       int info;
       struct node *link;
       };

       void display(struct node *start);
       struct node *addatbeg(struct node *start, int num);
       void addatend(struct node *start, int num);
       struct node *del(struct node *start, int num);
       void search(struct node *start, int num2);
       
       main()
       {
             struct node *start=NULL;
             int choice, num, num2;
             
             
             while(1)
             {
                     printf("n1. Displayn");
                     printf("2. Add node to empty list / Add at beginningn");
                     printf("3. Add node at the endn");
                     printf("4. Delete from listn");
                     printf("5. Exitnn");

                     scanf("%d",&choice);
                     switch(choice)
                     {
                    case 1:
                        display(start);
                        break;
                    case 2:
                        printf("Enter the number to be insertedn");
                        scanf("%d",&num);
                        start = addatbeg(start, num); 
                        break;
                    case 3:
                        printf("Enter the number to be insertedn");
                        scanf("%d",&num);
                        addatend(start, num);
                        break;
                    case 4:
                        printf("Enter the number to be deletedn");
                        scanf("%d",&num);
                        start=del(start, num);
                        break;
                    case 5:
                        exit(1);

                        default:
                        printf("Invalid Input");
                     }                  
                     
         }
       
                    
}

void display(struct node *start)
{
     struct node *p;
     if(start==NULL)
     {
                    printf("nList is emptyn");
                    return;
     }
     p=start;
     printf("List is : n");
     while(p != NULL)
     {
                   printf("%d ", p->info);
                   p=p->link;
     }
}


 struct node *addatbeg(struct node *start, int num)
 {
        struct node *temp;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->info=num;
        temp->link = start;
        start = temp;
        return start;
}
 
 void addatend(struct node *start, int num)
 {
        struct node *temp, *p;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->info=num;
        p=start;
        
        while(p->link!=NULL)
        {
           p = p->link;
        }
           
           p->link = temp;
           temp->link = NULL;
         
}


struct node *del(struct node *start,int num)
{
       struct node *tmp,*p;
       if(start==NULL)
       {
                      printf("List is emptyn");
                      return start;
       }
       
       if(start->info==num)         /*deletion from first or only node*/
       {
         tmp=start;
         start=start->link;
         free(tmp);
         return start;
       }            
       p=start;
       while(p->link!=NULL)
       {
                     if(p->link->info==num)
                     {
                                          tmp = p->link;
                                          p->link = tmp->link;
                                          free(tmp); 
                                          return start;    
                     }
                     p=p->link;
       }
       
       printf("%d not presentn",num);
       return start;
}
Previous Post
Linked List tutorial
Next Post
Linked list function to count the number of nodes

Related Posts

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu