Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create reverseastack.py #201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions reverseastack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# create class for stack
class Stack1:

# create empty list
def __init__(self):
self.Elements = []

# push() for insert an element
def push(self, value):
self.Elements.append(value)

# pop() for remove an element
def pop(self):
return self.Elements.pop()

# empty() check the stack is empty of not
def empty(self):
return self.Elements == []

# show() display stack
def show(self):
for value in reversed(self.Elements):
print(value)

# Insert_Bottom() insert value at bottom
def BottomInsert(s, value):

# check the stack is empty or not
if s.empty():

# if stack is empty then call
# push() method.
s.push(value)

# if stack is not empty then execute
# else block
else:
popped = s.pop()
BottomInsert(s, value)
s.push(popped)

# Reverse() reverse the stack
def Reverse(s):
if s.empty():
pass
else:
popped = s.pop()
Reverse(s)
BottomInsert(s, popped)


# create object of stack class
stk0 = Stack1()

stk0.push(1)
stk0.push(2)
stk0.push(3)
stk0.push(4)
stk0.push(5)

print("The Original Stack is")
stk0.show()

print("\nStack after Reversing it -")
Reverse(stk0)
stk0.show()