Unit 2: Linear Data Structures
Lecturer: Shimirwa Aline Valerie
Content
UNIT 1(Introduction To Algorithms And Data Structures)
Algorithms: Definition, Properties, Performance Analysis, Space Complexity, Time Complexity,
Asymptotic Notations. Data structures: Introduction, Data Structures types, DS Operations.
UNIT 2(Linear Data Structures)
Arrays, Stacks: Introduction, Stack Operations, Applications: Infix to Postfix Conversion, Evaluation
of Postfix Expression. Queues: Introduction, Operations on queues, Circular queues, Priority queues.
Linked lists: Introduction, Singly linked lists, Circular linked lists, Doubly linked lists, Applications.
Implementation of Stack and Queue using linked list.
UNIT 3(Non-linear Data Structure)
Trees: Basic terminology, Binary Trees, Binary Tree Representation, Graphs: Terminology, Sequential
and linked Representations of graphs, Graph Traversal : Depth First Search and Breadth First Search,
Spanning Trees, Spanning Trees: Prims and Kruskal algorithm
Content
UNIT 4(Sorting and Searching Algorithm)
Sorting: Introduction, Selection sort, Bubble sort, Insertion sort, Merge sort, Quick sort, Heap Sort.
Searching: Introduction, Linear search, Binary search, Hash table methods, Hashing Functions.
Basic Operations on Stack
Stack In order to make manipulations in a stack, there are
certain operations provided to us:
What is Stack? push(): to insert an element into the stack
A stack is a linear data structure in which the insertion
pop(): to remove an element from the stack
of a new element and removal of an existing element
takes place at the same end represented as the top of Peek() or top(): Returns the top element of the
the stack. stack.
To implement the stack, it is required to maintain isEmpty(): returns true if stack is empty else
the pointer to the top of the stack, which is the last false.
element to be inserted because we can access the
elements only on the top of the stack. size() returns the size of stack.
Stack uses LIFO(Last In First out) strategy to insert or
delete element from it. This strategy states that the
element that is inserted last will come out first. You
can take a pile of plates kept on top of each other as a
real-life example. The plate which we put last is on
the top and since we remove the plate that is at the
top, we can say that the plate that was put last comes
(push operation)
Push operation adds an item to the stack. If the stack
is full, then it is said to be an Overflow condition.
Algorithm for push:
(pop operation)
pop operation removes an item from the stack. The
items are popped in the reversed order in which they
are pushed. If the stack is empty, then it is said to be
an Underflow condition.
Algorithm for pop:
isEmpty operation
isEmpty operation returns true if the stack
(peek operation) is empty, else false.
Algorithm for isEmpty:
Peek or top operation returns the top element of the
stack without deleting it.
Algorithm for peek:
Understanding stack practically:
There are many real-life examples of a stack.
Consider the simple example of plates stacked
over one another in a canteen. The plate which
is at the top is the first one to be removed, i.e.
the plate which has been placed at the
bottommost position remains in the stack for the
longest period of time. So, it can be simply seen
to follow the LIFO/FILO order.
In addition to these two main types, there are several
other variations of Stacks, including:
Types of Stacks: 1.Infix to Postfix Stack: This type of stack is used to
convert infix expressions to postfix expressions.
Fixed Size Stack: As the name suggests, a fixed size
stack has a fixed size and cannot grow or shrink 2.Expression Evaluation Stack: This type of stack is
used to evaluate expressions.
dynamically. If the stack is full and an attempt is
made to add an element to it, an overflow error 3.Recursion Stack: This type of stack is used to keep
occurs. If the stack is empty and an attempt is made track of function calls in a computer program and to
to remove an element from it, an underflow error return control to the correct function when a function
occurs. This type of stack is implemented using an returns.
array. 4.Memory Management Stack: This type of stack is
Dynamic Size Stack: A dynamic size stack can used to store the values of the program counter and the
values of the registers in a computer program, allowing
grow or shrink dynamically. When the stack is full, the program to return to the previous state when a
it automatically increases its size to accommodate function returns.
the new element, and when the stack is empty, it
decreases its size. This type of stack is implemented 5.Balanced Parenthesis Stack: This type of stack is
used to check the balance of parentheses in an
using a linked list, as it allows for easy resizing of expression.
the stack.
6.Undo-Redo Stack: This type of stack is used in
computer programs to allow users to undo and redo
actions.
Application of Stack
Browser history: Web browsers use stacks to
Function calls and recursion: When a function is keep track of the web pages you visit. Each time
called, the current state of the program is pushed you visit a new page, the URL is pushed onto the
onto the stack. When the function returns, the state stack, and when you hit the back button, the
is popped from the stack to resume the previous previous URL is popped from the stack.
function’s execution.
Balanced Parentheses: Stack data structure is
Undo/Redo operations: The undo-redo feature in used to check if parentheses are balanced or not.
various applications uses stacks to keep track of the An opening parenthesis is pushed onto the stack,
previous actions. Each time an action is performed, and a closing parenthesis is popped from the
it is pushed onto the stack. To undo the action, the stack. If the stack is empty at the end of the
top element of the stack is popped, and the reverse expression, the parentheses are balanced.
operation is performed.
Backtracking Algorithms: The backtracking
Expression evaluation: Stack data structure is used algorithm uses stacks to keep track of the states of
to evaluate expressions in infix, postfix, and prefix the problem-solving process. The current state is
notations. Operators and operands are pushed onto pushed onto the stack, and when the algorithm
the stack, and operations are performed based on the backtracks, the previous state is popped from the
stack’s top elements. stack.
Advantages of Stack:
Easy implementation: Stack data structure is easy Supports backtracking: Stack data structure
to implement using arrays or linked lists, and its supports backtracking algorithms, which are used
operations are simple to understand and implement. in problem-solving to explore all possible
solutions by storing the previous states.
Efficient memory utilization: Stack uses a
contiguous block of memory, making it more Enables undo/redo operations: Stack data
efficient in memory utilization as compared to other structure is used to enable undo and redo
data structures. operations in various applications like text
editors, graphic design tools, and software
Fast access time: Stack data structure provides fast
development environments.
access time for adding and removing elements as the
elements are added and removed from the top of the
stack.
Helps in function calls: Stack data structure is used
to store function calls and their states, which helps
in the efficient implementation of recursive function
calls.
Disadvantages of Stack:
Not suitable for certain applications: Stack
Limited capacity: Stack data structure has a limited data structure is not suitable for applications that
capacity as it can only hold a fixed number of require accessing elements in the middle of the
elements. If the stack becomes full, adding new stack, like searching or sorting algorithms.
elements may result in stack overflow, leading to the
loss of data. Stack overflow and underflow: Stack data
structure can result in stack overflow if too many
No random access: Stack data structure does not elements are pushed onto the stack, and it can
allow for random access to its elements, and it only result in stack underflow if too many elements are
allows for adding and removing elements from the popped from the stack.
top of the stack. To access an element in the middle
of the stack, all the elements above it must be Recursive function calls limitations: While
removed. stack data structure supports recursive function
calls, too many recursive function calls can lead
Memory management: Stack data structure uses a to stack overflow, resulting in the termination of
contiguous block of memory, which can result in the program.
memory fragmentation if elements are added and
removed frequently.
Implementation of stack
using array
Stack can be implemented using array. Below figure
is the main functions where we can call the function
defined on the right part of the slide:
Implementation of stack
using Linked List
Stack can be also implemented using Linked List.
Below figure is the main function where we can call
the functions defined on the right part of the slide:
Implementation of stack
(Complexity Analysis)
Figure in this slide represents time complexity of
different operations on stack.
Queue
A queue is a linear data structure that is open at both ends
and the operations are performed in First In First Out
(FIFO)order.
We define a queue to be a list in which all additions to the list
are made at one end, and all deletions from the list are made
at the other end. The element which is first pushed into the
order, the delete operation is first performed on that.
FIFO Principle of Queue:
A Queue is like a line waiting to purchase tickets, where the
first person in line is the first person served. (i.e. First come
first serve).
Position of the entry in a queue ready to be served, that is, the
first entry that will be removed from the queue, is called
the front of the queue(sometimes, head of the queue),
similarly, the position of the last entry in the queue, that is,
the one most recently added, is called the rear (or the tail) of
the queue. See the below figure.
Basic Operations for Queue in
rear():This operation returns the element at
Data Structure the rear end without removing it.
Some of the basic operations for Queue in Data Structure isFull(): It validates if the queue is full.
are:
isNull(): It checks if the queue is empty.
Enqueue(): this operation Adds (or stores) an element
to the end of the queue..
Dequeue(): this operation removes of elements from
the queue.
Peek() or front()- Acquires the data element available
at the front node of the queue without deleting it.
Enqueue() operation
Enqueue() operation in Queue adds (or stores) an
element to the end of the queue.
The following steps should be taken to enqueue (insert)
data into a queue:
Step 1: Start
Step 2: Check if the queue is full.
Step 3: If the queue is full, return overflow error
and exit.
Step 4: If the queue is not full, increment the rear
pointer to point to the next empty space.
Step 5: Add the data element to the queue location, Complexity Analysis of enqueue operation
where the rear is pointing.
Step : End Time Complexity: O(1). Only a new node
is created and the pointer of the last node is
updated. This includes only memory
allocation operations. Hence it can be said
that insertion is done in constant time.
Dequeue() operation
Dequeue operation removes the first element from the
queue. The following steps are taken to perform the
dequeue operation:
Step 1: Check if the queue is empty.
Step 2: If the queue is empty, return the underflow
error and exit.
Step 3: If the queue is not empty, access the data
where the front is pointing.
Step 4: Increment the front pointer to point to the
next available data element.
Step 5: The Return success.
Complexity Analysis of dequeue operation:
Time Complexity: O(1). In array implementation,
only an arithmetic operation is performed i.e., the
front pointer is incremented by 1. This is a constant
time function.
Implementation of Queue
using array
Queue can be implemented using array. Below
figure is the main functions where we can call the
function defined on the right part of the slide:
Implementation of Queue
using linked list
Queue can be also implemented using linked list.
Below figure is the main functions where we can
call the function defined on the right part of the
slide: