0% found this document useful (0 votes)
48 views

Data Structure - Stack and Queue

Uploaded by

FF gaming
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)
48 views

Data Structure - Stack and Queue

Uploaded by

FF gaming
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/ 9
Unit I: Computational Thinking and Programming -2 Visit to webs snpythonacbse com (Oo Ty epee RO PL Le eat Ceia ltd eee OE Stack and Queue Brent 1 Stack 2 Example: Implementing Stack In Python 3 Queue 4 Example: Implementation of Queue Page 1of9 Visit to webs jpythondcbse.com O Data Structure - II Linear data structures are collections of components arranged ina straight line > Astack is a data structure that keeps objects in Last-In-First-Out (LIFO) order ees oe > Objects are added to the top of the stack > Only the top of the stack can be accessed Push anew Pop abok Bottom of book on top mop Asstack of lack > Apile of books or a stack of (inaccessible) four books dinner plates can be thought of examples of stacks. It has three primitive operations: © Push: Add an element to the stack © Pop: Remove an element from the stack © Peek: Get the topmost element of the stack In Python, a stack is implemented using a list object. © To push an item in the stack, use the list function append list.append{(item) To pop an item in the stack, use the list function pop list.pop() © To get the top most item in the stack, write list{-1] Page 20f9 Unit I: Computational Thinking and Programming -2 Chapter-10 Data Structure - II ‘Visit to website: learnpythondcbse.com File Et Formet Run Options Window Help # Created By: learnpythond4cbse.com # Stack using List # Function to check Stack is empty or not def isEmpty (Lst): if len(Lst)==0: return 1 else: return 0 # Function to add (PUSH) elements in Stack def Push_Stack(Lst, val): Lst append (val) top=len (Lst) -1 # Function to Delete (POP) elements in Stack def Pop Stack(Lst): if isempty (Lst) : return "UnderFlow" else: ele=Lst.pop() if len(Lst top=None else: top=len (Lst)-1 return ele # Function to Display Top element of Stack def Peek Stack(Lst): if isEmpty (Lst): return "UnderFlow" else: top=len(Lst)-1 return Lst [top] Ln37 Cok 23 Page 30f9 Unit I: Computational Thinking and Programming -2 [a etry Doin fie ae format fin Oman window Hep 4 Function to Display elenents of Stack Joef Display_stack(ist) Sf istmpey (st) Print ("NO Tem to Display.....") else: fen (Lst) -1 PEint (" (T0P]", en wails tp=0: print (ist [tp], "<-',end=" ") tp - print () » # Driver function Jost main(): List Top wile Truet print) print ("##### STACK OPERATIONS #####") print ("1. PUSH- Insertion") print ("2. POP- Deletion") print ("3. PEEK- Show Top Element”) print ("4. DISPLAY - Show Stack") print ("0. EXIT") choice=int (input ("Enter Your Choic: if choice==1: Element=int (input ("Enter El Push_Stack (List, Bement) elif choice==2: Element=Pop_stack (List) if Element=="UnderFlow": print ("stack is Empty") else: print ("Deleted Element was elif choice==3: Element=Peek stack (List) if Blement=="UnderFlow" print ("stack is Empty") else: print ("top Element : ",Element) elif choice: Display Stack (List) elif choice= print ("Good Luck. break ” main () Page 40f9 Chapter-10 Data Structure - II " Element) ‘Visit to website: learnpythondcbse.com Unit I: Computational Thinking and Programming -2 ‘Visit to website: learnpythondcbse.com Chapter-10 Data Structure - II OUTPUT sittttt STACK OPERATIONS ##t#it 1. PUSH- Insertion 2. POP- Deletion 3. PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 1 Enter Element to Push: 20 ‘uitttih STACK OPERATIONS #ititis 1. PUSH- Insertion 2. POP- Deletion 3. PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 1 Enter Element to Push: 30 itt STACK OPERATIONS ##s##t 1. PUSH- Insertion 2. POP- Deletion 3. PEEK- Show Top Element 4. DISPLAY - Show Stack 0. EXIT Enter Your Choice: 1 Enter Element to Push: 88 ‘Hitt STACK OPERATIONS #ttittt 1. PUSH- Insertion 2. POP- Deletion 3, PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 4 [TOP] 88 <- 30 <- 20 <- ‘atttttt STACK OPERATIONS ###it 1. PUSH- Insertion 2. POP- Deletion 3. PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 3 Top Element: 88 ‘uittih STACK OPERATIONS #iitit 1. PUSH- Insertion 2. POP- Deletion 3. PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 2 Deleted Element was: 88 ‘uittih STACK OPERATIONS #iitit 1. PUSH- Insertion 2. POP- Deletion 3. PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 4 [TOP] 30 <- 20 <- suits STACK OPERATIONS ##8##t 1. PUSH- Insertion 2. POP- Deletion 3. PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 0 Good Luck, Page 50f 8 Visit to webs jpythondcbse.com O Data Structure - II Queues are data structures that follow the First In First Out (FIFO) i.e. the first element that is added to the queue is the first one to be removed. > Waiting in line Real life examples aT of queue > Waiting on hold for tech wn ) ve hhh > Applications related to New element is added to the rear Computer Science of the queue > Round robin scheduling > Key board buffer QUEUE OPERATIONS: > Peek : getting first value of QUEUE i.e. of FRONT position. Queue[Front] _ # Front is an int storing index of first element of queue > Enqueue: addition of new item in QUEUE at REAR position. e.g. Queue. append(Item) > Dequeue: removal of item from the beginning of QUEUE. e.g. Queue.pop(0) Page 6 of 9 Unit I: Computational Thinking and Programming -2 Visit to website: learnpythondcbse.com Chapter-10 Data Structure - II File Edit Format Run Options Window Help \||# Created By: learnpython4cbse.com il || # Queue using List # Function to check Queue is empty or not def isEmpty (qLst): if len(qLst return 1 else: return 0 # Function to add elements in Queue def Enqueue (qLst, val): qist . append (val) if len(qLst) = front=rea: else: rear=len (qLst)-1 # Function to Delete elements in Queue def Dqueue (qLst) : if isEmpty (qhst) : return "UnderFlow™ else: | val = qbst.pop(0) y if len(qbst) = \ front=rear=None return val # Function to Display top element of Queue def Peek(qLst) : if isempty (qLst) : return "UnderFlow" else: front=0 return qLst [front] Und Cobo Page 70f9 ‘Visit to website: learnpythondcbse.com Unit I: Computational Thinking and Programming -2 Chapter-10 Data Structure - II (se Que y Dina mun Fie Eat Fomat fn Opbens Window Hep ¥ Function to Display elements of queue def Display (qLst): if isBmpty(qbst) + print ("No Ttem to Dispay in Queue. els: tp = len(qust)-1 print (* [FRONT: front = 0 i= front rear = len(qust)-1 while (i

You might also like