Linkedlist
Linkedlist
LINKED LIST
Linked List
For example
Linked List
structure.
Singly linked list is probably the most easiest data structure to implement.
Insertion and deletion of elements doesn't requires movement of all elements when
compared to an array.
Requires less memory when compared to doubly, circular or doubly circular linked list.
Can allocate or deallocate memory easily when required during its execution.
It is one of most efficient data structure to implement when traversing in one direction is
required.
Array Linked list
An array is a collection of elements of a similar data A linked list is a collection of objects known as a node
type. where node consists of two parts, i.e., data and address.
Array elements store in a contiguous memory location. Linked list elements can be stored anywhere in the
memory or randomly stored.
Array works with a static memory. Here static memory The Linked list works with dynamic memory. Here,
means that the memory size is fixed and cannot be dynamic memory means that the memory size can be
changed at the run time. changed at the run time according to our requirements.
Array elements are independent of each other. Linked list elements are dependent on each other. As
each node contains the address of the next node so to
access the next node, we need to access its previous
node.
Array takes more time while performing any operation Linked list takes less time while performing any
like insertion, deletion, etc. operation like insertion, deletion, etc.
Accessing any element in an array is faster Accessing an element in a linked list is
as the element in an array can be directly slower as it starts traversing from the first
accessed through the index. element of the linked list.
In the case of an array, memory is allocated In the case of a linked list, memory is
at compile-time. allocated at run time.
Memory utilization is inefficient in the array. Memory utilization is efficient in the case of
For example, if the size of the array is 6, and a linked list as the memory can be allocated
array consists of 3 elements only then the or deallocated at the run time according to
rest of the space will be unused. our requirement.
Types of Linked Lists
struct node
int data;
}
Example
2 Bilal 5
3 Osama 1
4 Noman 3
5 Navid 4
Insertion in Linked list
Insert at the beginning
Allocate memory for new node
Store data
Change next of new node to point to head
Change head to point to recently created node
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = head;
head = newNode;
Delete from a Linked List
Delete from beginning
Point head to the second node
head = head->next;
Delete from end
Traverse to second last element
Change its next pointer to null
struct node* temp = head;
while(temp->next->next!=NULL){
temp = temp->next;
}
temp->next = NULL;
Doubly Linked List
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link
called Next.
Prev − Each link of a linked list contains a link to the previous link
called Prev.
LinkedList − A Linked List contains the connection link to the first
link called First and to the last link called Last.
Basic Operations
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Insert Last − Adds an element at the end of the list.
Delete Last − Deletes an element from the end of the list.
Insert After − Adds an element after an item of the list.
Delete − Deletes an element from the list using the key.
Display forward − Displays the complete list in a forward manner.
Display backward − Displays the complete list in a backward manner.
Advantages of DLL over the singly linked list:
struct node
{ int data;
}
A three-member doubly linked list
Example
Example
Insertion in DLL
Algorithm
struct Node
{
int data;
struct Node * next;
}
;
Assignment