Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
16 views
Data Structure - Stack and Queue
Uploaded by
nikhilran907
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save 10. Data Structure_ Stack and Queue For Later
Download
Save
Save 10. Data Structure_ Stack and Queue For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
16 views
Data Structure - Stack and Queue
Uploaded by
nikhilran907
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save 10. Data Structure_ Stack and Queue For Later
Carousel Previous
Carousel Next
Save
Save 10. Data Structure_ Stack and Queue For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 9
Search
Fullscreen
Unit i: Computational Thinking ramming -2 Visit to website: learnpythondcbse.com Chapter-10 Data Structure - II Stack and Queue 1 Stack 2 Example: Implementing Stack In Python 3 Queue 4 Example: Implementation of Queue Page ofVisit to webs O Data Structure - II Linear data structures are collections of components arranged in a 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 Push a new Pop a book be accessed hod ‘book on top rnp Asta of ack > Apile of books or a stack of (inaccessible) four books dinner plates can be thought of examples of stacks. It has three primitive operations: e 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 20f9Uni ‘omputational Thinking and Programming -2 File Est Formet Run Options Window Help # Created By: learnpython4cbse.com B # Stack using List # Function to check Stack is empty or not def isEmpty (Lst) if len(Lst) 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 iszmpty (ist): return "UnderFlow" else: top=len (Lst)-1 return Lst[top] Ln37 Cok 23 Page 3.0f9 ‘Visit to website: learnpythondcbse.com Chapter-10 Data Structure - IIUnit I: Computational Thinking and! Visit to website: learnpythondcbse.com gramming -2 Chapter-10 Data Structure - II [Sk yD Fie Eat Format fun Opbons_vindow “Help # Function to Display elements of Stack F| cf Display_Stack(Lst) ip isBapty (ist) print ("No Item to Display.....") else: fen (Lst) -1 print (" (TOP) ", en wails tp=0: print (ist (tpl, "<-',end=" ") tp -=1 print Q) » # Driver function Jost main(): List Top while 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, Element) elif choice==2: Element=Pop_stack (List) + if Element=="UnderFlow": print ("stack is Empty") else: print ("Deleted Element was " Element) elif choice==3: Element=Peek_stack (bist) Lf Blement=="UnderFlow": print ("stack is Empty") else: print ("Top Element : ",Element) e1if choice: Display_stack (List) elif choice: print ("Good Luck. break main () | ” Page dofUnit I: Computational Thinking and Programming -2 ‘Visit to website: learnpythondcbse.com Chapter-10 Data Structure - II OUTPUT ‘itt STACK OPERATIONS ###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: 20 ‘uit STACK OPERATIONS sits 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 itis 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 <- ‘aititttt 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 ‘uit STACK OPERATIONS sits 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 ‘uit STACK OPERATIONS sits 1. PUSH- Insertion 2. POP- Deletion 3, PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 4 [TOP] 30 <- 20 <- itt STACK OPERATIONS ####t 1. PUSH- Insertion 2. POP- Deletion 3. PEEK- Show Top Element 4, DISPLAY - Show Stack 0. EXIT Enter Your Choice: 0 GoOd Lucksrvsree Page Sof 9Queues 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 v of queue > Waiting on hold for tech we ) support ) ) ) ) > 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 9Uni ‘omputational Thinking and Programming -2 Visit to website: learnpythondcbse.com Chapter-10 Data Structure - II File Edit Format Run Options Window Help \|I# Created By: learnpython4cbse.com I || # 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(qust) : return "UnderFlow" else: | val = qhst.pop(0) y if len (qust)= j 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 7 of 9Unit I: Computational Thinking and! Visit to website: learnpythondcbse.com Chapter-10 Data Structure - II (se Ques - Dvn Sm Fie Eat Fomat fon Opens Window Hep ¥ Function to Display elements of queue - dof Display (gist): | if isimpty(qhst) + print ("No Ttem to Dispay in Queue. els: tp = len(qust)-1 print (* [FRONT: front = 0 i = front rear = len(qhst)-1 while (i
You might also like
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6127)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (627)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brene Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (932)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8215)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2923)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4972)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Tóibín
3.5/5 (2061)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1987)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1993)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2641)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2543)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Principles: Life and Work
From Everand
Principles: Life and Work
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Steve Jobs
From Everand
Steve Jobs
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Yes Please
From Everand
Yes Please
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
The Outsider: A Novel
From Everand
The Outsider: A Novel
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
John Adams
From Everand
John Adams
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
Little Women
From Everand
Little Women
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel