A linked list is a kind of linear data structure in which the data is stored in a non-contiguous memory location.
It is a type of linked list in which traversing occurs only in one direction from the head to the end node. Every node contains data and the pointer which contains the address of the next node.
Example:
The most common type of linked list is a single linked list. A singly linked list can only be traversed in one direction. Each node in a singly linked list has data as well as a pointer to the next node.
Here, every node points to the next node, the last node is a pointer to NULL for the end of the list and the head is pointing to the first node.
- Type declaration of a Linked List of Integer
- Implementation of Singly Linked List
- Traversing the Singly Linked List
- Insertion of a Node at the Beginning of the List
- Insertion of a Node at the End of the List
- Insertion of a New Node at the Given Position
- Deletion of a Node from the Beginning of the List
- Deletion of a Node from the End of the List
- Deletion of a Node from the Given Position
- Search a Node Data in the List
- Update a Node Data
We have declared the type of linked list using the structure in C language.
Node Syntax:
struct node { int data; struct node* next; }; struct node* head = NULL;
Structure of a Node
A node in a single linked list is made up of two parts: the data node and the link part.
Here, in the above declaration, the first field will store the data of the node and the second field will store the address of the next node.
Here is source code of the C program to implement the singly linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C program to Implement the Singly Linked List */ #include<stdio.h> #include<malloc.h> //type declaration of a node struct node { int data; struct node* next; }; //global head pointer struct node* head = NULL; //prototyping of the functions struct node* create_node(int); void insert_at_beginning(int); void insert_at_end(int); void insert_at_position(int, int); void delete_at_beginning(); void delete_at_end(); void delete_at_position(int); void print_from_beginning(); void print_from_end(struct node*); void search_data(int); void update_node_data(int, int); void empty_message(void); int size_of_list(); int getData(); int getPosition(); int main() { char user_active = 'Y'; int user_choice; int data, position; while(user_active == 'Y' || user_active == 'y') { printf("\n\n------ Singly Linked List -------\n"); printf("\n1. Insert a node at beginning"); printf("\n2. Insert a node at end"); printf("\n3. Insert a node at given position"); printf("\n\n4. Delete a node from beginning"); printf("\n5. Delete a node from end"); printf("\n6. Delete a node from given position"); printf("\n\n7. Print list from beginning"); printf("\n8. Print list from end"); printf("\n9. Search a node data"); printf("\n10. Update a node data"); printf("\n11. Exit"); printf("\n\n------------------------------\n"); printf("\nEnter your choice: "); scanf("%d", &user_choice); printf("\n------------------------------\n"); switch(user_choice) { case 1: printf("\nInserting a node at beginning"); data = getData(); insert_at_beginning(data); break; case 2: printf("\nInserting a node at end"); data = getData(); insert_at_end(data); break; case 3: printf("\nInserting a node at the given position"); data = getData(); position = getPosition(); insert_at_position(data, position); break; case 4: printf("\nDeleting a node from beginning\n"); delete_at_beginning(); break; case 5: printf("\nDeleting a node from end\n"); delete_at_end(); break; case 6: printf("\nDelete a node from given position\n"); position = getPosition(); delete_at_position(position); break; case 7: printf("\nPrinting the list from beginning\n\n"); print_from_beginning(); break; case 8: printf("\nPrinting the list from end\n\n"); print_from_end(head); break; case 9: printf("\nSearching the node data"); data = getData(); search_data(data); break; case 10: printf("\nUpdating the node data"); data = getData(); position = getPosition(); update_node_data(data, position); break; case 11: printf("\nProgram was terminated\n\n"); return 0; default: printf("\n\tInvalid Choice\n"); } printf("\n...............................\n"); printf("\nDo you want to continue? (Y/N) : "); fflush(stdin); scanf(" %c", &user_active); } return 0; } /* * Function will show an empty list message */ void empty_message() { printf("\n\tList is Empty!\n"); } /* * Function is used to show the memory allocation failure */ void memory_message() { printf("\nMemory can't be allocated\n"); } /* * Creates a new node and returns the address of that node */ struct node* create_node(int data) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); if(new_node == NULL) { memory_message(); return NULL; } new_node->data = data; new_node->next = NULL; return new_node; } /* * Insert the new node at the beginning of the list */ void insert_at_beginning(int data) { struct node* new_node = NULL; new_node = create_node(data); if(new_node != NULL) { new_node->next = head; head = new_node; printf("\n* Node with data %d was Inserted\n", data); } } /* * Insert the new node at the end of the list */ void insert_at_end(int data) { struct node* new_node = NULL; new_node = create_node(data); if(new_node != NULL) { //if list is empty if(head == NULL) { head = new_node; } else { struct node* last = head; //getting the last node while(last->next != NULL) { last = last->next; } //link the last node next pointer to the new node last->next = new_node; } printf("\n* Node with data %d was Inserted\n", data); } } /* * Insert the new node at the given position */ void insert_at_position(int data, int pos) { //calculate the size of the list int list_size = 0; list_size = size_of_list(); //if the list is empty and the position is greater than the 1 if(head == NULL && (pos <= 0 || pos > 1)) { printf("\nInvalid position to insert a node\n"); return; } // if the list is not empty and the position is out of range if(head != NULL && (pos <= 0 || pos > list_size)) { printf("\nInvalid position to insert a node\n"); return; } struct node* new_node = NULL; new_node = create_node(data); if(new_node != NULL) { struct node* temp = head; //getting the position-1 node int count = 1; while(count < pos-1) { temp = temp -> next; count += 1; } //if the position is 1 then insertion at the beginning if(pos == 1) { new_node->next = head; head = new_node; } else { new_node->next = temp->next; temp->next = new_node; } printf("\n* Node with data %d was Inserted\n", data); } } /* * Delete the node from the beginning of the list */ void delete_at_beginning() { if(head == NULL) { empty_message(); return; } struct node* temp = head; int data = head->data; //move head pointer to the next node to the head head = head->next; free(temp); printf("\n* Node with data %d was Deleted\n", data); } /* * Delete the node from the ending of the list */ void delete_at_end() { if(head == NULL) { empty_message(); return; } struct node* temp = head; struct node* prev = NULL; int data; //reaching the last node while(temp->next != NULL) { prev = temp; temp = temp->next; } data = temp->data; //if there is only one node if(temp == head) { free(temp); head = NULL; } else { free(temp); prev->next = NULL; } printf("\n* Node with data %d was Deleted\n", data); } /* * Deleting the node from the given position */ void delete_at_position(int pos) { //calculate the size of the list int list_size = 0; list_size = size_of_list(); // if the position is out of range if(pos <= 0 || pos > list_size) { printf("\nInvalid position to delete a node\n"); return; } struct node* temp = head; struct node* prev = NULL; int count = 1; while(count < pos) { prev = temp; temp = temp->next; count += 1; } int data = temp->data; if(temp == head) { head = head->next; free(temp); } else { prev->next = temp->next; free(temp); } printf("\n* Node with data %d was Deleted\n", data); } /* * Search the node with given data in the list */ void search_data(int data) { int position = 0; int flag = 0; struct node* temp = head; while(temp != NULL) { position += 1; if(temp->data == data) { flag = 1; break; } temp = temp->next; } if(flag == 0) { printf("\nNode with data %d was not found!\n", data); } else { printf("\nFound data at %d position\n", position); } } /* * Update the node with the given new data */ void update_node_data(int new_data, int pos) { //calculate the size of the list int list_size = 0; list_size = size_of_list(); // if the position is out of range if(pos <= 0 || pos > list_size) { printf("\nInvalid position to update a node\n"); return; } struct node* temp = head; int count = 1; while(count < pos) { temp = temp->next; count += 1; } temp->data = new_data; printf("\nUpdated node data is %d\n", new_data); } /* * Prints the data from the start of the list */ void print_from_beginning() { if(head == NULL) { empty_message(); return; } struct node* temp = head; while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } /* * Prints the list from the end of the list */ void print_from_end(struct node* head) { if(head == NULL) { return; } print_from_end( head->next ); printf("%d ", head->data); } /* * Returns the size of the list */ int size_of_list() { struct node* temp = head; int count = 0; while(temp != NULL) { count += 1; temp = temp->next; } return count; } /* * Getting node data from the user */ int getData() { int data; printf("\n\nEnter Data: "); scanf("%d", &data); return data; } /* * Getting the position of the node from the user */ int getPosition() { int pos; printf("\nEnter Position: "); scanf("%d", &pos); return pos; }
In the above program, we have used different kinds of functions to operate the Singly linked list.
Here, we discuss the main operations in depth, including their complexities.
Traversing the singly linked list requires the following steps:
- Take any temporary node type of pointer temp and point it to the head pointer.
- Start iterating using temp until it does not reach the NULL value.
- Print the data of that node which is pointing by the temp node.
Time Complexity: O(n)
Traversing requires n time, where n is the size of the list. So, the time complexity of traversing the singly linked list is O(n).
Space Complexity: O(1)
Here, only constant space is used for iteration of the list. Therefore the space complexity is O(1).
There are two steps to insert the data at the beginning:
- Update the next pointer of the new node to the head node.
- Update head pointer to the new node.
Example: Let’s insert a new node with data 10 at the beginning of the given list.
Step 1: Updating the next of the new node to head node
Step 2: Updating the head pointer to the new node
Time Complexity: O(1)
Because we are not traversing the list, the time complexity for adding a node to a singly linked list is O(1).
Space Complexity: O(1)
Because only temporary variables are used, the space complexity is O(1).
In this case, we are inserting the nodes from the beginning of the singly linked list.
------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 1 ------------------------------ Inserting a node at beginning Enter Data: 90 * Node with data 90 was Inserted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 1 ------------------------------ Inserting a node at beginning Enter Data: 45 * Node with data 45 was Inserted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 45 90 ............................... Do you want to continue? (Y/N) : N
To insert the new node at the end of the list we have to take the following steps:
- Traverse the list to the last node.
- Update the next pointer of the last node to the new node.
Example: Inserting a new node with data 88 at the end of the list.
Step 1: Traversing the list, and reach at the last node
Step 2: Update the next of the temp to the new node
Time Complexity: O(n)
we are traversing from start to end, which means the size of the list n. So the time complexity is O(1).
Space Complexity: O(1)
Because only temporary variables are used, the space complexity is O(1).
In this case, we are inserting the nodes from the end of the singly linked list.
------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 2 ------------------------------ Inserting a node at end Enter Data: 67 * Node with data 67 was Inserted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 45 90 67 ............................... Do you want to continue? (Y/N) : N
Insertion of the new node at the middle of the list needs the following steps:
- Traverse to the list till given position using temp pointer and move another pointer prev to its just previous node.
- Update the next pointer of the previous node to the new node.
- Change the next pointer of the new node to the temp node.
Example: Inserting a new node with data 34 at position 3 in the given list.
Step 1: Traverse the list and reach at the given position
Step 2: Update the next of the prev to the new node
Step 3: Update the next of the new node to temp node
Time Complexity: O(n)
In the worst case, the traversing may reach the end of the list. Therefore the time complexity is O(n).
Space Complexity: O(1)
Here, only constant space is used for iteration of the list. Therefore the space complexity is O(1).
In this case, we are inserting the nodes at the particular position in the list.
------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 3 ------------------------------ Inserting a node at the given position Enter Data: 12 Enter Position: 2 * Node with data 12 was Inserted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 45 12 90 67 ............................... Do you want to continue? (Y/N) : N
To delete the node from the beginning of the list we have to take the following steps:
- Store the head node to any other temporary pointer.
- Move head to its next node.
- free the memory of the temporary pointer.
Example: Deleting the node from the beginning in the given list.
Step 1: Store the head node to the temp and move head to its next node
Step 2: Free the memory for temp
Time Complexity: O(1)
It takes a constant amount of time because no iteration occurred. So, the time complexity will be O(1).
Space Complexity: O(1)
Because of the temporary variables, only constant space is used here. As a result, the space complexity is O(1).
In this case, we are deleting the nodes from the beginning of the singly linked list.
------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 45 12 90 67 ............................... Do you want to continue? (Y/N) : Y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 4 ------------------------------ Deleting a node from beginning * Node with data 45 was Deleted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 12 90 67 ............................... Do you want to continue? (Y/N) : N
This operation takes the following steps to delete the node:
- Traverse to the end of the node using the temp pointer and use another pointer prev which pointer previous node of the temp pointer.
- Update the next pointer field of the prev to NULL.
- Free the memory of the temp pointer.
Example: Deleting the last node of the given list.
Step 1: Traverse the list at the one node before the last node
Step 2: Update the next of temp node to NULL and free the last node
Time Complexity: O(n)
We are traversing a list of size n. The time complexity of deleting the last node in the given list is O(n).
Space Complexity: O(1)
Here, only constant space is used for iteration of the list. Therefore the space complexity is O(1).
In this case, we are deleting the nodes from the end of the singly linked list.
------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 12 90 67 ............................... Do you want to continue? (Y/N) : Y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 5 ------------------------------ Deleting a node from end * Node with data 67 was Deleted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 8 ------------------------------ Printing the list from beginning 90 12 ............................... Do you want to continue? (Y/N) : N
Deleting a node at the given position needs the following steps:
- Traverse the list to the given position node using the temp pointer and use another previous pointer prev which points to the previous node of the temp pointer.
- Update the next pointer of the prev to the next of the temp pointer.
- Free the memory of the temp pointer.
Example: Deleting the node at position 3 in the given list.
Step 1: Traverse the list at the given position
Step 2: Update the next of prev node to next of temp node
Step 3: Free the memory of the temp node
Time Complexity: O(n)
In the worst case, the traversing may reach the end of the list. Therefore the time complexity is O(n).
Space Complexity: O(1)
Since we are not using any extra space so it takes constant memory i.e. O(1).
In this case, we are deleting the node from the particular position of the list.
------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 12 90 ............................... Do you want to continue? (Y/N) : Y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 6 ------------------------------ Delete a node from given position Enter Position: 1 * Node with data 90 was Deleted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 12 ............................... Do you want to continue? (Y/N) : N
To find a node with the given key, we need to traverse the list from start to end and match the data with the key. If the data is found in the list then return 1 otherwise return 0.
Time Complexity: O(n)
In the worst case, it takes to traverse the entire linked list of size n to find the key.
Space Complexity: O(1)
It takes constant space to search the data in the linked list.
In this case, we are searching for a node in the list.
------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 1 ------------------------------ Inserting a node at beginning Entert Data: 67 * Node with data 67 was Inserted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 1 ------------------------------ Inserting a node at beginning Entert Data: 34 * Node with data 34 was Inserted ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 8 ------------------------------ Printing the list from end 12 67 34 ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 9 ------------------------------ Searching the node data Enter Data: 67 Found data at 2 position ............................... Do you want to continue? (Y/N) : N
Updation is the process to change the value of a node. To update the node data we have to follow the given steps:
- Traverse the list from start to the given position.
- Update the value of that node.
In this case, we are updating a node data at a particular position.
------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 34 67 12 ............................... Do you want to continue? (Y/N) : Y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 10 ------------------------------ Updating the node data Entert Data: 100 Enter Position: 1 Updated node data is 100 ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 7 ------------------------------ Printing the list from beginning 100 34 67 12 ............................... Do you want to continue? (Y/N) : y ------ Singly Linked List ------- 1. Insert a node at beginning 2. Insert a node at end 3. Insert a node at given position 4. Delete a node from beginning 5. Delete a node from end 6. Delete a node from given position 7. Print list from beginning 8. Print list from end 9. Search a node data 10. Update a node data 11. Exit ------------------------------ Enter your choice: 11 ------------------------------ Program was terminated
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Check Programming Books
- Check Computer Science Books
- Practice Programming MCQs
- Check Data Structure Books
- Apply for Computer Science Internship