DS 1.2 Linked list
DS 1.2 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;
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
start
3. Change link of new node to point to start
newNode->link=start
start
Insertion at end---steps
1. Allocate memory for new node
a. struct node *newNode;
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.
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
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;