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

Chapter 3

Chapter 3 discusses linked lists, a dynamic data structure consisting of nodes that store data and pointers to the next node, allowing for efficient insertion and deletion. It covers various types of linked lists, including singly, doubly, and circular linked lists, and explains their memory representation and traversal algorithms. Additionally, it touches on garbage collection, overflow and underflow, and compares linked lists with arrays in terms of memory allocation and access efficiency.

Uploaded by

lap use
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 3

Chapter 3 discusses linked lists, a dynamic data structure consisting of nodes that store data and pointers to the next node, allowing for efficient insertion and deletion. It covers various types of linked lists, including singly, doubly, and circular linked lists, and explains their memory representation and traversal algorithms. Additionally, it touches on garbage collection, overflow and underflow, and compares linked lists with arrays in terms of memory allocation and access efficiency.

Uploaded by

lap use
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter 3 (Linked Lists)

Linked Lists
• A linked list is a data structure that stores a sequence of elements. Each element in the
list is called a node, and each node has a reference to the next node in the list. The first
node in the list is called the head, and the last node in the list is called the tail.

• An array is a collection of elements of a similar data type. A linked list is a collection


of objects known as a node where node consists of two parts, i.e., data and address.
• There are several advantages of linked list over arrays, such as dynamic size, efficient
insertion and deletion, memory efficiency, easy implementation of abstract data types,
and more efficient sorting in some cases.
• Since an array usually occupies a block of memory space, one cannot increase the size
of an array when additional space is required.
Properties of Linked List
• Dynamic size: Instead of fixed-size arrays, Linked lists can expand or contract as needed
while a program is running.
• Effective element insertion and deletion: By simply updating the pointers, linked lists
can insert or delete elements from anywhere in the list.

• Singly Linked List


Begins with a pointer to the first node.
Terminates with a null pointer.
Only traversed in one direction.
• Doubly Linked List
Two start pointers (first element and last element)
Each node has a forward pointer and a backward pointer
Always traverses both forwards and backwards
• Circular, Singly Linked List
Pointer in the last node points back to the first node.
• Circular, Doubly Linked list
Forward pointer of the last node points to the first node and backward pointer of the first node
points to the last node.
Representation of an Linked Lists in Memory
This representation of a linked list depicts that each node consists of two fields. The first field
consists of data, and the second field consists of pointers that point to another node.
Linked List is a linear data structure, in which elements are not stored at a contiguous location,
rather they are linked using pointers. Linked List forms a series of connected nodes, where
each node stores the data and the address of the next node.
Unlike Arrays, LinkedList is not stored in a contiguous memory location. Each element int the
list is spread across the memory and are linked by the pointers in the Node. Thus whenever a
new element needs to be added a separate memory is allocated enough to store both key and
the pointer to the next element.
Linked lists can be represented in
memory by using two 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 name
or start, which contains address of
first node. Pointer field of last node
is denoted by null which indicates
the end of list.
• Start = 9, so info[9] = N is the
first character.
• Link[9] = 3, so info[3] = O is the second character
• Link[3] = 6, so info[6] = □ (blank) is the third character
• Link[6] = 11, so info[11] = E is the fourth character
• Link[11] = 7, so info[7] = X is the fifth character
• Link[7] = 10, so info[10] = I is the sixth character
• Link[10] = 4, so info[4] = T is the seventh character
• Link[4] = 0, the Null value, so the list has ended.
Traversing a linked List
• Let LIST be a linked list in memory with START pointing to the first element and
NULL indicating the end of LIST. Suppose we want to traverse LIST in order to
process some application. The traversing algorithm uses a pointer variable PTR which
points to the node that is currently being processed. Accordingly, PTR:= LINK[PTR]
points to the next node to be processed.
• The algorithm for traversing a Linked List is-
1. set PTR:= START. [Initializes pointer PTR]
2. repeat steps 3 and 4 while PTR ≠ NULL
3. apply PROCESS to INFO[PTR]
4. set PTR:= LINK[PTR. [PTR points to the next node]
end of the step 2 loop.
5. Exit.
• The procedure to find the number NUM of elements in a linked list is given
below-
COUNT(INFO, LINK, START, NUM)
1. Set NUM := 0. [initializes counter]
2. Set PTR := START. [initialize pointer]
3. Repeat steps 4 and 5 while PTR ≠ NULL
4. Set NUM := NUM + 1. [increase NUM by 1]
5. Set PTR := LINK[PTR]. [update pointer]
[end of step 3 loop]
6. Return.
Searching a Linked List
• Searching is performed in order to find the location of a particular element in the list.
Searching any element in the list needs traversing through the list and make the
comparison of every element of the list with the specified element.
• Implementation for C Program to search an element in a linked list:-
Initialize head = Null
Add a few items to the Linked List
Take input from the user for the item he wants to search
Linearly traverse the Linked List from head to the end until you hit the null node
For each node check if its data value == item user wants to search
Return index of node where data was found else return -1

Suppose the data in LIST are not necessarily sorted. Then one searches for ITEN in LIST
by traversing through the list using pointer variable PTR and comparing ITEM with contents
INFO[PTR] of each node, one by one.
• SEARCH(INFO, LINK, START, ITEM, LOC)
1. set PTR:= START
2. repeat step 3 while PTR ≠ NULL
3. if ITEM = INFO[PTR], then set LOC:= PTR and Exit.
else set PTR:= LINK[PTR]. [PTR now points to the next node]
end of If structure.
[end of step 2 loop]
4. [search is unsuccessful] set LOC:= NULL
5. Exit.
Suppose the data in LIST are sorted. This algorithm finds the location LOC of the node
where ITEM first appears in LIST or set LOC = NULL.
• SRCHSL(INFO, LINK, START, ITEM, LOC)
1. set PTR:= START
2. repeat step 3 while PTR ≠ NULL
3. if ITEM ˂ INFO[PTR], then set PTR := LINK[PTR]. [PTR points to the next node]
else if ITEM = INFO[PTR], then set LOC := PTR and Exit. [successful]
Else set LOC:= NULL and Exit. [ITEM exceeds INFO[PTR]]
[End of step 2 loop]
4. set LOC := NULL
5. Exit.
Garbage Collection
• Garbage collection (GC) is a memory recovery feature built into programming
languages such as C and Java. A GC-enabled programming language includes one or
more garbage collectors that automatically free up memory space that has been allocated
to objects no longer needed by the program.
• The main objective of Garbage Collection is to free heap memory by destroying the
objects that don't contain a reference. When there are no references to an object, it is
assumed to be dead and no longer needed. So the memory occupied by the object can
be reclaimed.
• Using GC in data structures means designing them to manage memory well. This helps
automatically free up memory that's no longer needed. By using tools or techniques like
reference counting and efficient memory use, data structures can work smoothly and
avoid causing memory problems in programs.
Overflow & Underflow
• In programming, data types have a fixed range of values they can represent. When a
calculation or operation produces a value beyond this range, it leads to overflow or
underflow. Overflow happens when the result is too large, while underflow occurs when
the result is too small.
• If the stack is full and an attempt is made to add an element to it, an overflow error
occurs. If the stack is empty and an attempt is made to remove an element from it, an
underflow error occurs.
• Overflow is when the absolute value of a number is too high to be represented by the
CPU in question in the space given to store it. Underflow is when the absolute value of
a number is too close to zero for it to be represented in memory in the space allotted to
it.
Insertion into a Linked List

Let LIST be a linked list with successive


nodes A and B (figure (a)). Suppose a
node N is to be inserted into the list
between nodes A and B. The diagram of
such an insertion is shown in figure (b).
For a given Linked List, the task is to
insert a new node in this given Linked
List at the following positions:
• At the front of the linked list
• After a given node.
• At the end of the linked list.
Insertion into a Linked List
• To insert a node at the start/beginning/front of a Linked List, we need to:
Make the first node of Linked List linked to the new node
Remove the head from the original first node of Linked List
Make the new node as the Head of the Linked List.
• To insert a node after a given node in a Linked List, we need to:
• Check if the given node exists or not.
• If it do not exists,
• terminate the process.
• If the given node exists,
• Make the element to be inserted as a new node
• Change the next pointer of given node to the new node
• Now shift the original next pointer of given node to the next pointer of new
node
Header Linked List
• A header node is a special node that is found at the beginning of the list. A list that
contains this type of node, is called the header-linked list. This type of list is useful
when information other than that found in each node is needed.
• A header linked list is a type of linked list that uses a special header node to represent
the beginning of the list. The header node is actually a dummy node with no actual
data but rather acts as a reference point to the first node in the list.
Linked Lists & Arrays
• Here’s the comparison of Linked List vs Arrays
Linked List
• Data Structure: Non-contiguous
• Memory Allocation: Dynamic
• Insertion/Deletion: Efficient
• Access: Sequential
Array
• Data Structure: Contiguous Access: Random
• Memory Allocation: Static
• Insertion/Deletion: Inefficien

You might also like