A linked list is a collection of “nodes”. It is connected together via links. These nodes consist of the data to be stored and a pointer to the address of the next node within the linked list. In the case of arrays, the size is limited to the definition, but in linked lists, there is no defined size. Any amount of data can be stored in it and can be deleted from it.
There are three types of linked lists −
Singly Linked List − The nodes only point to the address of the next node in the list.
Doubly Linked List − The nodes point to the addresses of both previous and next nodes.
Circular Linked List − The last node in the list will point to the first node in the list. It can either be singly linked or doubly linked.
Linked List Representation
Linked list can be represented as a chain of nodes, where every node points to the next node.
As per the above figure, following are the important points to be considered.
Linked List contains a link element called first (head).
Each link carries a data field(s) and a link field called next.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
Types of Linked List
Following are the various types of linked list.
Singly Linked Lists
Singly linked lists contain two “buckets” in one node; one bucket holds the data and the other bucket holds the address of the next node of the list. Traversals can be done in one direction only as there is only a single link between two nodes of the same list.
Doubly Linked Lists
Doubly Linked Lists contain three “buckets” in one node; one bucket holds the data and the other buckets hold the addresses of the previous and next nodes in the list. The list is traversed twice as the nodes in the list are connected to each other from both sides.
Circular Linked Lists
Circular linked lists can exist in both singly linked list and doubly linked list.
Since the last node and the first node of the circular linked list are connected, the traversal in this linked list will go on forever until it is broken.
Basic Operations in the Linked Lists
The basic operations in the linked lists are insertion, deletion, searching, display, and deleting an element at a given key. These operations are performed on Singly Linked Lists as given below −
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
Example
Following is the program of creating a list using linked and inserting element at beginning list in C programming language −
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(44); insertatbegin(50); printf("Linked List: "); // print list printList(); }
Output
Linked List: [ 50 44 30 22 12 ]
Insertion at Ending
In this operation, we are adding an element at the ending of the list.
Example
Following is the program of this operation in C programming languages −
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertatend(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; struct node *linkedlist = head; // point it to old first node while(linkedlist->next != NULL) linkedlist = linkedlist->next; //point first to new first node linkedlist->next = lk; } void main(){ int k=0; insertatbegin(12); insertatend(22); insertatend(30); insertatend(44); insertatend(50); printf("Linked List: "); // print list printList(); }
Output
Linked List: [ 12 22 30 44 50 ]
Insertion at a Given Position
Example
Following is the program of inserting element at given position in C programming language −
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertafternode(struct node *list, int data){ struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; lk->next = list->next; list->next = lk; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertafternode(head->next, 30); printf("Linked List: "); // print list printList(); }
Output
Linked List: [ 22 12 30 ]
Deletion Operation
Deletion in linked lists is also performed in three different ways. They are as follows −
Deletion at Beginning
In this deletion operation of the linked, we are deleting an element from the beginning of the list. For this, we point the head to the second node.
Example
Following is the program of deleting element at beginning in C programming language −
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deleteatbegin(){ head = head->next; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); deleteatbegin(); printf("\nLinked List after deletion: "); // print list printList(); }
Output
Linked List: [ 55 40 30 22 12 ] Linked List after deletion: [ 40 30 22 12 ]
Deletion at Ending
In this deletion operation of the linked, we are deleting an element from the ending of the list.
Example
Following is the program of deleting element at the end in C programming languages −
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deleteatend(){ struct node *linkedlist = head; while (linkedlist->next->next != NULL) linkedlist = linkedlist->next; linkedlist->next = NULL; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); deleteatend(); printf("\nLinked List after deletion: "); // print list printList(); }
Output
Linked List: [ 55 40 30 22 12 ] Linked List after deletion: [ 55 40 30 22 ]
Deletion at a Given Position
In this deletion operation of the linked, we are deleting an element at any position of the list.
Example
Following is the program of deleting element at a given position in C programming languages −
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; printf("\n["); //start from the beginning while(p != NULL) { printf(" %d ",p->data); p = p->next; } printf("]"); } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void deletenode(int key){ struct node *temp = head, *prev; if (temp != NULL && temp->data == key) { head = temp->next; return; } // Find the key to be deleted while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } // If the key is not present if (temp == NULL) return; // Remove the node prev->next = temp->next; } void main(){ int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatbegin(40); insertatbegin(55); printf("Linked List: "); // print list printList(); deletenode(30); printf("\nLinked List after deletion: "); // print list printList(); }
Output
Linked List: [ 55 40 30 22 12 ] Linked List after deletion: [ 55 40 22 12 ]
No comments:
Post a Comment