Data Structure Unit 3
Data Structure Unit 3
STRU CTURES
Unlocking the Power of Data: Your Journey into Data Structures
By Pranav Zambre
MEET YOUR INSTRUCTOR
➤ Unlike arrays, the elements are not stored in contiguous memory locations. Instead,
they are connected via pointers.
➤ Traversals can be done in one direction only as there is only a single link between two
nodes of the same list.
SINGLE LINKED LIST
Retrieving Nodes
➤ In a single linked list, you can easily find and get a specific piece of data. It's like looking
for a book in a library; you can start from the beginning, go through the pages one by one,
or jump directly to the section you need.
Adding Nodes
➤ When you want to put something new in the list, you have options. It's like adding new
friends to your contact list on your phone. You can put them at the start, at the end, or
squeeze them in wherever you want.
Removing Nodes
➤ Just like deleting unwanted apps from your phone, removing items from a linked list is
straightforward. You can get rid of them from the front, from the back, or pluck them out
from anywhere in between.
APPLICATION OF LINKED LIST
Employee Management System
➤ In an employee management system, single linked lists can be used to represent the
list of employees in a company. Each employee's information, such as their name,
employee ID, position, and salary, can be stored in a node of the linked list.
REPRESENTATION OF A LINKED
LIST IN MEMORY
There are two ways to represent a linked list in memory:
Insertion
Deletion
Search
Display
INSERTION IN SINGLY LINKED LIST
Insertion into a singly linked list can be performed at
different positions. This operation involves adding a new
node containing certain data into the list. The new node
typically has an empty data field and an empty next field
initially.
Likewise, For the free pool, we have a pointer AVAIL which stores the address of
the first free space.
INSERTING A NODE AT THE BEGINNING
In a linked list, each element (node) contains a data part and a reference (or link) to the
next node in the sequence. When inserting a node at the beginning of a linked list, the
new node becomes the first node, and its link points to the former first node.
Real-Life Example : Imagine you are organizing a playlist of songs where each song is
connected to the next one in a sequence (like a linked list). When you want to add a new
song to the beginning of the playlist.
ALGORITHM
INSERT_FIRST(START,ITEM)
Else
PTR = (Node*) malloc (size of (Node))
// create new node from memory and assign its address to PTR
ALGORITHM
Step 2: Set PTR -> INFO = ITEM
Set the next pointer of the new node to point to the current head node of the list.
Imagine you are organizing a playlist of songs where each song is connected to
the next one in a sequence (like a linked list). When you want to add a new song
to the end of the playlist, you simply add it after the last song. The new song's
link points to NULL, indicating it is now the last song in the playlist.
ALGORITHM
INSERT_FIRST(START,ITEM)
Else
PTR = (Node*) malloc (size of (Node))
// create new node from memory and assign its address to PTR
ALGORITHM
Step 2: Set PTR -> Info = Item
Step 3: Set PTR -> Next = NULL
Step 4: IF Start = NULL and then set Start = PTR;
Else
Step 5: Set Loc = Start
Step 6: Repeat Step 7 Until Loc -> Next != Null
Step 7: Set Loc = Loc -> Next
Step 8: Set Loc -> Next = Ptr
ALGORITHM
REPRESENTATION OF INSERTING A NODE
AT THE END
Create a New Node:
Allocate memory for the new node.
Initialize the new node with the given data.
Example: You have a playlist with the following songs in order: Song1, Song2, and
Song3. Now, you want to add a new song, "NewSong," at position 2. This means
"NewSong" should come after Song1 but before Song2.
ALGORITHM
Insert Location (START,ITEM,LOC)
Else
PTR = (Node*) malloc (size of (Node))
// create new node from memory and assign its address to PTR
ALGORITHM
Step 2 : Set PTR -> INFO = Item
Step 3 : If Start = NULL Then
Set Start = PTR
Set PTR -> Next = Null
Step 4 : Initialize The Counter I and Pointer
Set I = 0
Set temp = Start
Step 5 : Repeat Steps 6 and 7 Until I < Loc
Step 6 : Set temp = temp -> Next
Step 7 : Set I = I+1
Step 8 : Set PTR -> Next = Temp -> Next
Step 9 : Set temp -> Next = PTR
ALGORITHM
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
Identify the Position:
Determine the position where the new node is to be inserted, denoted as pos.
Set the next pointer of the new node to point to the node currently at
position pos.
Set the next pointer of prev_node to point to the new node.
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
INSERT A NODE IN SORTED LINKED LIST
This operation involves placing a new node into a linked list such that the order
of nodes, based on a specified key (like a numerical value or a string), is
maintained after insertion. The process ensures that the linked list remains
sorted without needing to sort the entire list again.
// Step 2: Retrieve the data value 'key' from the node to be inserted ('newP')
key = newP->data
// Step 4: Insert the new node ('newP') into the linked list
newP->link = temp->link
temp->link = newP
ALGORITHM
REPRESENTATION OF INSERT A NODE AT
SPECIFIC POSITION
Start at the Beginning
Begin with a pointer (temp) set to the head of the linked list.
Step 2: Initialize PTR to point to the first node (START) of the linked list.
Step 3: Input the value (Key) that you want to search for in the linked list.
Step 4: Traverse through the linked list using a loop. Inside the loop:
Check if PTR is not Null (to prevent accessing null pointer) and if PTR's INFO (the
data stored in the current node) is not equal to the Key.
REPRESENTATION OF DELETE A NODE
FROM THE END OF THE LIST
Move PTR to point to the next node in the linked list.
In singly linked list we can traverse in only Insertion and deletion take more time
one direction (forward). While, doubly compared to singly linked list.
linked list allows to traverse in both the
directions i.e. both forward and backward. Increased Memory Usage: Requires more
memory due to two pointers per node.
Insertion and deletion operations are more
efficient.
Insertion of a new node at the beginning of a doubly linked list means adding a new
item to the very start of the list. A doubly linked list is like a chain where each link
(node) has a value and connections to both the next and previous links.
STEPS INVOLVED IN THIS INSERTION
Create the New Node: Allocate space for the new node, set its data, and initialize
its pointers. The new node’s Next pointer is set to the current head of the list
(making it point to the former first node), and its Prev pointer is set to NULL
(since it will be the new head).
Update Pointers: Adjust the pointers of the new node and the current head
node. If the list is empty, the new node becomes both the head and the only
node in the list. If the list is not empty, update the Prev pointer of the current
head node to point to the new node and then set the list's head pointer to the
new node.
Change Head Pointer: Set the list's head pointer to the new node, making it the
new starting point of the list.
ALGORITHM
// Step 1: Check for Memory Allocation // Step 3: Update the current head
PTR = malloc(size of(Node)) node if it exists
if PTR is null: if START is not null:
print("Overflow") START -> Prev = PTR
exit
// Step 4: Update the head pointer
// Step 2: Set node information START = PTR
PTR -> Info = ITEM
PTR -> Next = START
PTR -> Prev = null
INSERTION OF NEW NODE AT THE
BEGINNING OF THE DOUBLY LINKED LIST
DIAGRAM
INSERTION OF NEW NODE AT THE
END OF THE DOUBLY LINKED LIST
Inserting a new node at the end of a doubly linked list means adding a new item to
the last position in the list. A doubly linked list is like a chain where each node (link)
has a value and connections to both the next and previous nodes.
STEPS INVOLVED IN THIS INSERTION
Create the New Node: Allocate space for the new node, set its data, and initialize
its pointers. The new node’s Next pointer is set to NULL because it will be the
last node in the list, and its Prev pointer is initially set to NULL (which will be
updated later if the list is not empty).
Update Pointers: Adjust the pointers of the new node and the current last node.
If the list is empty, the new node becomes both the head and the tail. If the list
is not empty, traverse to the end of the list, update the Next pointer of the
current last node to point to the new node, and set the Prev pointer of the new
node to the current last node.
Change Tail Pointer (if applicable): If a tail pointer is maintained in the list
implementation (pointing to the last node), update it to reference the new node.
ALGORITHM
// Step 1: Create the New Node START = PTR
PTR = malloc(size of(Node)) TAIL = PTR
if PTR is null: else:
print("Overflow") // List is not empty
exit LAST = START
PTR -> Info = ITEM while LAST -> Next is not null:
PTR -> Next = null LAST = LAST -> Next
PTR -> Prev = null
LAST -> Next = PTR
// Step 2: Update Pointers PTR -> Prev = LAST
if START is null: TAIL = PTR
// List is empty
INSERTION OF NEW NODE AT THE END OF
THE DOUBLY LINKED LIST DIAGRAM
INSERTION OF NEW NODE AT THE SPECIFIC
POSITION OF THE DOUBLY LINKED LIST
Insertion of a new node at a specific position in a doubly linked list involves adding
a new node at a particular location within the list. In a doubly linked list, each node
contains a value and two pointers: one pointing to the next node and another
pointing to the previous node.
STEPS INVOLVED IN THIS INSERTION
Create the New Node: Allocate space for the new node, set its data, and initialize
its pointers. The new node’s Next pointer will be set to point to the node
currently at the specified position, and its Prev pointer will be set to point to the
node immediately before the specified position (these will be updated later).
Update Pointers: Traverse the list to find the node currently at the specified
position and the node just before it. Adjust the pointers of the surrounding
nodes:
Set the Next pointer of the node before the specified position to point to the new
node.
Set the Prev pointer of the node at the specified position to point to the new node.
STEPS INVOLVED IN THIS INSERTION
Set the Prev pointer of the new node to the node before the position.
Set the Next pointer of the new node to the node at the specified position.
Maintain List Integrity: If the insertion occurs at the beginning or end of the list,
update the HEAD or TAIL pointers accordingly to reflect the new node's position
in the list.
ALGORITHM
// Step 1: Create the New Node while CURRENT is not null and INDEX
PTR = malloc(size of(Node)) < POSITION - 1:
if PTR is null: CURRENT = CURRENT -> Next
print("Overflow") INDEX = INDEX + 1
exit if POSITION == 0:
// Insert at the beginning
PTR -> Info = ITEM PTR -> Next = START
PTR -> Next = null if START is not null:
PTR -> Prev = null START -> Prev = PTR
START = PTR
// Step 2: Traverse to the Position
CURRENT = START
INDEX = 0
ALGORITHM
else if CURRENT is not null:
// Insert at a position other than the beginning
PTR -> Next = CURRENT
PTR -> Prev = CURRENT -> Prev
// Step 2: Identify the Node to Remove // Step 4: Free the Deleted Node
NODE_TO_DELETE = TAIL free(NODE_TO_DELETE)