LINKED LIST
A linked list is a collection of elements called ‘nodes’ where each node consists of two parts:
- Info: Actual element to be stored in the list. It is called a data field.
- Next: one or more link that points to the next and previous node in the list. It is also called the pointer field.
- The link list is a dynamic structure i.e. it grows or shrinks depending on different operations performed.
- The last node has some specified value called NULL as the next address which means the end of the list.
TYPES OF LINKED LIST
- Singly Linked List
It is the simplest type of linked list in which every node contains some data and a
pointer to the next node of the same data type. The node contains a pointer to
the next node means that the node stores the address of the next node in the
sequence. A single linked list allows the traversal of data only in one way.
- Doubly Linked List
A doubly linked list or a two-way linked list is a more complex type of linked list
which contains a pointer to the next as well as the previous node in sequence,
Therefore, it contains three parts are data, a pointer to the next node, and a
pointer to the previous node. This would enable us to traverse the list in the
backward direction as well.
- Circular Linked List
A circular linked list is that in which the last node contains the pointer to the first
node of the list. While traversing a circular liked list, we can begin at any node
and traverse the list in any direction forward and backward until we reach the
same node we started. Thus, a circular linked list has no beginning and no end.
- Doubly Circular Linked List
A Doubly Circular linked list or a circular two-way linked list is a more complex
type of linked list that contains a pointer to the next as well as the previous node
in the sequence. The circular doubly linked list does not contain null in the
previous field of the first node.
LINKED LIST OPERATIONS
- Traversal: Access each element stored in the linked list
- Insertion: Add new elements to the linked list
- Deletion: Delete elements from the linked list
- Searching: Locating node in the linked list
- Sorting: Sort the nodes of the linked list
Algorithm
Insertion:
a. Insert at Head
It involves the insertion of the new element at the beginning of the linked list.
- Create a node using the malloc function
- new node= (struct node*) malloc(sizeof(struct node));
- Assign data to the info field of the new node
- newnode->info=data
- Set
- newnode->next=NULL
- if the head is NULL then set head=newnode and exit
- head=newnode
- otherwise.
- Set next of new node to head
- new node->next=head
- Set the head pointer to point to the new node
- head=newnode
7. End
b. Insert at Tail
It involves the addition of a new element to the end of the linked list.
- Create a node using the malloc function
- new node= (struct node*) malloc(sizeof(struct node));
- Assign data to the info field of the new node
- new node->info=data
- if the head is NULL then set head=newnode and exit
- Otherwise
- Set
- ptr=head
- Find the last node
- while( ptr->next !=NULL)
- ptr=ptr->next
- Set
- ptr->next=newnode
6. End
c. Insert at Specified Position
It inserts a new element at the given position in the linked list.
- Create a node using the malloc function
- new node= (struct node*) malloc(sizeof(struct node));
- Assign data to the info field of the new node
- new node->info=data
- Enter the position of a node at which you want to insert a new node.
- Let the position be pos
- Set
- ptr=head
- for(i=0;i<pos-1;i++){
- ptr=ptr->next;
- if(ptr=NULL) }
- printf("position not found");
- return;}}
- Set
- newnode->next=ptr->next
- Set next of ptr to point to the new node
- ptr->next=newnode
- End
Deletion:
a. Deletion at Beginning
It removes the element stored at the beginning of the list.
- If (head==NULL) then
- Display "Node not found" and Exit
- Otherwise, store the address of the first node in the temporary variable ptr
- ptr=head
- Set head of the next node to head
- head=head->next
- Free the memory reserved by the temp variable
- free(ptr)
- End
b. Deletion at End
It deletes the element stored at the end of the linked list.
- If (head==NULL) then
- Display "Node not found" Exit
- Otherwise if (head->next==NULL) Set
- ptr=head
- head=NULL
- free(ptr)
- Otherwise
- ptr=head
- while(ptr->next!=NULL)
- temp=ptr
- ptr=ptr->next
- temp->next=NULL
- free(ptr)
- End
c. Deletion at the specified position
It deletes the element stored at the given position in the linked list.
- If head=NULL
- Display "Node not found" and Exit
- Otherwise
- Enter the position pos of the node to be deleted
- If pos=0
- Set ptr=head
- head=head->next
- free(ptr)
- Otherwise
- Set
- ptr=head
- for( i=0;i<pos;i++){
- temp=ptr
- ptr=ptr->next
- if(ptr==NULL){ Display "Not found"
- return }}
- Set
- temp->next=ptr->next
- free(ptr)
- End
LINKED LIST UTILITY
Lists are one of the most popular and efficient data structures, with a wide range of implementation in every programming language like C, C++, Python, Java, and C#.
Linked lists are a great way to learn how pointers and memory work. By practicing how to manipulate linked lists, you can prepare yourself to learn more advanced data structures like graphs and trees.
TIME COMPLEXITY
The time complexity for the operations performed on the linked list is:
Search O(n) O(n)
Insert O(1) O(1)
Deletion O(1) O(1)
APPLICATIONS OF LINKED LIST
- Dynamic memory allocation
- In undoing the functionality of the software
- Hash tables, Graphs
- Polynomial Manipulation representation
- Access to previous and next URL searched
- The songs in the Music Player are linked to the next and the previous song