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

Stacks

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

Stacks

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1.

Understanding the fundamental idea behind a stack


and how its Python data structure is implemented.

2. Describe how the Stack abstract data type behaves.

3. Determine example applications with Stack.


A Stack is a linear data structure that holds a
linear, ordered sequence of elements. It is an
abstract data type. A Stack works on the LIFO
process (Last In First Out), i.e., the element
that was inserted last will be removed first.

To implement the Stack, it is required to


maintain a pointer to the top of the Stack,
which is the last element to be inserted
because we can access the elements only on
the top of the Stack.
> INTERNET BROWSERS
> UNDO / REDO FEATURE
> REVERSE A STRING
> PARSING - CHECK FOR MATCHING PARENTHESIS
OR BRACKETS
•TO IMPLEMENT A STACK, ITEMS ARE INSERTED AND REMOVED AT THE SAME END (CALLED THE
TOP)

•EFFICIENT ARRAY IMPLEMENTATION REQUIRES THAT THE TOP OF THE STACK BE TOWARDS
THE CENTER OF THE ARRAY, NOT FIXED AT ONE END

•TO USE AN ARRAY TO IMPLEMENT A STACK, YOU NEED BOTH THE ARRAY ITSELF AND AN
INTEGER

•THE INTEGER TELLS YOU EITHER:

-WHICH LOCATION IS CURRENTLY THE TOP OF THE STACK, OR HOW MANY ELEMENTS ARE IN
THE STACK
A list is a finite, ordered sequence of
data items known as elements
("ordered" means that each element
has a position in the list)

The List implementations are grouped


into general-purpose and special-
purpose implementations.
PYTHON’S BUILT-IN DATA STRUCTURE
LIST CAN BE USED AS A STACK.
INSTEAD OF PUSH(),APPEND() IS USED
TO ADD ELEMENTS TO THE TOP OF THE
STACK WHILE POP() REMOVES THE
ELEMENT IN LIFO ORDER.
OUTPUT:
Python stack can be implemented using the deque class
from the collections module. Deque is preferred over the list
in the cases where we need quicker append and pop
operations from both the ends of the container.
Queue module also has LIFO
Queue, which is basically a stack.
Data is inserted into Queue using
the put() function and get()
takes data out from the Queue.
get(): Remove and Return an item from the
queue. Ifthe queue is empty wait until an item
available
put(): Put an item into the queue. If the queue is
full, wait until a free slot is available before
adding the item.
qsize: Return the number of items in the queue.
full(): is used to check if the queue is full and it
returns True if the queue is full.
maxsize(): an integer that tells the upper bound
limit of the number of elements that can be
entered in the queue.
OUTPUT->
Pushing in Python refers to adding an element to the
top of a stack. By using ‘append()’ When you append
an element to a list, it gets added to the end, effectively
making it the top element of the stack.

Popping in Python refers to removing the top element


from a stack. This is done using the ‘pop()’ method. The
pop() method removes and returns the last element
from the list, making it ideal for implementing the pop
operation in a stack.
Top Element Removal: The element at the top of the stack, which was the most recently added, is removed.

Size Reduction: The size of the stack decreases by one, reflecting the removal of the top element.

Stack State Adjustment: If the stack becomes empty after the pop operation, it transitions to an empty state, indicating
that there are no elements remaining.

Memory Management: Depending on the implementation, memory previously allocated for the popped element may be
freed up, contributing to efficient memory usage.

Error Handling: If popping is attempted on an empty stack, it may result in an error. Robust error handling mechanisms
should be in place to address such scenarios and prevent program instability.
AFTER POPPING
A stack is a linear data structure that stores items in a Last-
In/First-Out (LIFO) or First-In/Last-Out (FILO) manner.
In stack, a new element is added at one end and an element is
removed from that end only.
The insert and delete operations are often called push and pop.
OUTPUT

Python’s built-in data structure list can be used as a stack.


Instead of push(), append() is used to add elements to the
top of the stack while pop() removes the element in LIFO
order.

You might also like