0% found this document useful (0 votes)
40 views23 pages

Stacks - Quesues and Deques

This document is a lecture on data structures from Sara Elhishi at Mansoura University. It covers stacks, queues, and deques. Stacks follow LIFO order, with push and pop operations. Queues follow FIFO order, with enqueue and dequeue operations. Deques allow adding and removing from both ends. Examples of implementing each with Python lists are provided. Applications discussed include balancing parentheses with stacks and simulating a hot potato game with queues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views23 pages

Stacks - Quesues and Deques

This document is a lecture on data structures from Sara Elhishi at Mansoura University. It covers stacks, queues, and deques. Stacks follow LIFO order, with push and pop operations. Queues follow FIFO order, with enqueue and dequeue operations. Deques allow adding and removing from both ends. Examples of implementing each with Python lists are provided. Applications discussed include balancing parentheses with stacks and simulating a hot potato game with queues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Mansoura University

Faculty of Computers and Information


Department of Information System
First Semester- 2020-2021

[IS211P] Data Structures and Algorithms

Grade: 2
DR Sara S. Elhishi
Sara Shaker Elhishi

Lecture 7 – Stacks – Information Systems Department


sarashaker161@gmail.com
Queues - Deques sara_shaker2008@mans.edu.eg

Data Structure- Sara S. Elhishi 2


Linear Data
Structures

• Data Collections in which once an item is added, it stays in that position


relative to the other elements that came before and came after it.
3 • Stacks, queues, deques, and lists are examples
• It has Two Ends. From which items are added?

Data Structure- Sara S. Elhishi


Stack
Data Structure- Sara S. Elhishi 4
• A stack is a collection of objects that are inserted and
removed according to the last-in, first-out (LIFO) principle
• The order of insertion is the reverse of the order of
removal.
• used in many applications, such as the Internet Web
browsers history and the “undo” mechanism in Text editors

Data Structure- Sara S. Elhishi 5


The Stack Abstract Data
Type
• A stack instance S supports:
• stack()
• push(item)
• pop()
• peek()
• is_empty()
• size()

Data Structure- Sara S. Elhishi 6


Implementing
A Stack in
Python
• when we give an abstract data
type a physical implementation
we refer to the implementation
as a data structure.
• We will use a list class in Python
• assumes that the end of the list
will hold the top element of the
stack

Data Structure- Sara S. Elhishi 7


Stack class in
action

Data Structure- Sara S. Elhishi 8


Problem Solving • Balanced parentheses means that each opening symbol has a
corresponding closing symbol and the pairs of parentheses are
properly nested.
With Stacks: • write an algorithm that will read a string of parentheses from left
to right and decide whether the symbols are balanced.
Balance Parentheses
9 Data Structure- Sara S. Elhishi
Data Structure- Sara S. Elhishi 10
QUEUE
Data Structure- Sara S. Elhishi 11
12

Queue
The Queue
Abstract
Data Type

• queue()
• enqueue(item)

13 • dequeue()
• is_empty()
• size()
Data Structure- Sara S. Elhishi
Implementing A
Queue In Python
• The implementation assumes that the rear is
at position 0 in the list.

Data Structure- Sara S. Elhishi 14


Test

15
Data Structure- Sara S. Elhishi
Problem Solving With Queues: Hot Potato Game
Data Structure- Sara S. Elhishi 16
Simulation and Implementation

Data Structure- Sara S. Elhishi 17


DEQUES

Data Structure- Sara S. Elhishi 18


• New items can be added at either the front or the rear.
Deque: double- Likewise, existing items can be removed from either
end.

ended queue • It does not require the LIFO and FIFO orderings

19 Data Structure- Sara S. Elhishi


The Deque Abstract
Data Type
• deque()
• add_front(item)
• add_rear(item)
• remove_front()
• remove_rear()
• is_empty()
• size()

Data Structure- Sara S. Elhishi 20


Implementing a Deque
In Python
• Our implementation will assume that the rear of the
deque is at position 0 in the list.
• the pop(0) method must remove the first element of
the list

Data Structure- Sara S. Elhishi 21


Problem Solving: • A palindrome is a string that reads the same forward
Palindrome Checker and backward, for example, radar, toot, and madam.

Data Structure- Sara S. Elhishi 22


Thanks
2
Data Structure- Sara S. Elhishi 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy