linked-lists
linked-lists
• Array
• Linked List
• Stacks
• Queues
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No shifting
Elements are usually shifted
No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to our
full; otherwise may result in much memory need) there is no waste of memory.
waste.
Sequential access is faster [Reason: Elements Sequential access is slow [Reason: Elements not
in contiguous memory locations] in contiguous memory locations]
Representation of linked list in memory
Types of lists
There are two basic types of linked list
– Singly Linked list
– Doubly linked list
– Circular linked list
Singly Linked List
• Each node has only one link part
• Each link part contains the address of the next
node in the list
• Link part of the last node contains NULL value
which signifies the end of the node
Schematic representation
Here is a singly-linked list (SLL):
myList
a b c d
if (head == NULL)
{
head = curr;
tail = curr;
}
else {
tail->next = curr;
tail = curr;
}
// end of loop;
}
To display
void display()
{
curr = head;
printf( "\n");
while(curr != NULL) {
printf(" %d---> ", curr->val);
curr = curr->next ;
} }
Traversing a Linked List
Traversing linked list means visiting each and every node of the Singly
linked list. Following steps are involved while traversing the singly
linked list –
Firstly move to the first node
Fetch the data from the node and perform the operations such as
arithmetic operation or any operation depending on data type.
After performing operation, advance pointer to next node and
perform all above steps on Visited node.
Inserting the node in a SLL
There are 3 cases here:-
start
start
To be deleted
void delete_any()
{
struct node *temp,*temp1;
int key = data to be deleted;
if(start!=NULL) {
temp = start;
while(temp->next!=NULL and temp->data != key) {
temp1 = temp;
temp = temp->next;
}
if (temp->data == key){
temp1->next = temp->next;
delete(temp);
}
}
}
Searching a SLL
• Searching involves finding the required element in
the list
• We can use various techniques of searching like
linear search or binary search where binary search
is more efficient in case of Arrays
• But in case of linked list since random access is
not available it would become complex to do
binary search in it
• We can perform simple linear search traversal
In linear search each node is traversed till the data in
the node matches with the required value
void search(int x)
{
node*temp=start;
while(temp!=NULL)
{
if(temp->data==x)
{
cout<<“FOUND ”<<temp->data;
break;
}
temp=temp->next;
}
}
Reversing a linked list
• We can reverse a linked list by reversing the direction
of the links between 2 nodes
Doubly Linked List
1. Doubly linked list is a linked data structure that consists of a set of
sequentially linked records called nodes.
NULL A
11 786 200 B
656 400 786 777 CNULL
200 786 400
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};
}
else
{
node* temp=start;
start=p;
temp->previous=p; //making 1st node’s previous point to the new node
p->next=temp; //making next of the new node point to the 1st node
}
Inserting at the end
void insert_end(node* p)
{
if(start==NULL)
{
start=p;
}
else
{
node* temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->previous=temp;
}
}
Inserting after a node
Adjusting the next and previous pointers of the nodes b/w which the
new node accordingly
void insert_after(int c,node* p)
{
temp=start;
for(int i=1;i<c-1;i++)
{
temp1 = temp;
temp=temp->next;
}
p->next=temp;
temp->previous=p;
temp1->next=p;
p->previous=temp1;
}
Deleting a node
a b c