Write a C Program of single linked list with header node

By | 27.03.2017

C Program of single linked list with header node


Write a C Program of single linked list with header node. Here’s simple Menu Driven C Program of single linked list with header node with operations like Creation, Insertion, Deletion, Display, Count, Add Node, Delete Node, Search, Reverse, etc.  in C Programming Language.


What is Linked List ?


Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each link contains a connection to another link.

Following are the important terms to understand the concept of Linked List.

  • Link − Each link of a linked list can store a data called an element.
  • Next − Each link of a linked list contains a link to the next link called Next.

Each element in a linked list is called as “Node”. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs.


Basic Operations


Following are the basic operations supported by a list.

  • Insertion − Adds an element at the beginning of the list.
  • Deletion − Deletes an element at the beginning of the list.
  • Display − Displays the complete list.
  • Search − Searches an element using the given key.
  • Delete − Deletes an element using the given key.

Below is the source code for C Program of single linked list with header node which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program of single linked list with header node*/


#include<stdio.h>
#include<stdlib.h>

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

struct node *create_list(struct node *head);
void display(struct node *head);
struct node *addatend(struct node *head,int data);
struct node *addbefore(struct node *head,int data,int item );
struct node *addatpos(struct node *head,int data,int pos);
struct node *del(struct node *head,int data);
struct node *reverse(struct node *head);

int main()
{
        int choice,data,item,pos;
        struct node *head;
        head=(struct node *)malloc(sizeof(struct node));
        head->info=0;
        head->link=NULL;

        head=create_list(head);

        while(1)
        {

                printf("\n\n1.Display\n");
                printf("2.Add at end\n");
                printf("3.Add before node\n");
                printf("4.Add at position\n");
                printf("5.Delete\n");
                printf("6.Reverse\n");
                printf("7.Quit\n\n");
                printf("Enter your choice : ");
                scanf("%d",&choice);
                switch(choice)
                {
                 case 1:
                        display(head);
                        break;
                 case 2:
                        printf("\nEnter the element to be inserted : ");
                        scanf("%d",&data);
                        head=addatend(head,data);
                        break;
                 case 3:
                        printf("\nEnter the element to be inserted : ");
                        scanf("%d",&data);
                        printf("Enter the element before which to insert : ");
                        scanf("%d",&item);
                        head=addbefore(head,data,item);
                        break;
                 case 4:
                        printf("\nEnter the element to be inserted : ");
                        scanf("%d",&data);
                        printf("Enter the position at which to insert : ");
                        scanf("%d",&pos);
                        head=addatpos(head,data,pos);
                        break;
                 case 5:
                        printf("\nEnter the element to be deleted : ");
                        scanf("%d",&data);
                        head=del(head,data);
                        break;
                 case 6:
                        head=reverse(head);
                        break;
                 case 7:
                         exit(1);
                 default:
                        printf("\nWrong choice\n\n");
                }/*End of switch */
        }/*End of while */

        return 0;

}/*End of main()*/

struct node *create_list(struct node *head)
{
        int i,n,data;
        printf("\nEnter the number of nodes : ");
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
                printf("Enter the element to be inserted : ");
                scanf("%d",&data);
                head=addatend(head,data);
        }
        return head;
}/*End of create_list()*/

void display(struct node *head)
{
        struct node *p;
        if(head->link==NULL)
        {
                printf("\nList is empty\n");
                return;
        }
        p=head->link;
        printf("\nList is :\n");
        while(p!=NULL)
        {
                printf("%d ",p->info);
                p=p->link;
        }
        printf("\n");
}/*End of display() */

struct node *addatend(struct node *head,int data)
{
        struct node *p,*tmp;
        tmp= (struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        p=head;
        while(p->link!=NULL)
                p=p->link;
        p->link=tmp;
        tmp->link=NULL;
        return head;
}/*End of addatend()*/

struct node *addbefore(struct node *head,int data,int item)
{
        struct node *tmp,*p;
        p=head;
        while(p->link!=NULL)
        {
                if(p->link->info==item)
                {
                        tmp=(struct node *)malloc(sizeof(struct node));
                        tmp->info=data;
                        tmp->link=p->link;
                        p->link=tmp;
                        return head;
                }
                p=p->link;
        }
        printf("%d not present in the list\n",item);
        return head;
}/*End of addbefore()*/

struct node *addatpos(struct node *head,int data,int pos)
{
        struct node *tmp,*p;
        int i;
        tmp=(struct node *)malloc(sizeof(struct node) );
        tmp->info=data;
        p=head;
        for(i=1;i<=pos-1;i++)
        {
                p=p->link;
                if(p==NULL)
                {
                        printf("\nThere are less than %d elements\n",pos);
                        return head;
                }
        }
        tmp->link=p->link;
        p->link=tmp;
        return head;
}/*End of addatpos()*/

struct node *del(struct node *head, int data)
{
        struct node *tmp,*p;
        p=head;
        while(p->link!=NULL)
        {
                if(p->link->info==data)
                {
                        tmp=p->link;
                        p->link=tmp->link;
                        free(tmp);
                        return head;
                }
                p=p->link;
        }
        printf("\nElement %d not found\n",data);
        return head;
}/*End of del()*/

struct node *reverse(struct node *head)
{
        struct node *prev, *ptr, *next;
        prev=NULL;
        ptr=head->link;
        while(ptr!=NULL)
        {
                next=ptr->link;
                ptr->link=prev;
                prev=ptr;
                ptr=next;
        }
        head->link=prev;
        return head;
}/*End of reverse()*/

OUTPUT : :


*************** OUTPUT *******************


Enter the number of nodes : 5
Enter the element to be inserted : 1
Enter the element to be inserted : 2
Enter the element to be inserted : 3
Enter the element to be inserted : 4
Enter the element to be inserted : 5


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 1

List is :
1 2 3 4 5


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 2

Enter the element to be inserted : 6


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 2

Enter the element to be inserted : 7


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 1

List is :
1 2 3 4 5 6 7


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 3

Enter the element to be inserted : 0
Enter the element before which to insert : 1


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 2

Enter the element to be inserted : 8


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 1

List is :
0 1 2 3 4 5 6 7 8


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 4

Enter the element to be inserted : 9
Enter the position at which to insert : 10


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 1

List is :
0 1 2 3 4 5 6 7 8 9


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 5

Enter the element to be deleted : 9


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 1

List is :
0 1 2 3 4 5 6 7 8


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 6


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 1

List is :
8 7 6 5 4 3 2 1 0


1.Display
2.Add at end
3.Add before node
4.Add at position
5.Delete
6.Reverse
7.Quit

Enter your choice : 7

Process returned 1

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments