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

DSA521S-Linked List 3

The document discusses linked lists and their implementation. It covers: 1. The objectives of learning about linked lists including basic operations like insertion and deletion at different positions in the list. 2. Why linked lists are useful compared to arrays in terms of memory usage and efficiency of operations. 3. An overview of different types of linked lists like singly linked, doubly linked, circular, and doubly circular lists. 4. Pseudocode for implementing common linked list operations like insertion and deletion at the beginning, end, or a given position in a singly linked list.

Uploaded by

g4zbjcw8jp
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)
28 views

DSA521S-Linked List 3

The document discusses linked lists and their implementation. It covers: 1. The objectives of learning about linked lists including basic operations like insertion and deletion at different positions in the list. 2. Why linked lists are useful compared to arrays in terms of memory usage and efficiency of operations. 3. An overview of different types of linked lists like singly linked, doubly linked, circular, and doubly circular lists. 4. Pseudocode for implementing common linked list operations like insertion and deletion at the beginning, end, or a given position in a singly linked list.

Uploaded by

g4zbjcw8jp
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/ 30

DSA521S Data Structures and

Algorithms 1
Linked List
2023
Objectives

At the end of the lesson, students should be able to carry out basic
operations such as:
 Inserting a node at beginning of list, at position x, at the end of list!
 Deleting a node; from beginning of list, at position x, at the end of list!
 Traversing the list!
 Finding a node x from list!(In-class activity)
WHY linked List if we can easily use arrays??

 Classical arrays statically declared with a fixed size(list


growth/shrinking may become expensive to accommodate)
 Memory allocation is contiguous (inefficient memory usage)!
 Insertion / Deletion may require shifting(expensive)
Linked List
What is a linked list?
A linked list is a linear dynamic data structure that stores a sequence of
elements in non-consecutive locations. Each node in a list has a two parts.
The first is the data part and the second is a pointer (address) to the next
(successor) node and in some cases, a pointer to the previous
(predecessor) node. The head is a pointer to the first node, and the last
node in the list is called the tail. We can compare a linked list (Singly-Linked
List) to the way a train and its wagons are connected to each other. There
are four main types of linked lists.

Node

Data Address
Types of linked lists
 Singly linked list
 Doubly linked list
 Circular linked list
 Doubly Circular linked list
Singly Linked List
A singly linked list is a linked list which can be traversed in only one
direction (forward traversal only). For example; from head to tail only, not
vice versa. It is the most widely used linked list. A singly linked list can be
illustrated as follows:
Head Pointer Head Node Tail

100 5 200 2 300 8 Null

100 200 300


The first cell of the singly linked list stores the data, and second cell stores
the address or pointer to the next node. There are many applications
where a singly linked list can be used. One common application is to store a
list of elements that have to be processed in order. The head points to the
first element and the tail represents the last element in the list.
Doubly Linked List
Each node in the doubly linked list has three parts; The Data part and two
address parts. That is, each node has a pointer or address to the previous
node and a pointer to the next node, as shown below.
Node

Address 1 Data Address 2


The diagram below shows a doubly linked list with 3 nodes.
Head Pointer Head Node Tail

100 Null 5 200 100 2 300 200 8 Null

100 200 300

Circular Linked List


A circular linked list is a variation of the singly linked list. It is a list of
elements where each element / node is has a link to the next element, and
the last element has a link to the first element in the list, as shown below.
Head Pointer Head Node

100 5 200 2 300 8 100

100 200 300

Doubly Circular Linked List


A doubly circular linked list is a variation of the doubly and circular linked
lists. That means it has properties of both the doubly and circular linked
lists. Each node has three parts; The Data part and two address parts. Each
node has a pointer to the previous node and a pointer to the next node.
The last node has a link to the first node, and the first node has a link to the
last node in the list, as shown below.

Head Node
Head Pointer
300 5 200 100 2 300 200 8 100
100

100 200 300


Implementation of Linked List
Let’s look at the diagram below. This is a simple singly linked list that stores
three elements (5, 2 and 8).

Head Pointer

100 5 200 2 300 8 Null

100 200 300


Insertion in Linked Lists
Let’s write a simple pseudocode to insert data in the linked list based on
the diagram above.
Assuming that a new node is already created, let’s add data to the
newly created node using the pseudocode below. The address of that
new node is stored in a variable called newNode (newNode = address
of new node).

1. Inserting a node at the beginning of list

DISPLAY “Enter the data you want to insert” //prompt user


GET newNode -> data //read input and insert in the new node
newNode -> next = head //set the address of the new node
head = newNode //point the head to the newly created node
A node is now inserted at the beginning. Next, we will look at inserting
a node at the end of the list.

2. Inserting a node at the end of list


To insert a node at the end of the list, we need to traverse through the
list from the beginning to the point where we want to insert the new
node. Unlike inserting at the beginning, where we used the head
pointer, it is not possible in this example, because moving the head
pointer will result in losing the reference to the first node. That
means, we need another pointer variable to traverse through the list.
We will call this pointer variable temp.

newNode = 400

DISPLAY “Enter the data you want to insert” //prompt user


GET newNode -> data //read input and insert in the new node
newNode -> next = NULL //set the next address of the new node
temp = head // point temp the first node in the list
WHILE(temp -> next != NULL)//traverse using temp until end of list
temp = temp -> next
ENDWHILE
temp -> next = newNode

A node is now inserted at the end. Next, we will look at inserting a


node at a given position in the list.
3. Inserting a node at (after) a given position in the list
For demonstration, let’s insert the new node at position 2 or after
position 2. We cannot directly insert after position 2, we need to use
another variable and we will call it i (i = 1), then temp and position.

newNode = 400
i=1

DISPLAY “Enter the position to insert the data” //prompt user


GET position //read input and insert in the new node data part

IF(position > listLength) THEN //listLength is length of the list


DISPLAY “Invalid position!”
ELSE
temp = head //point temp the first node in the list
WHILE(i < position)//use loop to get to given position
temp = temp -> next
i=i+1
ENDWHILE
DISPLAY “Enter the data you want to insert” //prompt user
GET newNode -> data //read input and insert in the new node
newNode -> next = temp -> next//create right link of new node
temp -> next = newNode // create left link of the new node
ENDIF

Now we have created three insert operations of the linked list. It is more
efficient to create three functions, one for inserting a node at the
beginning, another for inserting a node at the end and another function for
inserting a node at a given position, and then call those functions in the
main function.

Deletion in Linked Lists


Head Pointer

100 5 200 2 300 8 400 9 NULL

100 200 300 400

Let’s write a simple pseudocode to delete data in the linked list based
on the diagram above.
A node is deleted when it is disconnected from other nodes, but even
if it is disconnected from others, we still need to free the memory
allocated to that node, because it is still holding some data. Freeing
the memory allocated to the node means completely deleting that
node.
Let’s delete data from the list using the pseudocode below. We are
going to use functions in this examples.
1. Deleting a node at the beginning of list
To delete a node at the beginning of the list, we update the value of
the head to point to the second node in the list. Let’s look at the
pseudocode below.

deleteAtBeg(){

IF(head == NULL)THEN
DISPLAY “List is empty!”
ELSE
temp = head //pointing temp to first node
head = head -> next //pointing head to second node
temp = free //deleting first node by freeing it or use free(temp)
function
ENDIF
}

2. Deleting a node at the end of list


To delete a node at the end of the list, we set the address part of the
second last node to zero, and then the link to the last node is broken,
after that free the memory allocated to the last node. Since a linked
list is a sequential data structure, we need to traverse the list until the
end of it.
That means we need a variable to traverse the list. We will call that
variable temp. Let’s look at the pseudocode below.
deleteAtEnd(){
temp = head //point temp to the first node

WHILE(temp -> next != NULL)//traverse using temp until end of list


prevNode = temp //point prevNode to the first node
temp = temp -> next //traverse the list using temp
ENDWHILE
IF(temp == head)THEN //if temp is still at first node, then only one
node in list
head = NULL //detach head from the first node
ELSE
prevNode -> next = NULL
ENDIF
temp = free //completely delete node (free(temp))
}

3. Deleting a node at the a given position


Let’s delete a node at the third position in the list. That means
updating the address part of the second node to point to the address
of the fourth node, and then free the third node. But to get to the
third node, we need to traverse the list from the beginning. Let’s look
at the pseudocode below.

deleteAtGivenPos(){

temp = head
DISPLAY “Enter position where you want to delete” //prompt
GET position //get position to delete
WHILE(i < position - 1) //traverse the list until third node-1
temp = temp -> next //move temp pointer
i = i + 1 //increment i
ENDWHILE
nextNode = temp -> next //assign address of third node to nextNode
temp -> next = nextNode -> next //assign address of last node to
second node
nextNode = free //completely delete node (free(nextNode))
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy