Chapter 3
Chapter 3
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.
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