First-In-First-Out - Data Structures - Lab, Exercises of Data Structures and Algorithms

Some concept of Data Structures are Data Structures, Dynamic Programming, First-In-First-Out, Implementation, Python Code. Main points of this lecture are: First-In-First-Out, Implementation, Understand, Python, Python List, Enqueue, Dequeue, Peek, Size, Time

Typology: Exercises

2012/2013

Uploaded on 04/30/2013

naji
naji 🇮🇳

4.3

(6)

87 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objective: To understand FIFO (First-In-First-Out) queue implementations in Python including being able to
determine the big-oh of each operation.
To start the lab: Download and unzip the file at:
Part A: The textbook’s QueueText implementation in lab3/queue_text.py uses a Python list
"Abstract Queue"
items:
Queue Object
List Objects
'w' 'w'
'x' 'x'
'y' 'y'
front front
rear rear
0 1 2
a) Complete the big-oh notation for the above QueueText implementation: ("n" is the # items)
Big-oh
__str__isEmpty( ) size( )peek( )dequeue( )enqueue(item)__init__
b) Explain your big-oh answer for enqueue(item).
c) Explain your big-oh answer for dequeue( )
d) Run the timeQueue.py file which times 100,000 enqueues followed by 100,000 dequeues.
Time for 100,000 enqueues: Time for 100,000 dequeues:
e) Why do the enqueues take so much more time?
After answering the above questions, raise you hand and explain your answers.
Part B: Complete the QueueAlt implementation in lab3/queue_alt.py uses a Python list
"Abstract Queue"
items:
QueueAlt Object
List Objects
'w' 'w'
'x' 'x'
'y' 'y'
front front
rear rear
0 1 2
a) Complete the big-oh notation for the above QueueAlt implementation: ("n" is the # items)
Big-oh
__str__isEmpty( ) size( )peek( )dequeue( )enqueue(item)__init__
b) Run the timeQueueAlt.py file which times 100,000 enqueues followed by 100,000 dequeues.
Time for 100,000 enqueues: Time for 100,000 dequeues:
After completing the QueueAlt class, answering the above questions, raise you hand and demonstrate your code.
Data Structures (CS 1520) Lab 3 Name:_________________
Lab
3
-
1
Docsity.com
pf2

Partial preview of the text

Download First-In-First-Out - Data Structures - Lab and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Objective: To understand FIFO (First-In-First-Out) queue implementations in Python including being able to determine the big-oh of each operation.

To start the lab: Download and unzip the file at:

Part A: The textbook’s QueueText implementation in lab3/queue_text.py uses a Python list

"Abstract Queue"

items:

Queue Object

List Objects

'w' 'x' 'y' (^) 'y' 'x' 'w'

front rear (^) rear front

0 1 2

a) Complete the big-oh notation for the above QueueText implementation: ("n" is the # items)

Big-oh

init enqueue(item) dequeue( ) peek( ) size( ) isEmpty( ) str

b) Explain your big-oh answer for enqueue(item).

c) Explain your big-oh answer for dequeue( )

d) Run the timeQueue.py file which times 100,000 enqueues followed by 100,000 dequeues. Time for 100,000 enqueues: Time for 100,000 dequeues:

e) Why do the enqueues take so much more time?

After answering the above questions, raise you hand and explain your answers.

Part B: Complete the QueueAlt implementation in lab3/queue_alt.py uses a Python list

"Abstract Queue"

items:

QueueAlt Object

List Objects

'w' 'x' 'y' (^) 'w' 'x' 'y'

front rear (^) front rear

0 1 2

a) Complete the big-oh notation for the above QueueAlt implementation: ("n" is the # items)

Big-oh

init enqueue(item) dequeue( ) peek( ) size( ) isEmpty( ) str

b) Run the timeQueueAlt.py file which times 100,000 enqueues followed by 100,000 dequeues. Time for 100,000 enqueues: Time for 100,000 dequeues:

After completing the QueueAlt class, answering the above questions, raise you hand and demonstrate your code.

Data Structures (CS 1520) Lab 3 Name:_________________

Docsity.com^ Lab^3 -^1

Part C: Consider the LinkedQueue implementation in lab3/linked_queue.py which uses a linked structure that looks like:

"Abstract Queue"

_front:

_rear:

_size: (^3)

data next^ data^ next^ data next 'w' 'x'^ 'y'

LinkedQueue Object Node Objects 'w' 'x' 'y' front rear

a) Draw the picture and number the steps for the enqueue method of the “normal” case (non-empty queue) above

b) Complete the enqueue method code for the “normal” case in the lab3/linked_queue.py file

c) Starting with the empty queue below, draw the resulting picture after your “normal” case code executes.

_front:

_rear:

_size: (^0)

empty LinkedQueue Object

d) Fix your “normal” case code to handle the “special case” of an empty queue.

_front:

_rear:

_size: (^3)

data next^ data^ next^ data next 'w' 'x'^ 'y'

LinkedQueue Object

e) Draw the picture and number the steps for the dequeue method of the “normal” case (non-empty queue) above

f) Complete the dequeue method code for the “normal” case in the lab3/linked_queue.py file

g) What “special case(s)” does the dequeue method code need to handle?

h) Complete the big-oh notation for the LinkedQueue methods: ("n" is the # items)

Big-oh

init enqueue(item) dequeue( ) peek( ) size( ) isEmpty( ) str

i) Run the timeLinkedQueue.py file which times 100,000 enqueues followed by 100,000 dequeues. Time for 100,000 enqueues: Time for 100,000 dequeues:

After thoroughly testing your linked implementation, raise you hand and demonstrate your queue.

Data Structures (CS 1520) Lab 3 Name:_________________

Docsity.com^ Lab^3 -^2