Linked List Data Structure
Linked list in DS: The linked list is a non-primitive and linear data structure. It is a list of a particular type of data element that is connected to each other, as shown in the figure.
The linked list represents the group of nodes in which each node has two parts. The first part represents the data, and the second part represents the pointer. The pointer part of the linked list holds the address of the next node. A linked list is a data structure whose length can be increased or decreased dynamically (in run-time). The linked list is used to create a tree and graph.
Operations of Linked list
There are following types of operations in the linked list:
- Creation operation: It is used to create a new node in the linked list.
Node creation function:
struct node { int data; struct node *next; }; struct node *head, *ptr; ptr = (struct node *)malloc(sizeof(struct node *));
- Insertion operation: It is used to add a new node to a particular location in a specific situation. A new node can be inserted at the following locations.
- At the beginning of the linked list
- At the middle of the linked list
- At the end of the linked list
- Deletion operation: It is used to delete a node in a specific situation. A node can be deleted from the following locations.
- At the beginning of the linked list
- At the middle of the linked list
- At the end of the linked list
- Traversing Operation: This operation is used to print the values ??from a node point to another node point. In other words, traversing operations is a process in which all the nodes of a linked list are checked end-to-end. If traversing from the first node to the last node, it is called forward traversing.
The following code represents the traversing of a node in the linked list:
void traverse(node *head){if(head != NULL){traverse (head -> next);printf(“%d”, head -> data);}}
- Display Operation: The display operation is used to print the information of each node. This operation displays the complete list of nodes.
- Search Operation: The search operation is used to search a particular node in a linked list. A sequential search is most commonly used to search the nodes in a linked list.
- Concatenation Operation: This operation is used to connect a node to another node in the linked list.
Representation of Linked List in the Memory
The linked list is stored in different locations in the memory. The memory is allocated dynamically for each node. Dynamic means that it is allocated when it is needed. Due to the dynamical allocation, the user can increase and decrease the size of the linked list whenever he wants. An example of a memory representation in a linked list is shown in the figure below.