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

stacks ppt

Uploaded by

LOLBOI
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)
109 views

stacks ppt

Uploaded by

LOLBOI
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/ 9

Delhi Public School

Sec- 45 ,Gurgaon

A stack is a data structure in which all the insertion


and deletion of data / values are done at one
end only. POP
PUSH
It follows LIFO(Last In First
Out) property.
Insertion / Deletion in stack
can only be done from top.
Insertion in stack is also
known as a PUSH operation.
Deletion from stack is also
known as POP operation in
stack.
Delhi Public School Using List as Stack in Python:
Sec- 45 ,Gurgaon

The concept of Stack implementation is easy in


Python , because it support inbuilt functions
(append() and pop()) for stack implementation.
These functions make the code short and simple
for stack implementation.
To add an item to the top of the list, i.e., to push
an item, we use append() function and to pop
out an element we use pop() function.
These functions work quiet efficiently and fast in
end operations.
Delhi Public School
Sec- 45 ,Gurgaon

program:

stack = [15, 19, 13]


OUTPUT
stack.append(17)
stack.append(21)
[15, 19, 13, 17, 21]
print(stack)
21
print(stack.pop())
[15, 19, 13, 17]
print(stack)
17
print(stack.pop())
[15, 19, 13]
print(stack)
Applications of stack
• To reverse a String: When we
push every letter of a string till
the last character and pop them
back we get the string in reverse

• For polish notation: Stack


K
plays a vital role in solving C
complex arithmetic expressions
in any high-level language. For A
this stack is used to convert an T
expression in infix notation to
postfix and vice versa. S
Delhi Public School
Sec- 45 ,Gurgaon

Queue is a data structures that is based on First In First Out


(FIFO) stretagy ,i.e. the first element that is added to the
queue is the first one to be removed.

Queue follows the FIFO (First - In - First Out)


structure.
• According to its FIFO structure, element inserted
first will also be removed first.
• In a queue, one end is always used to insert data
enqueue) and the other is used to delete data
dequeue), because queue is open at both its ends.
01 2 3 4 5 6 7

Delhi Public School


. . . . . . .
Sec- 45 ,Gurgaon .

Front= NULL Rear=NULL


. . . . . . .
6

Rear
Front=0 Rear=0

. . . . . .
6 4

Front
Front =0 Rear=1
. . . . .
6 4 1
9

Front=0 Rear=2
Applications of Queues
• Suppose there is a web-server hosting a web-site to
declare result(s). This server can handle a maximum of 50
concurrent requests to view result(s). So, to serve
thousands of user requests, a Queue would be the most
appropriate data structure to use.
• Some Operating Systems (OS) are required to handle
multiple tasks called - jobs, seeking to use the processor.
Therefore, in a multitasking operating system, jobs are lined
up (queued) and then given access to the processor
according to some order. The simplest way is to give access
to the processor on a FIFO basis.
• When we send print commands from multiple files from the
same computer or from different computers using a shared
printer. The OS puts these print requests in a queue and
sends them to the printer one by one on a FIFO basis.
Write a program to add, remove and display the book details using
list implementation as a Queue

Delhi Public School


Sec- 45 ,Gurgaon
Library=[]
c='y'
while (c=='y'):
print ("1. INSERT")
print ("2. DELETE ")
print ("3. Display")
choice=int(input("Enter your choice: "))
if (choice==1):
book_id=input("Enter book_id : ")
bname = input("Enter the book name :")
lib = (book_id,bname) #tuple created for a new book
Library.append(lib) #new book added to the list/queue
elif (choice==2):
if (Library==[]):
print("Queue Empty")
else:
print("Deleted element is:",Library.pop(0))
elif (choice==3):
l=len(Library)
for i in range(0,l):
print (Library[i])
else:
print("wrong input")
c=input("Do you want to continue or not : ")
Write a program for implementation of List as stack

Delhi Public School


Sec- 45 ,Gurgaon
s=[]
c="y"
while (c=="y"):
print ("1. PUSH")
print ("2. POP ")
print ("3. Display")
choice=int(input("Enter your choice: "))
if (choice==1):
a=input("Enter any number :")
s.append(a)
elif (choice==2):
if (s==[]):
print ("Stack Empty")
else:
print ("Deleted element is : ",s.pop())
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1): #To display elements from last
element to first
print (s[i])
print(s.reverse())
else:
print("Wrong Input")
c=input("Do you want to continue or not? ")

You might also like