0% found this document useful (0 votes)
8 views5 pages

Together With

Class 12 stack chapter

Uploaded by

Arush Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Together With

Class 12 stack chapter

Uploaded by

Arush Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
5 DATA STRUCTURE-STACK LEARNING OUTCOMES ‘After this chapter students will be able to: + describe the data structure along with its operations. + understand the stack. DATA STRUCTURE USING LIST, STACKS AND QUEUES 1. Data Structure. It is a way to store a collection of arbitrary data. The data structure can be implemented via the list, dictionary, tuple, set etc. 2. Classification of Data Structure. Data ¥ y ake Static or Fixed Dynamic Structure Structure 12 Together ucek® Computer Science (Python) —12 n reverse order of their arrival. The stack 4 Structure. Some of eT called Last In First Out (LIFO) arrangemen, 3. Operations on Da the operations that are performed on data structure are as FollOWS: ce a aa ee nts in ush, Pe «Insertion (Addition of new eleme [Lisserion_} mmm Deletion the list.) «Deletion (Removal of an element from alist.) « Traversal (Visiting elements of a list.) « Searching (Finding an element in the list.) 4, Stack. Stack means arranging one object over another and these objects are removed 5. Operations on Stack. Push : Insertion of a new element (from the beginning) in stack. [Push(4) Top 2] [Push2)] [5 J [Pushts)) Top ESaS Top Top Pop : Deletion of an element(from the beginning) in stack. Pop 4 3 z 1 6. Conditions associated with Stack/Queue: * Overflow condition: It is a situation where we are not able to ai (o memory ition: i able to add new element di lue full or range is overfilled with values. * Underflow condition: It is a situation where there is no we wish to it is i is no element left and we still stil remove an element or display the list. ications of Stack. Stack a word or rev e and it can also used Stack. can be used to reverse /erse a lin al ction call routines, SOLVED QUESTION BANK Very Short Answer Type Questions [1 Mark] 1. Define data structure. Ans. It is a Way to store a collection of related data together. The data structure can be implemented via the list, dictionary, tuple, set etc 2, What is LIFO? Ans. Last In First Out. 3. What is traversal? Ans. Accessing each element is known as traversal 4. Name some applications of Stack. Ans. Application of Stack are: (a) Reverse of a String or Line (b) Function call routines. (0) Backtracking 5. Alist contains LST=(10,7,9,15,12].8 is added to the list. What will be the output? If (a) LST as Stack —_(b) LST as Queue Ans. (a) LST as Stack LST = [8,10,7,9,15,12] (6) LST as Queue LST = (10,7,9.15.12.8] Short Answer Type Questions [2/3 Marks] 6. Python program to check whether the list is empty or not. FileName: ch-datas\Q15] Ams. A = [] Af not A: print(“List 15 eroty”) Long Answer Type Questions [4 Marks] 7. Write # program to implement the stack using list. Ans-() wy wile (c+s"y") §. Write Push(Book) and Pop(Book) methods in Python to Add a new Book and Remove a Book from a List of Books, considering them to act as PUSH and POP operations of the data structure Stack. Ams. def push(Book Enter any number print (“Stack Enoty* else: print (“Deleted elenent 15:" Book. pop 9. Write a menu driven python program ‘using function Push(), Pop() and Display to implement the stack. The program will store the name of the books. 14 Jogether week® Computer Science (Python)—12 Ans, def push(Book) : nane=input ("Enter the book nane") Book. append(nane) def pop(Book): if Book= print("Under Flow") else: print("Book Deleted" ,Book.pop()) def display (Book): print("Books in Stack") 1=1en (Book) for i in range(1~ print (Book[i], -1,-1): vend="*) # rain Book=[] while True: print("\n1. Push\n2. Pop \n3. Display\ nu, Exit") cheint(input ("Enter your choice") if ch==1: push (Book) elifch==2: pop (Book) elifch==3: display (Book) else: break 10. Write a menu driven python program using function Push (), Pop () and Display() to implement the stack. The program will store the Employee details i.e. Employee number, Employee name and Salary. Ans. def push(Emp): : enosint (input ("Enter the Empolyee Number") ) 'e=input("Enter the Employee Name") input ("Enter the Salary") pend([eno, enane, sal]) (Emp) print ("Under Flow") else: print("Employee Recorg Deleted" Emp. pop()) def display (Emp) : if Emp==[]: print ("Underflow!!!") else: print ("Employee record in Stack\n*) A-len(Emp) for i in range(1-1,-1,-1): print(Emp[i}) # main Emp=[] while True: print("\nl. Pushy n2. Pop \n3. Display\ nd. Exit") cheint (input ("Enter your choice")) if ch push(Emp) elif che=2: pop (Emp) elif ch==3: display (Emp) else: break Competency/Case-based Question 11. Read the following code carefully which is implementing the concept of STACK: defisEmpty(STACK): if len(STACK) return True else: return False def pop(STACK): if isEmpty(STACK): return "Underflow" else: element = STACK.pop() if, len(STACK) ‘top = None else: top = len(STACK) - 2 return element (a) How are the above two functions inter- related? Ans. The function isEmpty(STACK) will be called when pop(STACK) is called (6) What is the difference in between STACK.pop() and pop(STACK) Ans. STACK. pop() is pre defined and pop(STACK) is user defined (©) What is the retum type of the function defisEmpty (STACK) PRACTICE QUESTIONS Very Short Answer Type Questions [1 Mark] 1. Write the full form FIFO. 2. A list contains LST=[10,7,9,15,12]. An element is removed. What will be the output? If (a) LST as Stack (b) LST as Queue 3. Which method accepts two arguments (index element)? If the index is more than the size, then it will insert at the end. Data Structure-Stack 15 Ans. Boolean (@) What will the pop(STACK) function return if the stack already consist of [‘A’,’B’] at the position 0 and 1 respectively Ans. B (0) if len(STACK ; What is the purpose of this line in the above code: Ans. It checks if the stack is already empty. 4. Which is a situation when there is no element left and still wish to remove element? Long Answer Type Questions [4 Marks] 5. Write algorithm for PUSH(insert) operation in stack. 6. Write algorithm for POP(Remove) operation in stack. ANSWERS TO PRACTICE QUESTIONS 1. First in first out. 2. (a) LST as Stack LST = (7,9,15,12] (6) LST as Queue LST = [10,7,9,15,12] 3. Insert() 4. Underflow 5, Algorithm for PUSH in stack 1. Start 2.Element = Input("Enter the value to be added as element in Stack.") 3.Stak.append(Element) # Stak is the name of Stack. append() is used to insert the # element in the end of stack. 4. TopeToprd 5. End. 6. Algorithm for POP in stack. Start StakLen = len(Stak) # len() is used to calculate the length of the Stack. if StakLen <0 # To check whether stack is enpty or not Print("Pop is not possible as stack is empty”) else: val = Stak.pop() # pop() is used to remove the last inserted element in Stack. top = top-1 return top End

You might also like