0% found this document useful (0 votes)
4 views

Data Structure Stack and Queue

Uploaded by

bdjsjdnxnsj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structure Stack and Queue

Uploaded by

bdjsjdnxnsj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II

Stack and Queue


In this tutorial we will discuss the following topics

S. No. Topics
1 Stack

2 Example: Implementing Stack In Python

3 Queue

4 Example: Implementation of Queue

Page 1 of 9
Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II


Linear data structures are collections of components arranged in a
straight line
1) Stack:
 A stack is a data structure that keeps objects in Last-In-First-Out (LIFO)
order
 Objects are added to the top
of the stack
 Only the top of the stack can
be accessed
 A pile of books or a stack of
dinner plates can be thought of examples of stacks.
It has three primitive operations:

 Push: Add an element to the stack

 Pop: Remove an element from the stack

 Peek: Get the topmost element of the stack

In Python, a stack is implemented using a list object.


 To push an item in the stack, use the list
function append list.append(item)
 To pop an item in the stack, use the list function pop list.pop()
 To get the top most item in the stack, write list[-1]

Page 2 of 9
Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II


Example: Implementing Stack In Python

Page 3 of 9
Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II

Page 4 of 9
Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II


OUTPUT

##### STACK OPERATIONS ##### ##### STACK OPERATIONS #####


1. PUSH- Insertion 1. PUSH- Insertion
2. POP- Deletion 2. POP- Deletion
3. PEEK- Show Top Element 3. PEEK- Show Top Element
4. DISPLAY - Show Stack 4. DISPLAY - Show Stack
0. EXIT 0. EXIT
Enter Your Choice: 1 Enter Your Choice: 3
Enter Element to Push: 20 Top Element : 88

##### STACK OPERATIONS ##### ##### STACK OPERATIONS #####


1. PUSH- Insertion 1. PUSH- Insertion
2. POP- Deletion 2. POP- Deletion
3. PEEK- Show Top Element 3. PEEK- Show Top Element
4. DISPLAY - Show Stack 4. DISPLAY - Show Stack
0. EXIT 0. EXIT
Enter Your Choice: 1 Enter Your Choice: 2
Enter Element to Push: 30 Deleted Element was: 88

##### STACK OPERATIONS ##### ##### STACK OPERATIONS #####


1. PUSH- Insertion
1. PUSH- Insertion
2. POP- Deletion
2. POP- Deletion
3. PEEK- Show Top Element
3. PEEK- Show Top Element
4. DISPLAY - Show Stack
4. DISPLAY - Show Stack
0. EXIT
0. EXIT
Enter Your Choice: 1
Enter Your Choice: 4
Enter Element to Push: 88
[TOP] 30 <- 20 <-
##### STACK OPERATIONS ##### ##### STACK OPERATIONS #####
1. PUSH- Insertion 1. PUSH- Insertion
2. POP- Deletion 2. POP- Deletion
3. PEEK- Show Top Element 3. PEEK- Show Top Element
4. DISPLAY - Show Stack 4. DISPLAY - Show Stack
0. EXIT 0. EXIT
Enter Your Choice: 4 Enter Your Choice: 0
[TOP] 88 <- 30 <- 20 <- Good Luck..........
Page 5 of 9
Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II


2) Queue:
Queues are data structures that follow the First In First Out (FIFO) i.e. the
first element that is added to the queue is the first one to be removed.

Real life examples


 Waiting in line
 Waiting on hold for tech
support
 Applications related to
Computer Science
 Round robin scheduling
 Key board buffer

QUEUE OPERATIONS:
 Peek : getting first value of QUEUE i.e. of FRONT position.
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)

Page 6 of 9
Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II


Example: Implementation of Queue in Python

Page 7 of 9
Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II

Page 8 of 9
Unit I: Computational Thinking and Programming - 2

Chapter-10 Data Structure - II


OUTPUT:
##### QUEUE OPERATION ##### ##### QUEUE OPERATION #####
1. ENQUEUE 1. ENQUEUE
2. DEQUEUE 2. DEQUEUE
3. PEEK 3. PEEK
4. DISPLAY 4. DISPLAY
0. EXIT 0. EXIT
Enter Your Choice: 1 Enter Your Choice: 3
Enter element to insert20 Item at Front: 20

##### QUEUE OPERATION ##### ##### QUEUE OPERATION #####


1. ENQUEUE 1. ENQUEUE
2. DEQUEUE 2. DEQUEUE
3. PEEK 3. PEEK
4. DISPLAY 4. DISPLAY
0. EXIT 0. EXIT
Enter Your Choice: 1 Enter Your Choice: 2
Enter element to insert25
Deleted Element was : 20
##### QUEUE OPERATION #####
##### QUEUE OPERATION #####
1. ENQUEUE
1. ENQUEUE
2. DEQUEUE
2. DEQUEUE
3. PEEK
3. PEEK
4. DISPLAY
4. DISPLAY
0. EXIT
0. EXIT
Enter Your Choice: 1
Enter Your Choice: 4
Enter element to insert90
[FRONT] 25 <- 90 <-
##### QUEUE OPERATION #####
##### QUEUE OPERATION #####
1. ENQUEUE
1. ENQUEUE
2. DEQUEUE
2. DEQUEUE
3. PEEK
3. PEEK
4. DISPLAY
4. DISPLAY
0. EXIT
0. EXIT
Enter Your Choice: 4
Enter Your Choice: 0
[FRONT] 20 <- 25 <- 90 <-
Good Luck......
Enter Your Choice:
Page 9 of 9

You might also like