SlideShare a Scribd company logo
M269
Algorithms, Data
Structures and
Computability
Agenda
• Introduction to Data Structures.
• ADT vs. DS.
• Python Built-in Data Structures (Lists, Tuples, Sets, Dict).
• Famous Data Structures:
Stack.
Queue.
Linked Lists && Doubly Linked Lists.
Dictionaries in
Python
Dictionaries (I)
• Store pairs of entries called items
{ 'CS' : '743-713-3350', 'UHPD' : '713-743-3333'}
• Each pair of entries contains
• A key
• A value
• Key and values are separated by a colon
• Paris of entries are separated by commas
• Dictionary is enclosed within curly braces
Usage
• Keys must be unique within a dictionary
• No duplicates
• If we have
age = {'Alice' : 25, 'Bob' :28}
then
age['Alice'] is 25 #is used for checking the value
and
age['Bob'] is 28
Dictionaries are mutable
• >>> age = {'Alice' : 25, 'Bob' : 28}
• >>> saved = age
• >>> age['Bob'] = 29
• >>> age
{'Bob': 29, 'Alice': 25}
• >>> saved
{'Bob': 29, 'Alice': 25}
Keys must be unique
• >>> age = {'Alice' : 25, 'Bob' : 28, 'Alice' : 26}
• >>> age
{'Bob': 28, 'Alice': 26} # if two value for the same key  the
last one is
the one that will be saved
Displaying contents
• >>> age = {'Alice' : 25, 'Carol': 'twenty-two'}
• >>> age.items()
dict_items([ ('Alice', 25), ('Carol', 'twenty-two')])
• >>> age.keys()
dict_keys([ 'Alice', 'Carol'])
• age.values()
dict_values([28, 25, 'twenty-two'])
Updating directories
• >>> age = {'Alice': 26 , 'Carol' : 22}
The update function either update existing value or create it if no
exist
• >>> age.update({'Bob' : 29})
• >>> age
{'Bob': 29, 'Carol': 22, 'Alice': 26}
• >>> age.update({'Carol' : 23})
• >>> age
{'Bob': 29, 'Carol': 23, 'Alice': 26}
Returning a value
• >>> age = {'Bob': 29, 'Carol': 23, 'Alice': 26}
• >>> age.get('Bob')
29
• >>> age['Bob']
29
Removing a specific item (I)
• >>> a = {'Alice' : 26, 'Carol' : 'twenty-two'}
• >>> a
{'Carol': 'twenty-two', 'Alice': 26}
• # pop function take the key remove the key and value from the dictionary and return the
value
• >>> a.pop('Carol’)
'twenty-two'
• >>> a
{'Alice': 26}
Removing a specific item (II)
• >>> a.pop('Alice')
26
• >>> a
{}
Remove the first item
# popitem()  it will select the first item in the dictionary to return and remove it
• >>> age = {'Bob': 29, 'Carol': 23, 'Alice': 26}
• >>> age.popitem()
• ('Bob', 29)
• >>> age
• {'Carol': 23, 'Alice': 26}
• >>> age.popitem()
('Carol', 23)
• >>> age
{'Alice': 26}
14
Data Types & Data Structures
• Applications/programs read data, store data temporarily,
process it and finally output results.
• What is data? Numbers, Characters, etc.
Application/
Program
Input
data
Output
data
Introduction to Data Structures
16
The Need for Data Structures
• Data structures organize data
 more efficient programs.
• More powerful computers  more complex applications.
• More complex applications demand more calculations.
• Complex computing tasks are unlike our everyday experience.
• Any organization for a collection of records can be searched,
processed in any order, or modified.
• The choice of data structure and algorithm can make the difference
between a program running in a few seconds or many days.
17
Efficiency
A solution is said to be efficient if it solves the problem within its
resource constraints.
• Space
• Time
• The cost of a solution is the amount of resources that the
solution consumes… expressed in terms of big O notation
18
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the resource
constraints a solution must meet.
2. Determine the basic operations that must be supported.
Quantify the resource constraints for each operation.
3. Select the data structure that best meets these
requirements.
19
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.
Each ADT operation is defined by its inputs and outputs.
Encapsulation: Hide implementation details.
20
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.
• Data structure usually refers to an organization for data in main memory.
• File structure is an organization for data on peripheral storage, such as a disk
drive.
• Data structure is representation of the logical relationship existing between
individual elements of data.
• In other words, a data structure is a way of organizing all data items that
considers not only the elements stored but also their relationship to each other.
21
Logical vs. Physical Form
Data items have both a logical and a physical form.
Logical form: definition of the data item within an ADT.
• Ex: Integers in mathematical sense: +, -
Physical form: implementation of the data item within a data
structure.
• Ex: 16/32 bit integers, overflow.
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
• The most commonly used operation on data
structure are broadly categorized into following
types:
• Create.
• Insert
• Delete
• update
Some DS need extra functions
• Selection.
• Searching.
• Sorting.
• Merging.
Insertion needs sometime
• Index.
• Key.
• Position.
• Priority.
Common Data Structures
• List
• Set
• Tuple
• Dictionary
• Stack
• Queue
• Linked List
• Tree
• Heap
• Hash Table
• Priority Queue
this lecture
through next lectures
https://www.youtube.com/watch?v=R-HLU9Fl5ug
Video tutorial: (in Python)
https://www.programiz.com/python-programming/list
Interactive tutorial: (in Python)
https://www.programiz.com/python-programming/set
https://www.programiz.com/python-programming/tuple
https://www.programiz.com/python-programming/dictionary
Stack Data Structure
Visualize the Stack:
https://visualgo.net/en/list
26
The Stack ADT
• The Stack ADT stores arbitrary
objects
• Insertions and deletions follow
the last-in first-out scheme
• Think of a spring-loaded plate
dispenser
• Main stack operations:
• push(object): inserts an element
• object pop(): removes and returns
the last inserted element
• Auxiliary stack operations:
• object top(): returns the last
inserted element without
removing it
• integer len(): returns the
number of elements stored
• boolean is_empty(): indicates
whether no elements are stored
Example
Applications of Stacks
• 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
• A simple way of implementing the Stack ADT uses a list
• We add elements from left to right
• A variable keeps track of the index of the top element
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
Visualize the Queue:
https://visualgo.net/en/list
The Queue ADT
• The Queue ADT stores arbitrary
objects
• Insertions and deletions follow
the first-in first-out scheme
• Insertions are at the rear of the
queue and removals are at the
front of the queue
• Main queue operations:
• enqueue(object): inserts an
element at the end of the queue
• object dequeue(): removes and
returns the element at the
front of the queue
• Auxiliary queue
operations:
• object first(): returns the
element at the front
without removing it
• integer len(): returns the
number of elements stored
• boolean is_empty():
indicates whether no
elements are stored
34
Example
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
• Use the following three instance variables:
• _data: is a reference to a list instance with a fixed capacity.
• _size: is an integer representing the current number of elements
stored in the queue (as opposed to the length of the data list).
• _front: is an integer that represents the index within data of the
first element of the queue (assuming the queue is not empty).
Queue in Python, Beginning
Queue in Python, Continued
Linked List Data Structure
Visualize the Queue:
https://visualgo.net/en/list
Singly Linked List
A singly linked list is a
concrete data structure
consisting of a sequence of
nodes, starting from a head
pointer
Each node stores
element
link to the next node
next
elem node
A B C D

head
Inserting at the Head
1. Allocate a new node
2. Insert new element
3. Have new node point to old
head
4. Update head to point to new
node
Removing at the Head
1. Update head to point
to next node in the
list
2. Allow garbage
collector to reclaim
the former first node
Inserting at the Tail
1. Allocate a new node
2. Insert new element
3. Have new node
point to null
4. Have old last node
point to new node
5. Update tail to point
to new node
Removing at the Tail
Removing at the tail
of a singly linked list
is not efficient!
There is no
constant-time way
to update the tail to
point to the previous
node
Linked Lists
Stack as a Linked List
We can implement a stack with a singly linked list
The top element is stored at the first node of the list
The space used is O(n) and each operation of the Stack ADT
takes O(1) time

t
nodes
elements
Linked-List Stack in Python
Linked Lists
Queue as a Linked List
We can implement a queue with a singly linked list
The front element is stored at the first node
The rear element is stored at the last node
The space used is O(n) and each operation of the Queue ADT
takes O(1) time
f
r

nodes
elements
Linked-List Queue in Python
Linked Lists 48
Doubly Linked List Data Struture
Doubly-Linked Lists
Doubly Linked List
• A doubly linked list provides a natural
implementation of the Node List ADT
• Nodes implement Position and store:
• element
• link to the previous node
• link to the next node
• Special trailer and header nodes
prev next
elem
trailer
header nodes/positions
elements
node
Visualize the Doubly linked List:
https://visualgo.net/en/list
Insertion
• Insert a new node, q, between p and its successor.
A B X C
A B C
p
A B C
p
X
q
p q
Deletion
• Remove a node, p, from a doubly-linked list.
A B C D
p
A B C
D
p
A B C
Doubly-Linked List in Python
data structures queue stack insert and delete time complexity

More Related Content

Similar to data structures queue stack insert and delete time complexity (20)

FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
AntareepMajumder
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
Yonas D. Ebren
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introduction
Mandeep Singh
 
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
NathanielAdika
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
JohnStuart83
 
Data Structures and Algorithms: introduction
Data Structures and Algorithms: introductionData Structures and Algorithms: introduction
Data Structures and Algorithms: introduction
superhy199148
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptx
OnkarModhave
 
Data Exploration in R.pptx
Data Exploration in R.pptxData Exploration in R.pptx
Data Exploration in R.pptx
Ramakrishna Reddy Bijjam
 
introduction of Data strutter and algirithm.pptx
introduction of Data strutter and algirithm.pptxintroduction of Data strutter and algirithm.pptx
introduction of Data strutter and algirithm.pptx
ssuser7b3003
 
Data_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptxData_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptx
aamirali1061a
 
Data_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptxData_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptx
aamirali1061a
 
Data structures
Data structuresData structures
Data structures
Manaswi Sharma
 
12888239 (2).ppt
12888239 (2).ppt12888239 (2).ppt
12888239 (2).ppt
SrinivasanCSE
 
Introduction to datastructures presentation
Introduction to datastructures presentationIntroduction to datastructures presentation
Introduction to datastructures presentation
krishkiran2408
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
blessyboban92
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
sarala9
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
NagendraK18
 
Data structure Unit-I Part A
Data structure Unit-I Part AData structure Unit-I Part A
Data structure Unit-I Part A
SSN College of Engineering, Kalavakkam
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
MathewJohnSinoCruz
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
Ashok280385
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
AntareepMajumder
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
Yonas D. Ebren
 
1. Data structures introduction
1. Data structures introduction1. Data structures introduction
1. Data structures introduction
Mandeep Singh
 
Data Structures and Algorithms: introduction
Data Structures and Algorithms: introductionData Structures and Algorithms: introduction
Data Structures and Algorithms: introduction
superhy199148
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptx
OnkarModhave
 
introduction of Data strutter and algirithm.pptx
introduction of Data strutter and algirithm.pptxintroduction of Data strutter and algirithm.pptx
introduction of Data strutter and algirithm.pptx
ssuser7b3003
 
Data_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptxData_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptx
aamirali1061a
 
Data_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptxData_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptx
aamirali1061a
 
Introduction to datastructures presentation
Introduction to datastructures presentationIntroduction to datastructures presentation
Introduction to datastructures presentation
krishkiran2408
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
blessyboban92
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
sarala9
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
NagendraK18
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
Ashok280385
 

Recently uploaded (20)

Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Ad

data structures queue stack insert and delete time complexity

  • 2. Agenda • Introduction to Data Structures. • ADT vs. DS. • Python Built-in Data Structures (Lists, Tuples, Sets, Dict). • Famous Data Structures: Stack. Queue. Linked Lists && Doubly Linked Lists.
  • 4. Dictionaries (I) • Store pairs of entries called items { 'CS' : '743-713-3350', 'UHPD' : '713-743-3333'} • Each pair of entries contains • A key • A value • Key and values are separated by a colon • Paris of entries are separated by commas • Dictionary is enclosed within curly braces
  • 5. Usage • Keys must be unique within a dictionary • No duplicates • If we have age = {'Alice' : 25, 'Bob' :28} then age['Alice'] is 25 #is used for checking the value and age['Bob'] is 28
  • 6. Dictionaries are mutable • >>> age = {'Alice' : 25, 'Bob' : 28} • >>> saved = age • >>> age['Bob'] = 29 • >>> age {'Bob': 29, 'Alice': 25} • >>> saved {'Bob': 29, 'Alice': 25}
  • 7. Keys must be unique • >>> age = {'Alice' : 25, 'Bob' : 28, 'Alice' : 26} • >>> age {'Bob': 28, 'Alice': 26} # if two value for the same key  the last one is the one that will be saved
  • 8. Displaying contents • >>> age = {'Alice' : 25, 'Carol': 'twenty-two'} • >>> age.items() dict_items([ ('Alice', 25), ('Carol', 'twenty-two')]) • >>> age.keys() dict_keys([ 'Alice', 'Carol']) • age.values() dict_values([28, 25, 'twenty-two'])
  • 9. Updating directories • >>> age = {'Alice': 26 , 'Carol' : 22} The update function either update existing value or create it if no exist • >>> age.update({'Bob' : 29}) • >>> age {'Bob': 29, 'Carol': 22, 'Alice': 26} • >>> age.update({'Carol' : 23}) • >>> age {'Bob': 29, 'Carol': 23, 'Alice': 26}
  • 10. Returning a value • >>> age = {'Bob': 29, 'Carol': 23, 'Alice': 26} • >>> age.get('Bob') 29 • >>> age['Bob'] 29
  • 11. Removing a specific item (I) • >>> a = {'Alice' : 26, 'Carol' : 'twenty-two'} • >>> a {'Carol': 'twenty-two', 'Alice': 26} • # pop function take the key remove the key and value from the dictionary and return the value • >>> a.pop('Carol’) 'twenty-two' • >>> a {'Alice': 26}
  • 12. Removing a specific item (II) • >>> a.pop('Alice') 26 • >>> a {}
  • 13. Remove the first item # popitem()  it will select the first item in the dictionary to return and remove it • >>> age = {'Bob': 29, 'Carol': 23, 'Alice': 26} • >>> age.popitem() • ('Bob', 29) • >>> age • {'Carol': 23, 'Alice': 26} • >>> age.popitem() ('Carol', 23) • >>> age {'Alice': 26}
  • 14. 14 Data Types & Data Structures • Applications/programs read data, store data temporarily, process it and finally output results. • What is data? Numbers, Characters, etc. Application/ Program Input data Output data
  • 15. Introduction to Data Structures
  • 16. 16 The Need for Data Structures • Data structures organize data  more efficient programs. • More powerful computers  more complex applications. • More complex applications demand more calculations. • Complex computing tasks are unlike our everyday experience. • Any organization for a collection of records can be searched, processed in any order, or modified. • The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.
  • 17. 17 Efficiency A solution is said to be efficient if it solves the problem within its resource constraints. • Space • Time • The cost of a solution is the amount of resources that the solution consumes… expressed in terms of big O notation
  • 18. 18 Selecting a Data Structure Select a data structure as follows: 1. Analyze the problem to determine the resource constraints a solution must meet. 2. Determine the basic operations that must be supported. Quantify the resource constraints for each operation. 3. Select the data structure that best meets these requirements.
  • 19. 19 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. Each ADT operation is defined by its inputs and outputs. Encapsulation: Hide implementation details.
  • 20. 20 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. • Data structure usually refers to an organization for data in main memory. • File structure is an organization for data on peripheral storage, such as a disk drive. • Data structure is representation of the logical relationship existing between individual elements of data. • In other words, a data structure is a way of organizing all data items that considers not only the elements stored but also their relationship to each other.
  • 21. 21 Logical vs. Physical Form Data items have both a logical and a physical form. Logical form: definition of the data item within an ADT. • Ex: Integers in mathematical sense: +, - Physical form: implementation of the data item within a data structure. • Ex: 16/32 bit integers, overflow.
  • 22. 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.
  • 23. Functions of Data Structures • The most commonly used operation on data structure are broadly categorized into following types: • Create. • Insert • Delete • update Some DS need extra functions • Selection. • Searching. • Sorting. • Merging. Insertion needs sometime • Index. • Key. • Position. • Priority.
  • 24. Common Data Structures • List • Set • Tuple • Dictionary • Stack • Queue • Linked List • Tree • Heap • Hash Table • Priority Queue this lecture through next lectures https://www.youtube.com/watch?v=R-HLU9Fl5ug Video tutorial: (in Python) https://www.programiz.com/python-programming/list Interactive tutorial: (in Python) https://www.programiz.com/python-programming/set https://www.programiz.com/python-programming/tuple https://www.programiz.com/python-programming/dictionary
  • 25. Stack Data Structure Visualize the Stack: https://visualgo.net/en/list
  • 26. 26 The Stack ADT • The Stack ADT stores arbitrary objects • Insertions and deletions follow the last-in first-out scheme • Think of a spring-loaded plate dispenser • Main stack operations: • push(object): inserts an element • object pop(): removes and returns the last inserted element • Auxiliary stack operations: • object top(): returns the last inserted element without removing it • integer len(): returns the number of elements stored • boolean is_empty(): indicates whether no elements are stored
  • 28. Applications of Stacks • 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
  • 29. List-based Stack • A simple way of implementing the Stack ADT uses a list • We add elements from left to right • A variable keeps track of the index of the top element S 0 1 2 t …
  • 30. 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)
  • 32. Queue Data Structure Visualize the Queue: https://visualgo.net/en/list
  • 33. The Queue ADT • The Queue ADT stores arbitrary objects • Insertions and deletions follow the first-in first-out scheme • Insertions are at the rear of the queue and removals are at the front of the queue • Main queue operations: • enqueue(object): inserts an element at the end of the queue • object dequeue(): removes and returns the element at the front of the queue • Auxiliary queue operations: • object first(): returns the element at the front without removing it • integer len(): returns the number of elements stored • boolean is_empty(): indicates whether no elements are stored
  • 35. 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
  • 36. Queue in Python • Use the following three instance variables: • _data: is a reference to a list instance with a fixed capacity. • _size: is an integer representing the current number of elements stored in the queue (as opposed to the length of the data list). • _front: is an integer that represents the index within data of the first element of the queue (assuming the queue is not empty).
  • 37. Queue in Python, Beginning
  • 38. Queue in Python, Continued
  • 39. Linked List Data Structure Visualize the Queue: https://visualgo.net/en/list
  • 40. Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes, starting from a head pointer Each node stores element link to the next node next elem node A B C D  head
  • 41. Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Have new node point to old head 4. Update head to point to new node
  • 42. Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node
  • 43. Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node
  • 44. Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node
  • 45. Linked Lists Stack as a Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time  t nodes elements
  • 47. Linked Lists Queue as a Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time f r  nodes elements
  • 48. Linked-List Queue in Python Linked Lists 48
  • 49. Doubly Linked List Data Struture
  • 50. Doubly-Linked Lists Doubly Linked List • A doubly linked list provides a natural implementation of the Node List ADT • Nodes implement Position and store: • element • link to the previous node • link to the next node • Special trailer and header nodes prev next elem trailer header nodes/positions elements node Visualize the Doubly linked List: https://visualgo.net/en/list
  • 51. Insertion • Insert a new node, q, between p and its successor. A B X C A B C p A B C p X q p q
  • 52. Deletion • Remove a node, p, from a doubly-linked list. A B C D p A B C D p A B C

Editor's Notes

  • #16: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #17: Alternate definition: Better than known alternatives (“relatively efficient”). Space and time are typical constraints for programs. This does not mean always strive for the most efficient program. If the program operates well within resource constraints, there is no benefit to making it faster or smaller.
  • #18: Typically want the “simplest” data structure that will meet the requirements.
  • #46: slot is nothing more than a memory management nicety: when you define __slots__ on a class, you’re telling the Python interpreter that the list of attributes described within are the only attributes this class will ever need, and a dynamic dictionary is not needed to manage the references to other objects within the class
  • #47: Insert at the trail Remove at the head