009.1 Data Structure - II

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

for more updates visit: www.python4csip.

com

DATA STRUCTURE - II
Stack and Queues
for more updates visit: www.python4csip.com

STACK

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
A stack is a collection of data items that can be
accessed at only one end, called top.
Items can be inserted and deleted in a stack only
at the top.
The last item inserted in a stack is the first one
to be deleted.
Therefore, a stack is called a Last-In-First-Out
(LIFO) data structure.
2 mains operations on Stack is PUSH & POP
PUSH means inserting new item at top and POP
means deleting item from top.
for more updates visit: www.python4csip.com

OTHER STACK TERM

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
 Peek : getting the most recent value of stack i.e
value at TOP

 OverFlow : a situation when we are Pushing item


in Stack that is full.

 Underflow : a situation when we are Popping


item from empty stack
for more updates visit: www.python4csip.com

IMPLEMENTING STACK IN PYTHON

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
This function will
check Stack is
empty or not

This function will


add new item in
Stack, here setting
top is mandatory

This function is used


to remove item from
stack, also perform
checks before deletion
for more updates visit: www.python4csip.com

IMPLEMENTING STACK IN PYTHON

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
This function will
return the top
most item from the
stack

This function will


display stack items
for more updates visit: www.python4csip.com

IMPLEMENTING STACK IN PYTHON

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
Displaying menu
to user to interact
for more updates visit: www.python4csip.com

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
IMPLEMENTING STACK IN PYTHON
for more updates visit: www.python4csip.com

QUEUE

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
 Queue is a linear list which follows FIFO
approach.
 Queue allows addition of element only at one end
called REAR (end of list) & Deletion of element
only from FRONT end (beginning of list).
 The operation of addition and deletion is known
as Enqueue and Dequeue respectively.
 Applications of Queue:
 Printer Spooling
 CPU Scheduling
 Mail Service
 Keyboard Buffering
 Elevator
for more updates visit: www.python4csip.com

IMPLEMENTING QUEUE IN PYTHON


:: QUEUE OPERATIONS ::

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
 Peek : getting first value of QUEUE i.e. of
FRONT position.
e.g. Queue[Front] # Front is an int storing index of first element of queue

 Enqueue: addition of new item in QUEUE at


REAR position.
e.g. Queue.append(Item)

 Dequeue: removal of item from the beginning of


QUEUE.
e.g. Queue.pop(0)
for more updates visit: www.python4csip.com

PYTHON CODE: QUEUE

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
This function will
check Queue is
empty or not

This function will


add new item in
Queue, here setting
top is mandatory

This function is used


to remove item from
Queue, also perform
checks before deletion
for more updates visit: www.python4csip.com

PYTHON CODE: QUEUE

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
This function will
return the Front
item from the
Queue

This function will


display Queue
items
for more updates visit: www.python4csip.com

PYTHON CODE: QUEUE

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
Displaying menu
to user to interact
for more updates visit: www.python4csip.com

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
PYTHON CODE: QUEUE
for more updates visit: www.python4csip.com

VARIATIONS OF QUEUE

SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
 Circular Queue : it is implemented in circular
form rather than straight line. It is used in many
programming language to overcome the problems
of linear queue (utilization of unused position in
the beginning).
 Dequeue (Doubly-ended queue) : it is the form of
queue in which elements can be added or
removed from any end but not in the middle. It is
of 2 type: (1) Input restricted (2) Output
restricted
 Input restricted means insertion only at one end but
deletion from both end
 Output restricted means deletion from one end and
insertion from both end

You might also like