Write a C
program to implement the operations on a singly linked list. The program should
include the following operations.
i. Creation of linked list
ii. Traversal of linked list
iii.Insertion of element---
( a). At the
beginning of list
( b).At the end of list
( c).Before a given node
( d). After a given node
( e). At a given position
iv.
Deletion of a node---
(a). First node
(b)Last node
(c).
Node with a particular value
(d). Node at a given position
CODING in C:-
- #include<stdio.h>
- #include<malloc.h>
- //Definition of the data structure
- struct node
- {
- //parts of node
- int data;
- struct node *next;
- };
- //Rename of the data structure
- typedef struct node node;
- //Function prototype declarations
- node *createlist (node *head);
- void traverse (node *head);
- node *insert (node *head);
- node *insertbegin(node *head, int d);
- node *insertend(node *head, int d);
- node *insertbefore(node *head, int d);
- node *insertafter(node *head, int d);
- node *insertatparticularposition(node *head, int d);
- node *delet(node *head);
- node *deletbegin(node *head);
- node *deletend(node *head);
- node *deletvalue(node *head);
- node *deletatparticularposition(node *head);
- int main()
- {
- //Declaration of variable
- char cont;
- node *head;
- //Allocation memory dynamically for the variable
- head = (node *)malloc(sizeof(node));
- do
- {
- //Displaying and calling the functions
- printf("\nCreation of a Linked List");
- head = createlist(head);
- printf("\nContents of Linked List : \n");
- traverse(head);
- printf("\nInsertion of a new node : \n");
- head = insert(head);
- printf("\nDeletion of Node : \n");
- head = delet(head);
- printf("\n Do you want to continue ? (Y or N) : ");
- scanf("%s",&cont);
- }while(cont == 'Y' || cont =='y');
- }
- //Function to create a linked list
- node *createlist (node *head)
- {
- //Declaration of variables
- int n, i;
- node *list;
- //Inputt total number
- printf("\nEnter total number of elements in the list : ");
- scanf("%d", &n);
- //Initioalizing the list by head pointer
- list = head;
- for ( i = 0; i<n; i++)
- {
- printf("Enter data :");
- scanf("%d", &list->data);
- if ( i == n-1)
- list->next = NULL;
- else
- {
- list->next=(node *)malloc(sizeof(node));
- list = list->next;
- }
- }
- return head;
- }
- //Function to traverse a linked list
- void traverse (node *head)
- {
- //Declaration of variables
- node *list;
- //Initializing the list by pointer head
- list = head;
- while (list->next != NULL)
- {
- printf("%d -> ",list->data);
- list = list -> next;
- }
- printf("%d\n", list->data);
- }
- //Function to insert nodes in a linked list
- node *insert (node *head)
- {
- //declaring variables
- int element, option;
- //Inputt the data to insert
- printf("Enter data to insert :");
- scanf("%d", &element);
- //Displaying insertion options
- printf("Insertion options:");
- printf("\n1. At the beginning");
- printf("\n2. At the end");
- printf("\n3. Before a key node");
- printf("\n4. After a key node");
- printf("\n5. At a particular position");
- printf("\nEnter option : ");
- //Inputt the insertion option
- scanf("%d", &option);
- switch(option)
- {
- //Calling of functions according to the oprtions
- case 1:
- head = insertbegin(head, element);
- break;
- case 2:
- head = insertend(head, element);
- break;
- case 3:
- head = insertbefore(head, element);
- break;
- case 4:
- head = insertafter(head, element);
- break;
- case 5:
- head = insertatparticularposition(head, element);
- break;
- //Printing the default case
- default:
- printf("\nInvalid choice. Insertion not done.");
- }
- printf("\nContents of Linked List after Insertion: \n");
- traverse(head);
- return head;
- }
- //Function to insert node at the begining
- node *insertbegin(node *head, int d)
- {
- //Declaration of variable
- node * tempnode;
- //Allocating memory dynamically for the new node
- tempnode = (node *)malloc(sizeof(node));
- //Insert the new node at the begining
- tempnode->data = d;
- tempnode->next = head;
- head = tempnode;
- return head;
- }
- //Function to insert node at the end
- node *insertend(node *head, int d)
- {
- //Declaration of variables
- node *tempnode, *list;
- //Allocating memory dynamically for the new node
- tempnode = (node *)malloc(sizeof(node));
- //Inputt the data into the new node
- tempnode->data = d;
- tempnode->next = NULL;
- list = head;
- while (list -> next != NULL)
- list = list -> next;
- list -> next = tempnode;
- return head;
- }
- //Function to insert node before a particular node
- node *insertbefore(node *head, int d)
- {
- //Declaration and initialization of variables
- int key;
- int found = 0;
- node *tempnode, *list;
- //Allocating memory dynamically for the new node
- tempnode = (node *)malloc(sizeof(node));
- //Inputt the data into the new node
- tempnode->data = d;
- printf("Enter value of key :") ;
- scanf("%d",&key);
- if ( head -> data == key)
- {
- //Insertion of the new node at the beginging
- tempnode -> next = head;
- head = tempnode;
- return head;
- }
- list = head;
- while (found == 0 && list -> next != NULL)
- {
- if (list -> next -> data == key)
- {
- tempnode -> next = list -> next;
- list -> next = tempnode;
- found = 1;
- }
- else
- list = list -> next;
- }
- if ( found == 0)
- printf ("Key Node not found!");
- return head;
- }
- //Function to insert node after a particular node
- node *insertafter (node *head, int d)
- {
- //Initialization and declaration of variables
- int key;
- int found = 0;
- node *list, *tempnode;
- //Allocating memory dynamically for the new node
- tempnode = (node *)malloc(sizeof(node));
- tempnode->data = d;
- printf("Enter value of key :") ;
- scanf("%d",&key);
- list = head;
- while (found == 0 && list != NULL)
- {
- if (list -> data == key)
- {
- tempnode -> next = list -> next;
- list -> next = tempnode;
- found = 1;
- }
- else
- list = list -> next;
- }
- if ( found == 0)
- printf ("Key Node not found!");
- return head;
- }
- //Function to insert a node at a particular position
- node *insertatparticularposition(node *head, int d)
- {
- //Declaration and initialization of variables
- int keyposition;
- int currentposition=1;
- int validposition=0;
- node *list, *tempnode;
- //Dynamically allocation of memory for the new node
- tempnode = (node *)malloc(sizeof(node));
- tempnode->data = d;
- //Input value of keynode
- printf("Enter the position :") ;
- scanf("%d",&keyposition);
- list=head;
- //If the position is in first location
- if(keyposition==0)
- {
- //Insert the new node at the begining
- tempnode -> next = head;
- head=tempnode;
- return head;
- }
- //Inserting the new node at the particular location
- while(validposition==0 && list !=NULL)
- {
- if(currentposition==keyposition)
- {
- tempnode -> next = list -> next;
- list -> next = tempnode;
- validposition=1;
- }
- else
- {
- currentposition=currentposition+1;
- list=list -> next;
- }
- }
- if(validposition==0)
- {
- printf("Invalid Position.");
- }
- return head;
- }
- //Function to delete the nodes of a linked list
- node *delet(node *head)
- {
- //Initializing the option variable
- int option;
- //Displaying the deletion options
- printf("Deletion options:");
- printf("\n1. Deletion of First Node");
- printf("\n2. Deletion of Last Node");
- printf("\n3. Deletion of node with a particular value");
- printf("\n4. Deletion of a node at a given position");
- printf("\nEnter option : ");
- scanf("%d", &option);
- switch(option)
- {
- case 1:
- head = deletbegin(head);
- break;
- case 2:
- head = deletend(head);
- break;
- case 3:
- head = deletvalue(head);
- break;
- case 4:
- head = deletatparticularposition(head);
- break;
- //Diplaying the default option
- default:
- printf("\nInvalid choice. Deletion not done.");
- }
- printf("\nContents of Linked List after Deletion: \n");
- traverse(head);
- return head;
- }
- //Function to delete the first node of the list
- node *deletbegin(node *head)
- {
- //Declaring the variables
- node *delnode, *list;
- //If list does not exist
- if (head == NULL)
- {
- printf("Empty List");
- return NULL;
- }
- //initializing the variable delnode with head
- delnode = head;
- //Deleting the first node
- head = head -> next;
- //Deallocating the deleted node
- free(delnode);
- return head;
- }
- //Function to delete the last node if a list
- node *deletend(node *head)
- {
- //Declaring the variables
- node *delnode, *list;
- //If the list does not exist
- if (head == NULL)
- {
- printf("Empty List");
- return NULL;
- }
- //If the node to be deleted is the first node
- if (head -> next == NULL)
- {
- //Deleting the node
- delnode=head;
- head = NULL;
- free(delnode);
- return(head);
- }
- list = head;
- //Deleting the node
- while( list -> next -> next != NULL )
- list = list -> next;
- delnode = list -> next;
- list -> next = NULL;
- //Deallocating the deleted node
- free(delnode);
- return(head);
- }
- //Function to delete a node with a particular value
- node *deletvalue(node *head)
- {
- //Declaration and initialization of variables
- int key, found = 0;
- node *delnode, *list;
- //Inputt the value
- printf("Enter value of node that you want to delete : ");
- scanf("%d", &key);
- //If the list is empty
- if (head == NULL)
- {
- printf("Empty List");
- return NULL;
- }
- //If the first node is the node with the value
- if (head -> data == key)
- {
- //Deleting the node
- delnode=head;
- head = head-> next;
- free(delnode);
- return(head);
- }
- list = head;
- //Deletion of that particular valued node
- while(list -> next != NULL && found == 0)
- {
- if(list -> next -> data == key)
- {
- found = 1;
- delnode=list->next;
- list->next = list -> next -> next;
- //Deallocating the node
- free(delnode);
- return(head);
- }
- list=list->next;
- }
- if(found==0)
- printf("Node not found");
- return(head);
- }
- //Deleting a node at a particular position
- node *deletatparticularposition(node *head)
- {
- //Declaration and initialization of variables
- int pos,cnt=1;
- //Input the value
- printf("Enter position of node that you want to delete : ");
- scanf("%d", &pos);
- node *p=head,*tmp;
- //To check valid position
- if(pos<1 || pos>countNodes(head))
- printf("\nWrong position value, try again:");
- else
- {
- if(pos==1)//if position is 1
- {
- p=head;
- head=head->next;
- }
- else//if position is more than 1
- {
- while(cnt<pos)
- {
- cnt++;
- tmp=p;
- p=p->next;
- }
- tmp->next=p->next;//insert node
- }
- free(p);
- }
- return head;
- }
- int countNodes(node *head)
- {
- //Declaration and initialization of variables
- int count=0;
- node *p;
- p=head;
- //Count the number of nodes
- while(p!=NULL)
- {
- count++;
- p=p->next;
- }
- return(count);
- }
OUTPUTS :-
OUTPUT 1 :-
Creation of a Linked List
Enter total number of elements in
the list : 3
Enter data :23
Enter data :34
Enter data :45
Contents of Linked List :
23 -> 34 -> 45
Insertion of a new node :
Enter data to insert :12
Insertion options:
1. At the beginning
2. At the end
3. Before a key node
4. After a key node
5. At a particular position
Enter option : 1
Contents of Linked List after
Insertion:
12 -> 23 -> 34 -> 45
Deletion of Node :
Deletion options:
1. Deletion of First Node
2. Deletion of Last Node
3. Deletion of node with a
particular value
4. Deletion of a node at a given
position
Enter option : 1
Contents of Linked List after
Deletion:
23 -> 34 -> 45
OUTPUT 2 :-
Creation of a Linked List
Enter total number of elements in
the list : 3
Enter data :12
Enter data :23
Enter data :34
Contents of Linked List :
12 -> 23 -> 34
Insertion of a new node :
Enter data to insert :45
Insertion options:
1. At the beginning
2. At the end
3. Before a key node
4. After a key node
5. At a particular position
Enter option : 2
Contents of Linked List after
Insertion:
12 -> 23 -> 34 -> 45
Deletion of Node :
Deletion options:
1. Deletion of First Node
2. Deletion of Last Node
3. Deletion of node with a
particular value
4. Deletion of a node at a given
position
Enter option : 2
Contents of Linked List after
Deletion:
12 -> 23 -> 34
OUTPUT 3:-
Creation of a Linked List
Enter total number of elements in
the list : 3
Enter data :12
Enter data :23
Enter data :45
Contents of Linked List :
12 -> 23 -> 45
Insertion of a new node :
Enter data to insert :34
Insertion options:
1. At the beginning
2. At the end
3. Before a key node
4. After a key node
5. At a particular position
Enter option : 3
Enter value of key :45
Contents of Linked List after
Insertion:
12 -> 23 -> 34 -> 45
Deletion of Node :
Deletion options:
1. Deletion of First Node
2. Deletion of Last Node
3. Deletion of node with a
particular value
4. Deletion of a node at a given
position
Enter option : 3
Enter value of node that you want
to delete : 45
Contents of Linked List after
Deletion:
12 -> 23 -> 34
OUTPUT 4:-
Creation of a Linked List
Enter total number of elements in
the list : 3
Enter data :12
Enter data :23
Enter data :34
Contents of Linked List :
12 -> 23 -> 34
Insertion of a new node :
Enter data to insert :31
Insertion options:
1. At the beginning
2. At the end
3. Before a key node
4. After a key node
5. At a particular position
Enter option : 4
Enter value of key :23
Contents of Linked List after
Insertion:
12 -> 23 -> 31 -> 34
Deletion of Node :
Deletion options:
1. Deletion of First Node
2. Deletion of Last Node
3. Deletion of node with a
particular value
4. Deletion of a node at a given
position
Enter option : 4
Enter position of node that you
want to delete : 4
Contents of Linked List after
Deletion:
12 -> 23 -> 31
0 comments:
Post a Comment