Wednesday, July 20, 2022

XII CS : Data Structure : STACK

 Data Structure in Python
STACK


Data Structure

A data structure in python can be defined as a structure which can holds related data. In other words we can say that data structure is a way of storing, organizing and fetching data in computer. 

There are four data structure in python :
1. List
2. Tuple
3. Dictionary
4. Set


STACK :

A stack is a linear data structure in python in which addition and deletion of elements can be done at one end only. A stack can be static or dynamic. A stack is known as LIFO (Last – In, First – Out) data structure in python. 
LIFO means the elements which are added in the last would be the first one to remove. 
Note: Stack may also known as : FILO (First In Last Out)

Examples of stack used in real life are:-
pile of books, 
pile of plates or 
stack of carom coins.





In above pile of rings the ring which we placed first is at the bottom and the ring which we placed in last is at the Top, So we can say that Stack is linear list implemented as LIFO.


Operations on Stack:

There are two main operations on Stack: PUSH & POP

Addition of element on the Top of the Stack is called PUSH and Deletion of element from the Top of the Stack is called POP.

OverflowDuring push operation when we add an item to the stack. If the stack is full, then it is said to be an Overflow condition.

Underflow : During pop operation when we remove an item from the stack. If the stack is empty, then it is said to be an Underflow condition.


Example: 

1. push (4) 
2. push (7) 
3. push ("a") 
4. push ("Suman")  
Above operations will form the stack as shown below :



So you can observe that the element which we inserted first is coming at the bottom of the stack.

In the form of List the above stack can be shown as.



Working with Stack using List:

The implementation of stack using list is a simple process as there are inbuilt function which we used during working with stack. 

Basic operations that we should know are :

How to create an empty stack?
How to add elements to a stack?
How to delete / remove elements from the stack
How to traverse or displaying elements of stack?
How to check for empty stack?

1.Creating an Empty Stack
An empty stack can be created by using the following code

st = [ ] or st = list( ) #Here st is an empty stack

NOTE : Working with stack is similar to working with list (we can add element by append( ), we can remove element by pop( ) and we can display element by using index value)

2. Adding an element to a Stack
We can add element in a stack by using append( ) function as shown below: 
 st.append(5)

Here element ‘5’ is added into a stack named ‘st’

NOTE : We can add element only at the end of the list as we are implementing list as stack.

3. Deleting elements from the stack
We can delete elements from the stack as shown below :
st.pop( )

NOTE : We can remove or delete element only from the end of the list as we are implementing list as stack.

4. Displaying all elements of the stack
We can display all elements in stack as shown below :
L = len(st) for i in range(L-1, -1, -1) : #As we have to display elements in reverse  
      print(st[i])
if (st == [ ]) : 
  print("stack is empty")


Practical Implementation of Stack using List


Q1. Write a function push(student) and pop(student) to add a new student name and remove a student name from a list student, considering them to act as PUSH and POP operations of stack Data Structure in Python.
st=[ ]
def push(st):
    sn=input("Enter name of student")
    st.append(sn)
def pop(st):
    if(st==[]):
        print("Stack is empty")
    else:
        print("Deleted student name :",st.pop())

Q2. Write a function push(number) and pop(number) to add a number (Accepted from the user) and remove a number from a list of numbers, considering them act as PUSH and POP operations of Data Structure in Python.

st=[ ]
def push(st):
    sn=input("Enter any Number")
    st.append(sn)
def pop(st):
    if(st==[]):
        print("Stack is empty")
    else:
        print("Deleted Number is :",st.pop())
Q3. Write PushOn(Book) and Pop(Book) methods/functions in Python to add a new Book and delete a Book from a list of Book titles, considering them to act as push and pop operations of the Stack data structure.
def PushOn(Book):
     a=input(“enter book title :”)
     Book.append(a)
def Pop(Book):
     if (Book = =[ ]):
          print(“Stack empty”)
     else:
          print(“Deleted element :”)
Book.pop()

Q4. Write a menu based program to add, delete and display the record of hostel using list as stack data structure in python. Record of hostel contains the fields : Hostel number, Total Students and Total Rooms

host=[ ]
ch='y'
def push(host):
    hn=int(input("Enter hostel number"))
    ts=int(input("Enter Total students"))
    tr=int(input("Enter total rooms"))
    temp=[hn,ts,tr]
    host.append(temp)

def pop(host):
    if(host==[]):
        print("No Record")
    else:
        print("Deleted Record is :",host.pop())

def display(host):
    l=len(host)
    print("Hostel Number\tTotal Students\tTotal Rooms")
    for i in range(l-1,-1,-1):
        print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])

while(ch=='y' or ch=='Y'):
    print("1. Add Record\n")
    print("2. Delete Record\n")
    print("3. Display Record\n")
    print("4. Exit")
    op=int(input("Enter the Choice"))
    if(op==1):
        push(host)
    elif(op==2):
        pop(host)
    elif(op==3):
        display(host)
    elif(op==4):
        break
    ch=input("Do you want to enter more(Y/N)")

No comments:

Post a Comment