Lec 3
Lec 3
Application/ Output
Input
Program data
data
3
Introduction to Data Structures
The Need for Data Structures
5
Efficiency
6
Selecting a Data Structure
7
Abstract Data Types
Abstract Data Type (ADT): a definition for a data type solely in terms of a
set of values and a set of operations on that data type.
8
Data Structure
• A data structure is the physical implementation of an ADT.
• Each operation associated with the ADT is implemented by one or more subroutines
in the implementation.
10
What is Program
• Data structure affects the design of both structural & functional aspects of a program.
Program=algorithm + Data Structure
• You know that a algorithm is a step by step procedure to solve a particular function.
• A Set of Instructions
• Data Structures + Algorithms
• Data Structure = A Container stores Data
• Algorithm = Logic + Control
• That means, algorithm is a set of instruction written to carry out certain tasks & the data structure is
the way of organizing the data with their logical relationship retained.
• To develop a program of an algorithm, we should select an appropriate data structure for that
algorithm.
• Therefore algorithm and its associated data structures from a program.
Functions of Data Structures
• List https://www.youtube.com/watch?v=R-HLU9Fl5ug
Interactive tutorial: (in Python)
• Set https://www.programiz.com/python-programming/list
• Tuple https://www.programiz.com/python-programming/set
https://www.programiz.com/python-programming/tuple
• Dictionary https://www.programiz.com/python-programming/dictionary
• Stack
• Queue this lecture
• Linked List
• Tree
• Heap through next lectures
• Hash Table
• Priority Queue
Stack Data Structure
• Direct applications
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Chain of method calls in a language that supports recursion
• Indirect applications
• Auxiliary data structure for algorithms
• Component of other data structures
List-based Stack
…
S
0 1 2 t
Performance and Limitations
• Performance
• Let n be the number of elements in the stack
• The space used is O(n)
• Each operation runs in time O(1) (amortized in
the case of a push)
List-based Stack in Python
Queue Data Structure
23
Applications of Queues
• Direct applications
• Waiting lists, bureaucracy
• Access to shared resources (e.g., printer)
• Multiprogramming
• Indirect applications
• Auxiliary data structure for algorithms
• Component of other data structures
Queue in Python
A B C D
Inserting at the Head
Linked Lists
elements
Linked-List Queue in Python
Linked Lists 37
Doubly Linked List Data Struture
Visualize the Doubly linked List:
Doubly Linked List https://visualgo.net/en/list
Doubly-Linked Lists
elements
Insertion
• Insert a new node, q, between p andpits successor.
A B C
A B q C
X
p q
A B X C
Deletion
• Remove a node, p, from a doubly-linked list.
p
A B C D
A B C p
A B C
Doubly-Linked List in Python
Thank You