Representation of a Linked List in C

A linked list is represented as a pointer to the first node where each node contains:

  • Data: Here the actual information is stored.
  • Next: Pointer that links to the next node.

Linked List

Linked List in C

A linked list is a linear data structure that does not store elements in contiguous memory locations, the elements in the linked list are linked using pointers. It is a collection of nodes in which each node stores data and a pointer that links to the next node to form a sequence of nodes.

In this article, we will learn about the linked list, its types, representation of the linked list in C, and also the basic and efficient operations that can be performed on a linked list as well as its applications.

Similar Reads

Types of Linked List in C

Following are the types of linked list in C:...

What is Singly linked list in C?

A linked list or singly linked list is a non-primitive, linear data structure that is made up of a group of nodes in which each node has two parts: the first is data, and the second is a pointer. The pointer part of the linked list holds the address of the next node and the last node points to null to indicate the end of the linked list. This allows dynamically increasing or decreasing the length of the linked list at run-time, which means it is dynamic in nature....

Representation of a Linked List in C

A linked list is represented as a pointer to the first node where each node contains:...

Implementation of Linked List in C

To implement a singly linked list in C, we first need to define a node structure that consists of two parts: data and the pointer to the next node. Let’s see how to create a new node....

Basic Operations on C Linked List

Following basic operations can be performed on a singly-linked List:...

1. Insertion

Insertion operation can be performed in the following three ways:...

2. Deletion

Similar to insertion, deletion operation can also be performed in three ways:...

3. Traversal

Linked list traversal means iterating over the each node of the list and performing the desired operations. To traverse a linked list and print its data, follow the below approach....

C Program to Implement Linked List

C #include #include // Node structure struct Node { int data; struct Node* next; }; // Function to create a new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } // Function to insert a node at the beginning void insertAtBeginning(struct Node** head, int data) { struct Node* newNode = createNode(data); newNode->next = *head; *head = newNode; } // Function to insert a node at the end void insertAtEnd(struct Node** head, int data) { struct Node* newNode = createNode(data); if (*head == NULL) { *head = newNode; return; } struct Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } // Function to delete a node with a given key void deleteNode(struct Node** head, int key) { struct Node* temp = *head; struct Node* prev = NULL; // If head node itself holds the key if (temp != NULL && temp->data == key) { *head = temp->next; free(temp); return; } // Search for the key while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If key was not present in list if (temp == NULL) return; // Unlink the node from linked list prev->next = temp->next; free(temp); } // Function to search for a node with a given key int search(struct Node* head, int key) { struct Node* current = head; while (current != NULL) { if (current->data == key) { return 1; // Key found } current = current->next; } return 0; // Key not found } // Function to traverse and print the linked list void traverse(struct Node* head) { struct Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } // Driver Code int main() { struct Node* head = NULL; // Insertion insertAtEnd(&head, 10); insertAtEnd(&head, 20); insertAtEnd(&head, 30); insertAtBeginning(&head, 5); printf("Linked List after insertions: "); traverse(head); // Deletion deleteNode(&head, 20); printf("Linked List after deletion of 20: "); traverse(head); // Searching int key = 10; if (search(head, key)) { printf("Element %d is found in the linked list.\n", key); } else { printf("Element %d is not found in the linked list.\n", key); } // Traversal printf("Final Linked List: "); traverse(head); return 0; }...

Advantages of a Linked List

Linked list is dynamic data structure it has memory allocation and de-allocation, which means that memory is only used when needed and not earlier in order to avoid waste.In linked list items are arranged in a linear fashion, new items can be inserted or removed as and when needed without affecting other items on the list, which makes these processes efficient.Memory is utilized effectively since nodes are created on-demand, which means that memory is not pre-allocated when the system has less load.Several linear data structures, like stacks and queues, can be implemented with the help of linked lists....

Disadvantages of a Linked List

In a linked list whenever we want to find the element with position n, all the other elements must be visited first, and this could take quite a lot of time.In a linked list, there is more memory needed than there is with an array because each node contains some pointer information.An operation that involves accessing elements of a linked list is not as simple as that in an array since elements of a linked list do not have indices that would help you jump to a given position directly.Reversing a linked list involves more than one level of pointer arrows in a singly linked list is not feasible or efficient....

Applications of Linked Lists

The following are the applications of linked list:...

Frequently Asked Questions on Linked List

How is the Linked List Different From an Array?...