Write a Python program to implement a stack using a list data-structure | Computer Science Term II Practical Program for class 12 python

Write a Python program to implement a stack using a list data-structure | Computer Science Term II Practical Program for class 12 python

A stack is a linear data structure that follows the principle of Last In First Out (LIFO). This means the last element inserted inside the stack is removed first.

Write a Python program to implement a stack using a list data-structure | Computer Science Term II Practical Program for class 12 python

Basic Operations of Stack

There are some basic operations that allow us to perform different actions on a stack.
  1. Push: Add an element to the top of a stack
  2. Pop: Remove an element from the top of a stack
  3. IsEmpty: Check if the stack is empty
  4. IsFull: Check if the stack is full
  5. Peek: Get the value of the top element without removing it

Working of Stack Data Structure

  • A pointer called TOP is used to keep track of the top element in the stack.
  • When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1.
  • On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP.
  • On popping an element, we return the element pointed to by TOP and reduce its value.
  • Before pushing, we check if the stack is already full
  • Before popping, we check if the stack is already empty

Source Code

 # Stack implementation in python

# Creating a stack
def create_new_stack():
    stack = []
    return stack


# Creating an empty stack
def empty(stack):
    return len(stack) == 0


# Adding items into the stack
def push(stack, item):
    stack.append(item)
    print("pushed item: " + item)


# Removing an element from the stack
def pop(stack):
    if (empty(stack)):
        return "stack is empty"

    return stack.pop()


stack = create_new_stack()
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
push(stack, str(4))
print("popped item: " + pop(stack))
print("stack after popping an element: " + str(stack))
  

Output

Write a Python program to implement a stack using a list data-structure | Computer Science Term II Practical Program for class 12 python

Search Related Tags 

write a python program to implement a stack using list
stack implementation in python using class
stack program in python
list program in python class 12
stack in python w3schools
data structures in python class 12 notes
write a menu based program to perform the operation on stack in python
write a code to create an empty stack named st
computer science project for class 12 python pdf
class 12 computer science python project slideshare
class 12 computer science project on hospital management python
class 12 computer science project python and sql
class 12 computer science project topics
class 12 cs project
cs project class 12 python synopsis
class 12 computer science project synopsis

Post a Comment

If you any doubts, Please let me know

Previous Post Next Post