0% found this document useful (0 votes)
16 views

DS 1.2 Linked list

Uploaded by

firoshkhan4126
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

DS 1.2 Linked list

Uploaded by

firoshkhan4126
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

LINKED LIST

DS Module 1
ARRAYS AND ITS LIMITATIONS

excecution
LINKED LIST
• Linear data structure
• Dynamic-memory allocation during run time.
• It is a collection of elements called nodes.
• Nodes includes two fields:
– Information part (info or data or value ...) contains data.
– Linking part (link or next ...) contains pointer (address) to the next
node.
• The pointer to the first element of the list is called start or head.
• The link part of last node has no next node to point to, so it is made
NULL.
• A node can be created using the following
structure;
struct node
{
int info;
struct node *link;
}*temp,*p;
– Self Referential structures are those structures that have one or more
pointers which point to the same type of structure, as their member
• The memory allocation for the structure variable
p (or the node p) can be done dynamically as;

p=(struct node *)malloc(sizeof(struct node))

• The info part of the node p can be written as;


p->info
• The link part of p can be written as;
p->link
Representation of Linked lists in memory

• Linked lists can be represented in memory by using two parallel


arrays respectively known as INFO and LINK, such that INFO[K] and
LINK[K] contains information of element and next node address
respectively.
• The list also requires a variable, start which contains the address of
the first node. Pointer field of the last node denoted by NULL,
which indicates the end of the list
• Unlike arrays, linked list elements are not stored at a contiguous
location; the elements are linked using pointers.
• In memory the linked list is stored in scattered cells (locations).
Representation of Linked lists in memory
OPERATIONS ON LINKED LISTS
● CREATION
● INSERTION
● DELETION
● TRAVERSAL
● REVERSAL
● SEARCHING
● CONCATENATION
● SORTING
● DISPLAY etc...
● Types of Linked List
○ Singly linked list
○ Doubly linked list
○ Circular linked list
■ Singly linked list as circular
■ Doubly linked list as circular
○ Header linked list
■ Grounded header list
■ Circular header list
Singly linked list

• A singly linked list is a type of linked list that is


unidirectional, that is, it can be traversed in
only one direction from head to the last node
(tail).
• The nodes in the singly linked list contains
only one pointer (forward pointer), which
points the next node in the list.
Creation of a singly linked list
Step 1 : Include alloc.h header File
Step 2 : Define Node Structure
struct node
{
int data;
struct node *link;
}*start=NULL;
Step 3 : Create a node using Dynamic Memory Allocation

new_node=(struct node *)malloc(sizeof(struct node));


Step 4 : Fill Information in newly Created Node

printf("Enter the data : ");


scanf("%d",&new_node->data);
new_node->link=NULL;
Step 5 : Creating Very First Node

if(start == NULL)
{
start = new_node;
current = new_node;
}
Step 6: Creating Second or nth node
else
{
current->link = new_node;
current = new_node;
}
Traversal in a singly linked list
● Traversal means visiting each node, starting from the first node
till we reach the last node.
void traversal(struct node *start)
{ if(start==NULL)
printf("\nList is empty");
else {
for(current=start;current!=NULL;current=current->link)
printf(" %d",current->data); } }
Garbage Collection----AVAIL list
● Memory space can be reused if a node is deleted from a list.
● Garbage collection is a technique in which the OS collects the
deleted space time to time onto the availability list(AVAIL LIST).
● The heap is handled as a linked list of unused blocks of memory,
so called free-list (or Avail list).
Overflow and Underflow
Overflow
● In linked list overflow occurs during insertion operation when
AVAIL=NULL.
● That is there is no available space in the avail list.
Underflow
● In linked list underflow occurs during deletion operation when
START=NULL.
Insertion operation in a singly linked list
Insertion operation in a singly linked list
● There are three cases
1. Insertion at the beginning
2. Insertion at the end
3. Insertion in between nodes
Insertion at beginning...steps
1. Allocate memory for new node

○ struct node *newNode;


○ newNode = (struct node *)malloc(sizeof(struct node));

2. Store data newNode->data = 2;(eg:)

start
3. Change link of new node to point to start
newNode->link=start

4.Change start to point to recently created node start=newNode

start
Insertion at end---steps
1. Allocate memory for new node
a. struct node *newNode;

b. newNode = (struct node *)malloc(sizeof(struct node));


2. Store data and make its link as NULL
a. newNode->data = 102;(eg:)
b. newNode->link=NULL
3. Traverse to last node
struct node *prev;
prev=start;
while(prev->link!=NULL)
prev=prev->link;
4. Change link of last node to recently created node

prev->link=newNode;
Insertion in between nodes---steps
1. Allocate memory and store data for new node
2. Traverse to node just before the required position of new node
3. Change links to include new node in between
a. newNode->link = temp->link;
b. temp->link = newNode;
Deletion operation in a singly linked list
● There are three cases
1. Deletion from the beginning
2. Deletion from the end
3. Deletion of intermediate node
Deletion from the beginning--steps
1) Make the start (head) pointer pointing towards the second node.
2) Free memory for the first node which is to be deleted.
Deletion from the end---steps
1. Traverse to second last element
2. Change its next pointer(link) to null
Deletion of intermediate node---steps
1. Traverse to element before the element to be deleted
2. Change next pointers(link) to exclude the node from the chain
Applications
1. Implementation of stacks and queues
2. Implementation of graphs
3. Dynamic memory allocation : We use linked list of free blocks.
4. Manipulation of polynomials
5. Representing sparse matrices
Polynomial Representation using linked list
● A polynomial can be thought of as an ordered list of non zero
terms.
● Each non zero term is a two-tuple which holds two pieces of
information: The exponent part ,The coefficient part
Searching in a singly linked list
• Searching operation is the process of
accessing the desired node in the list.

• For searching an element, we traverse


the linked list and while traversing we
compare the info(data) part of each node
with the given element to be searched.
void search(struct node *start, int item)
{
struct node *temp=start;
while(temp!=NULL)
{
if(temp->data==item)
{
printf(”Item found”);
return;
}
temp=temp->link;
}
printf(”Item not found”);
}
Doubly linked list (Two way list)
• A Doubly Linked List (DLL) contains an extra
pointer, typically called previous pointer,
together with next pointer and data.
• The structure for a node of doubly linked list can be declared as;

struct Node {
struct Node* prev;
int data;
struct Node* next;
};
• Here ‘prev‘ is a pointer that will contain the address of previous
node and ‘next‘ will contain the address of next node in the list.
• It is a two way list.
•In doubly linked list we can move in both
directions at any time.
•The next pointer of last node and prev pointer of
first node are NULL.
Circular linked list
• A linked list whose last node points back to the first
node instead of containing the NULL pointer, called a
circular list.
• Each node has a successor and all the nodes form a
ring.
• Circular linked list is a linked list where all nodes are
connected to form a circle. There is no NULL at the end.
• A circular linked list can be a singly circular linked list
or doubly circular linked list.
Singly Linked List as Circular

•In singly linked list, the next pointer of the last


node points to the first node.
Doubly Linked List as Circular
In doubly linked list, the next pointer of the
last node points to the first node and the
previous pointer of the first node points to
the last node making the circular in both
directions.
Linked List with Header Node
• Header node is a dummy node that is present at
the beginning of the list and its link part is used to
store the address of the first actual node of the
list.
• The info part of this node may be empty or can
be used to contain useful information about the
list like count of elements currently present in the
list.
Insertion on a Doubly Linked List

• We can insert elements at 3 different


positions of a doubly-linked list:
• Insertion at the beginning
• Insertion in-between nodes
• Insertion at the End
1. Insertion at the Beginning
• Consider the given list

Step 1. Create a new node


• allocate memory for newNode
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
• assign the data to newNode.
newNode->data = 6;
Step 2. Set prev and next pointers of newNode
• point next of newNode to the first node of the
doubly linked list
newNode->next = head;
• point prev to null
newNode->prev = NULL;
Step 3. Make newNode as head node
• Point prev of the first node to newNode
head->prev = newNode;
• Point head to newNode
head = newNode;
2. Insertion in between two nodes
Step 1. Create a new node
• allocate memory for newNode
• assign the data to newNode.
• Traverse till the position(temp node)
Step 2. Set the next pointer of newNode and temp node
• assign the value of next from temp node to the next of newNode
newNode->next = temp->next;
• assign the address of newNode to the next of temp node
temp->next = newNode;

temp node
Step 3. Set the prev pointer of new node and the next node
• assign the value of prev of next node to the prev of newNode
newNode->prev = temp;
• assign the address of newNode to the prev of next node
newNode->next->prev = newNode;

Final List after insertion


3. Insertion at the End
Step 1. Create a new node
Step 2. Traverse to the end of the doubly linked
list and set prev and next pointers of new node
and the last(temp) node.
newNode->next = NULL;
temp->next = newNode;
newNode->prev = temp;
Deletion from a DLL
1. Delete the First Node of Doubly Linked List
• If the node to be deleted (i.e. del_node) is at the beginning
Reset pointers of node after the del_node .
Finally, free the memory of del_node
if (head == del_node)
head = del_node->next;
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
free(del_node);
2. Deletion of the Inner Node
• If del_node is an inner node traverse till the particular node and we must
have to reset the next and prev pointers of the nodes before and after
the del_node. Finally, free the memory of del_node.
• For the node after the del_node
if (del_node->next != NULL)
del_node->next->prev = del_node->prev;
• For the node before the del_node
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
free(del_node);
3. Delete the Last Node of Doubly Linked List
• Traverse till the end and we can simply delete
the del_node (last node)and make the next of node
before del_node(last node) point to NULL.
• Finally, free the memory of del_node.
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
free(del_node);
Comparison between Array and
Linked List
ARRAY LINKED LIST

● Linear Data Structure ● Linear Data Structure


● Static ● Dynamic
● Memory allocated at compile time. ● Memory allocated at run time.
● Size fixed… Can’t change ● Size not fixed...Variable

● Stored in contiguous memory ● Not Stored in contiguous memory


locations
locations ● Sequential Access
● Random Access using index ● Memory overhead for pointers
● No memory overhead
• No wastage of memory
● Wastage of memory
● Insertion and deletion are difficult • Insertion and deletion are easier
● Programming is easy
• Programming is difficult

You might also like