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

Stack Using Array in Python PDF

The document describes how to implement a stack data structure using an array in Python. A stack class is defined with methods like push(), pop(), peak(), is_full(), print_all(), and size_of_stack() to manipulate a fixed-size stack on a last-in first-out basis. The class initializes the stack with a given size and tracks the top element using a top_most index in a list. Methods allow inserting and removing elements from the top of the stack and checking if it is full or empty.
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)
82 views

Stack Using Array in Python PDF

The document describes how to implement a stack data structure using an array in Python. A stack class is defined with methods like push(), pop(), peak(), is_full(), print_all(), and size_of_stack() to manipulate a fixed-size stack on a last-in first-out basis. The class initializes the stack with a given size and tracks the top element using a top_most index in a list. Methods allow inserting and removing elements from the top of the stack and checking if it is full or empty.
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/ 2

Untitled16

May 5, 2023

# Stack Data Structure using array - Size of stack is fixed (Constant Size) - Insert and delete from
Top - LI-FO Implementation (Last-in First-Out) - Stack size will ask when you creating object of
stack - Some methods are given which help to manupilate your stack - push(value): Push is used
for insert data in stack - pop(): Pop is used for removed data from stack - peak(index): Peak is
used for access data by index - is_full(): is_full is used for current state of stack - print_all():
print_all is used for print all elements of stack - size_of_stack(): is used to find the size of stack.

[304]: class stack:


def __init__(self,sizeOfArray):
self.size_of_array = sizeOfArray
self.list=[]
self.top_most = -1
def push(self,value):
if False if len(self.list) >= self.size_of_array else True:
self.top_most = self.top_most+1
self.list.insert(self.top_most,value)
else:
print("Stack is overflow")
raise Exception("Error: "+str(value)+" cannot be insert in stack")
def pop(self):
if False if self.top_most == -1 else True:
print(self.list[self.top_most])
self.list.pop(self.top_most)
self.top_most= self.top_most-1
else:
print("Stack is underflow")
def peak(self,index):
try:
print(self.list[index])
except:
print(f"{index} Out of stack")
def is_full(self):
if True if len(self.list) >= self.size_of_array else False:
print("You stack has fulled:")
else:
size = self.size_of_array - len(self.list)
c="element" if size == 1 else "elements"
print(f"No, You can insert more {size} {c}")

1
def print_all(self):
rear = self.top_most
for ele in self.list:
print(self.list[rear],"-->", end=" "),
rear = rear -1
def size_of_stack(self):
print(f"Your size of stack is {str(self.size_of_array)}")

[305]: stack = stack(5)

[306]: stack.push(30)
stack.push(40)
stack.push(50)

[307]: stack.list

[307]: [30, 40, 50]

[308]: stack.peak(0)

30

[309]: stack.is_full()

No, You can insert more 2 elements

[310]: stack.print_all()

50 --> 40 --> 30 -->

[311]: stack.size_of_stack()

Your size of stack is 5

[ ]:

You might also like