01 Linear Data Structures
01 Linear Data Structures
Module 1
Linear Data Structures
Data structures
Integer
Queue
Float
Hash Table
Character
Boolean Non-linear Tree
Graph
This makes it easier to calculate the position of each element by simply adding an
offset to a base value
Accessing • O(1)
Searching • O(n)
Inserting • O(n)
Deleting • O(n)
X(2)
Name of the array is also a pointer to X(4)
the first element of array
Complexity of accessing is O(1)
Search for 3
Insert 6 at location 2
5 7 8 1
For delete by value in an unsorted 0 1 2 3
array, the element to be deleted is
searched using the linear search, and Delete 3
then delete operation is performed
followed by shifting the elements Complexity of deleting is O(n)
Accessing • O(n)
Deleting • O(n)
Advantages Applications
• can be traversed in both forward and backward directions 1. Web browsers for backward and forward
• delete operation is more efficient if a pointer to the node to navigation of web pages
be deleted is given 2. Cache are constructed using Doubly
• In a singly linked list, to delete a node, a pointer to the Linked Lists
previous node is needed. To get this previous node, 3. Maintain undo and redo functionalities
sometimes the list is traversed. In DLL, we can get the 4. Thread scheduler to keep track of processes
previous node using the previous pointer.
that are being executed at that time
Applications
Advantages
1. Round-Robin Scheduling
• entire list can be traversed from any 2. Music and Media Players
node of the list 3. Simulations or games where objects
• saves time when we have to go to the interact in a circular
first node from the last node 4. Implementation of a Circular Buffer
isEmpty
• Returns true if the stack is empty, else false.
push(), pop(), peek() and isEmpty() all take O(1) time.
Department of Electronics And Electrical Engineering, IIT Guwahati
Applications of Stack
Redo-Undo Features
Evaluation of Arithmetic Expressions
Processing Function Calls
Forward and Backward Feature in Web Browsers
Backtracking
String Reversal
Department of Electronics And Electrical Engineering, IIT Guwahati
Implementation
array
implement
a stack
linked list
Array Implementation
Pros Cons
• Easy to implement. • It is not dynamic.
• Memory is saved as pointers • It doesn’t grow and shrink
are not involved. depending on needs at runtime.
-1
123405
0 1 2 3 4 5 Top
5 Push(4) 5 3 8 4
Push(5)
5 3 Push(9) 5 3 8 4 9
Push(3)
5 3 8 Push(2) 5 3 8 4 9 2
Push(8)
Push(6)
Department of Electronics And Electrical Engineering, IIT Guwahati Stack Overflow
Array Implementation - Pop
435
0 1 2 3 4 5 Top
5 3 8 4 9 2
Pop() 5 3 8 4 9
Pop() 5 3 8 4
STRESSED D D E S S E R T S
E
S
S
E
R
T
S
• Infix, Postfix and Prefix notations are three different but equivalent ways of
writing expressions.
• Infix notation: Operators are written in-between their operands.
• X+Y
• A* ( B+C) /D X Y operands
+ operator
• Postfix notation: Operators are written after their operands.
• XY+
• ABC+* D /
• Prefix notation: Operators are written before their operands.
• +XY
• /*A+BCD
Department of Electronics And Electrical Engineering, IIT Guwahati
Conversion Using Stacks
-1
123405
0 1 2 3 4 5 Peek /
Front
5 Enqueue(4) 4 8 3 5
Enqueue(5)
3 5 Enqueue(9) 9 4 8 3 5
Enqueue(3)
8 3 5 Enqueue(2) 2 9 4 8 3 5
Enqueue(8)
435
0 1 2 3 4 5 Peek /
Front
Solution:
A stack can be implemented using two queues
Let stack to be implemented be s and queues used to implement s be q1 and q2.
s can be implemented in two ways
Solution:
• A queue can be implemented using two stacks.
• Let queue to be implemented be q and stacks used to implement q be s1 and s2.
• q can be implemented in two ways
s1 5 4 3 2 1 s1 4 3 2 1
• Push the element to
be added to stack1
s2 s2 5
To perform dequeue
operation
s1 3 2 1 s1 1