0% found this document useful (0 votes)
39 views9 pages

12th Data Structure

The document discusses data structures and stacks. It defines a stack, provides examples of stack applications, and describes the push and pop operations. It also includes Python code to demonstrate creating and manipulating a stack.

Uploaded by

Kartik Rawal
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)
39 views9 pages

12th Data Structure

The document discusses data structures and stacks. It defines a stack, provides examples of stack applications, and describes the push and pop operations. It also includes Python code to demonstrate creating and manipulating a stack.

Uploaded by

Kartik Rawal
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

Data Structure

 A data structure defines a mechanism to store,


organize and access data along with
operations/processing easily.
 data structure example are: string, list, tuple etc.
 Other are stack and queue
Stack
 Arrangement of elements in a linear(sequence) order is
called a stack.
 Example: piles of books in the library or stack of
plates at home
 Elements are added and deleted from the stack at one
end only. The end from which elements are added or
deleted is called TOP of the stack.
 It follows the Last-In-First-out (LIFO) principle. That
is, the element which was inserted last (the most recent
element) will be the first one to be taken out from the
stack.
APPLICATIONS OF STACK

 Pile of clothes in an almiraha


 Multiple chairs in a vertical pile
 Bangles worn on wrist
 reverse a string
 redo/undo in text editor
 browsing the web and go back to the last visited web page
 matching of parentheses in evaluating expression
Operations on Stack

 PUSH and POP.


 PUSH(insertion): PUSH adds a new element at the TOP of the
stack. We can add elements to a stack until it is full. Trying to add
an element to a full stack results in an exception called ‘overflow’.
This condition will not arise in list because list has unlimited size.

 POP(delete operation ): POP operation is used to remove the top


most element of the stack. We can delete elements from a stack
until it is empty. Trying to delete an element from an empty stack
results in an exception called ‘underflow’.
write a program to create a
STACK in which we will:
 insert/delete elements
 check if the STACK is empty (no element in the stack)
 find the number of elements in the STACK
 read the value of the topmost element in the STACK
 #create empty list
 glassStack = list()
 #check whether the stack glassStack is empty or not
def isEmpty(glassStack):
if len(glassStack)==0:
return True
else:
return False
#opPush to insert (PUSH) a new element in stack.
def opPush(glassStack,element):
glassStack.append(element)
#find the number of elements in the glassStack.
def size(glassStack):
return len(glassStack)
 #Read the most recent element (TOP) in the glassStack.
def top(glassStack):
if isEmpty(glassStack):
print('Stack is empty')
return None
else:
x=len(glassStack)
element=glassStack[x-1]
return element
#Delete the topmost element from the stack.
def opPop(glassStack):
if isEmpty(glassStack):
print("underflow")
return None
else:
return(glassStack.pop())
 #Show the contents of the stack.
def display(glassStack):
x=len(glassStack)
print("Current elements in the stack are: ")
for i in range(x-1,-1,-1):
print(glassStack[i])
glassStack = list() # create empty stack
isEmpty(glassStack)
element='glass1' #add elements to stack
print("Pushing element ",element)
opPush(glassStack,element)
element='glass2'
print("Pushing element ",element)
opPush(glassStack,element)
#display number of elements in stack
print("Current number of elements in stack is",size(glassStack))
element=glassStack.pop() #delete an element from the stack
print("Popped element is",element)
element='glass3' #add new element to stack
print("Pushing element ",element)
opPush(glassStack,element)
#display the last element added to the stack
print("top element is",top(glassStack))
display(glassStack) #display all elements in the stack
while(not glassStack==[]): #delete all elements from stack
item=opPop(glassStack)
print("Popped element is ",item)
print("Stack is empty now")
Output:
Pushing element glass1
Pushing element glass2
Current number of elements in stack is 2
Popped element is glass2
Pushing element glass3
top element is glass3
Current elements in the stack are:
glass3
glass1
Popped element is glass3
Popped element is glass1
Stack is empty now

You might also like