linked list in c
Linked List in C
A linked list is a data structure that consists of a sequence of nodes, where each node contains data and a reference (or link) to the next node in the sequence. In C, a linked list can be implemented using structures and pointers.
Here are the steps to create a linked list in C:
- Define the structure for the linked list node:
- Create a structure that represents a node in the linked list.
- The structure should contain two members: one for storing the data and another for storing the reference to the next node.
For example:
c struct Node { int data; struct Node* next; };
Create the head (or start) of the linked list:
- Declare a pointer variable to the first node of the linked list, often called the "head" or "start" of the list.
- Initialize the head pointer to
NULL
to indicate an empty list. For example:
c struct Node* head = NULL;
Insert nodes into the linked list:
- To insert a new node at the beginning of the linked list:
- Create a new node using dynamic memory allocation (
malloc
orcalloc
). - Assign the data value to the new node.
- Set the
next
pointer of the new node to the current head of the list. - Update the head pointer to point to the new node.
- Create a new node using dynamic memory allocation (
- To insert a new node at the end of the linked list:
- Create a new node using dynamic memory allocation.
- Assign the data value to the new node.
- Set the
next
pointer of the new node toNULL
. - Traverse the linked list from the head to the last node.
- Update the
next
pointer of the last node to point to the new node.
For example, to insert a node at the beginning:
c struct Node newNode = (struct Node)malloc(sizeof(struct Node)); newNode->data = newData; newNode->next = head; head = newNode;
Traverse the linked list:
- To access or print the elements of the linked list, you need to traverse through each node.
- Start from the head and follow the
next
pointers until you reach the end of the list (wherenext
isNULL
). For example:
c struct Node* current = head; while (current != NULL) { // Access or print the data of the current node printf("%d ", current->data); // Move to the next node current = current->next; }
Delete nodes from the linked list:
- To delete a node from the linked list:
- Find the node to be deleted and its previous node.
- Update the
next
pointer of the previous node to skip the node to be deleted. - Free the memory allocated for the node to be deleted using
free
.
For example, to delete the first node:
c struct Node* temp = head; head = head->next; free(temp);
Free memory:
- After you are done with the linked list, make sure to free the memory allocated for each node.
- Traverse through the linked list and use
free
to release the memory for each node. - For example:
c struct Node* current = head; while (current != NULL) { struct Node* temp = current; current = current->next; free(temp); }
These are the basic steps to create, insert, traverse, delete, and free a linked list in C. Remember to handle edge cases, such as an empty list or deleting the last node, appropriately in your implementation.