DATA STRUCTURES
Data structure
It is a named group of data of different data types which is
stored in a specific way and can be processed as a single unit.
STACK:
Stack follows the property Last In First Out.
It is a linear structure in which insertion and deletion take
place at one end. i.e., top . stack is called LIFO data structure.
Two major operations of stack
1) Push – when an element is added on top of the stack.
2) Pop-when an element is removed from the top of the
stack.
To perform the above operations, we need to declare
variable that points to the top of the stack i.e., “TOP”. It acts
as a storage location or a container to store elements of the
stack.
Applications of a stack
Web browsers store the address of recently visited sites
in a stack. Each time they visit a new site pushed on
the stack.
The undo mechanism in an editor. The changes are kept
in stack. When the users press “undo” the changes is
popped.
Implementation of stack using List
1. Creating a stack.
2. Push operation/ adding elements to the stack.
3. Check for empty stack.
4. Pop operation/deleting elements from a stack
5. Traversal/ displaying a stack.
Creating a stack
Stack=list() #an empty stack/list
Stack=[] # an empty stack/list
Adding elements into a stack
Adding a new element in a stack is called push
operation.
In list append() is used to add elements into the stack.
Syntax:
List.append(x)
x is the element to be inserted into the stack.
ALGORITHM FOR PUSH OPERATION
1. Start
2. Initialize top with -1
3. Element= input(“enter the value to be added”)
4. Stack.append(element)
5. End
Checking an empty stack
While deleting or displaying elements from stack we
should check whether it is empty or not.
If it is not empty, then we can perform pop, operation
we can display stack.
ALGORITHM TO CHECK AN EMPTY STACK
1. Start
2. st_len=len(stack)
3. If st_len==[]:
Print(“stack is empty”)
go to step5
4. Element= stack.pop()
5. End
Deleting an element from a stack
The list function pop() is used to pop/remove/ delete
elements from a stack
Syntax:
list.pop()
or
list.pop([i])
The index position i is optional with the function pop().
The function pop() removes the element from the stack.
It also returns the popped/ deleted from the stack.
No index/ subscript value is required to be passed to the
function pop().
Traversing stack elements
Displaying the elements from the top position and
processed in the reverse order.
1) Write a python program to implement all basic
operations of a stack( push, pop, traversal)
Ans
#Stack implementation
s=[]
c="y"
while(c=="y"):
print("1.push")
print("2.pop")
print("3.display")
ch=int(input("enter your choice"))
if(ch==1):
a=int(input("enter number"))
s.append(a)
elif ch==2:
if(s==[]):
print("stack empty")
else:
print("deleted element is", s.pop())
elif ch==3:
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
else:
print("wrong input")
c=input("do you want to continue or not?")
2. Write a program to display unique vowels present in the
given word using stack.
vowels=['a','e','i','o','u']
word=input("enter the word to search for vowels")
stack=[]
for letter in word:
if letter in vowels:
if letter not in stack:
stack.append(letter)
print(stack)
print("the number of different vowels present in ",
word,"is",len(stack))
OUTPUT
Enter the word to search for vowels: HelloPython
[‘e’,’o’]
The number of different vowels present in HelloPython is 2
3) Give necessary declaration of a list implementation
stack containing numeric type data. Also write user-
defined function to pop a number from the stack.
Stack[]
def pop(stack, top):
if not stack:
print(“stack is empty”)
else:
num=stack.pop()
top=top-1
print(“value deleted from stack is”,num)
return top
4) Write a function in python , Makepush(package) and
Makepop(package), to add new package from list of
package description, considering them to act as push
and pop operations of the stack data structure.
def Makepush(package):
a=int(input(“enter package “)
Package.append(a)
def Makepop(package):
if package==[]:
Print(“stack empty”)
else:
print(“deleted element”, package.pop ())
push(books) and pop(books) methods in python to add
books and remove books considering them to act as
push and pop operations of stack.
def push(books):
name=int(input(“enter books”))
books.append(name)
or
def push(books):
stack=[]
stack.append(books)
print(“element ,”books “inserted successfully”)
def pop(books):
if stack==[]:
print(“stack is empty”)
else:
print(“deleted element is”, stack.pop())
2. write push(names) and pop(names) methods in python to
add names and remove names considering them to act as
push and pop operations of stack.
def push(names):
stack.append(names)
print(“element inserted successfully”)
0r
def push(names):
x=int(input(“enter the element”))
names.append(x)
def pop(names):
if stack==[]:
print(“stack is empty”)
else:
print(“ deleted element is “,stack.pop())
write pushon(book) and popon(book)
def pushon(book):
name=int(input(“enter book title”)
book.append(name)
def popon(book):
if book==[]:
print(“stack is empty”)
else:
print(“deleted element is”,book.pop())