Class 12 Computer Science Data Structure in Python Handout with important questions

Share with others

Class 12 Computer Science Data Structure in Python Handout

Table of Contents

  1. Data structure in Python
  2. What is Stack
    1. Operations on Stack
    2. Working with stack using list.
    3. Practice Questions – Part 1
    4. Practical Implementation of stack using list
  3. What is Queue?
    1. Operations on Queue
    2. Queues in daily life
    3. Working with queue using list
    4. Practice Questions – Part 2
    5. Practical Implementation of queue using list

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

In this handout we will learn that how List can be implemented as STACK & QUEUES

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 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. Examples of stack are pile of books, pile of plates or stack of carom coins.

Class 12 Computer science Data structure in python handout
Class 12 Computer Science Data Structure in Python Handout

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:

Addition of element on the Top of the Stack is called PUSH.

1. push (4)
2. push (7)
3. push ("a")
4. push ("Suman")

Above operations will form the stack as shown below :
Class 12 Computer Science Data Structure in Python Handout
Class 12 Computer Science Data Structure in Python Handout

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.

Class 12 Computer Science Data Structure in Python Handout
Class 12 Computer Science Data Structure in Python Handout

Removal of elements from the top of the Stack is called POP.

Data Structure in Python

In the form of List the above stack will be represented as shown below

Class 12 Computer Science Data Structure in Python Handout
Class 12 Computer Science Data Structure in Python Handout

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 :

  1. How to create an empty stack?
  2. How to add elements to a stack?
  3. How to delete / remove elements from the stack
  4. How to traverse or displaying elements of stack?
  5. 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 order
      print(st[i])
if (st == [ ]) :
     print("stack is empty")

Data Structure in Python – Question Answer Session – Part 1

Q1. Expand the term LIFO.
Q2. What do you mean by Stack?
Q3. What is the difference between pop( ) and append( ) function of list?
Q4. What do you mean by data structure? Q5. Write a code to create an empty stack named "st".
Q6. Write the technical term used for adding element in stack.
Q7. What happen when you try to delete an element from an empty stack?


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 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)")

For more questions on Data Structure in Python Click Here

Queue :

A queue is a specialized data structure in which elements are added at one end called Rear and removed from other end called Front.

Operations on Queue :

A queue performs only two operations :

  1. Insert : In this a new element is added at the end of list called Rear.
  2. Delete : In this an element is to be deleted from one end called Front.

NOTE : Insertion and Deletion of elements takes place at different end. Addition at Rear and Deletion at Front

Queues in Daily Life :

We can see the formation of queues in our daily life like :

  1. Outside the ATM
  2. In Bank
  3. Buying Ticket in Movie Hall

Working with Queue using List:

The implementation of queue using list is a simple process as there are inbuilt function which we used during working with queue. Basic operations that we should know are :

NOTE : Enqueue term means adding element in queue while Dequeue term means deleting element from queue

  1. How to create an empty queue?
  2. How to add elements to a queue?
  3. How to delete / remove elements from the queue
  4. How to traverse or displaying elements of queue?
  5. How to check for empty queue?
  1. Creating an empty queue : We can create an empty queue as similar to stack like :

Q = list( ) or Q = [ ]

2. Adding an element to queue : Elements can be added by using the function append( ) of list. This function automatically add the new element at the end of the queue. like –

Q.append(4) : This code will add element 4 at the end of the queue named “Q”

3. Deleting an element from Queue : pop( ) function is used to delete an element from the queue. Before deleting an element, it is to be checked that whether queue is empty or not. Code to delete an element is :

     if (Q == [ ]:
          print("Queue is Empty")
     else :
          print("Deleted element is :", Q.pop(0)) # index value "0" indicate the very first value of the queue

4. Displaying elements of the queue : Elements of the queue can be displayed as given below :

                          L = len(Q)
                          for i in range(0, L):
                               print(Q[i])

5. Checking an empty Queue : We use the following code to check whether the queue named “Q” is empty or not.

     if(Q==[ ]):
         print("Queue is Empty")

Data Structure in Python – Question Answer Session – Part 2

Q1. What is the difference between Stack and Queue data structure in python?
Q2. In queue, addition and deletion of elements take place at different ends(T/F)
Q3. Which method of list is used to add element at the end ?
Q4. Write the code to display all elements of queue named "Aqueue".
Q5. Write a function addQ(num) to add numbers in a queue using list. (num is a list of numbers) 
Q6. Write full form of FIFO.
Q7. Queue works on _______________(FIFO/LIFO) concept.
Q8. Write 2 applications of stack.
Q9. Which method of list is used to remove last element of the list?

Practical Implementation of queue using list

Q1. Write a function Aqueue(student) and Dqueue(student) to add a new student name and remove a student name from a list student, considering them to act as insert and delete operations of the queue Data Structure in Python.

student=[ ]
def Aqueue(student):
    sn=input("Enter name of student")
    student.append(sn)

def Dqueue(student):
    if(student==[]):
        print("Queue is empty")
    else:
         print("Deleted student name :",student.pop(0))

Q2. Write a function Add(book) and Del(book) to add a new book name and remove a book name from a list book, considering them to act as insert and delete operations of the queue Data Structure in Python.

book=[ ]
def Add(book):
    bn=input("Enter name of book")
    book.append(bn)

def Del(book):
    if(book==[ ]):
        print("Queue is empty")
    else:
         print("Deleted book name :",book.pop(0))

Q3. Write a menu driven program using function Qadd( ), Qdel( ) and disp( ) to add, delete and display the record of book using queue as data structure in python. Record of book store the following details : Book name, Book Number and Book Price

book=[ ]
ch='y'
def Qadd(book):
    bn=input("Enter book name")
    bnum=int(input("Enter book number"))
    bp=int(input("Enter book price"))
    temp=[bn,bnum,bp]
    book.append(temp)

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

def disp(book):
    l=len(book)
    print("Book Name\tBook Number\tBook Price")
    for i in range(0,l):
        print(book[i][0],"\t\t",book[i][1],"\t\t",book[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):
        Qadd(book)
    elif(op==2):
        Qdel(book)
    elif(op==3):
        disp(book)
    elif(op==4):
        break
    ch=input("Do you want to enter more(Y/N)")

For more questions on Data Structure in Python Click Here

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series


Share with others

Leave a Reply

%d bloggers like this: