Linked list A linked list is a linear data structure. It includes a series of connected nodes. E ach node stores the data and the address of the next node. For example: Initially to start, first node address is given using HEAD. And the last node is identified as point to NULL.
Types of Linked List Singly linked Doubly linked and Circular linked list
Singly Linked List It is the most common. Each node has data and a pointer to the next node. Node is represented as:
Singly Linked List A 3 -member singly linked list can be created as
int main() { // Initialize nodes struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; // Allocate memory one = malloc ( sizeof ( struct node)); two = malloc ( sizeof ( struct node)); three = malloc ( sizeof ( struct node)); // Assign value values one->value = 1; two->value = 2; three->value = 3; // Connect nodes one->next = two; two->next = three; three->next = NULL; // printing node-value head = one; printLinkedlist (head); } // Linked list implementation in C #include < stdio.h > #include < stdlib.h > // Creating a node struct node { int value; struct node *next; }; // print the linked list value void printLinkedlist ( struct node *p) { while (p != NULL) { printf ("%d ", p->value); p = p->next; } }
Doubly Linked List It adds a pointer to the previous node in a doubly-linked list. Thus, it can go in either direction: forward or backward. A node is represented as
Doubly Linked List A doubly linked list is a type of linked list in which each node consists of 3 components: * prev - address of the previous node data - data item *next - address of next node
Doubly Linked List A three-member doubly linked list can be created as:
Doubly Linked List In the above code, one, two, and three are the nodes with data items 1 , 2 , and 3 respectively. For node one : next stores the address of two and prev stores null (there is no node before it) For node two : next stores the address of three and prev stores the address of one For node three : next stores null (there is no node after it) and prev stores the address of two.