Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrutiAgarwal31 authored Sep 4, 2022
1 parent 3d807b7 commit 61960b3
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Stacks/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Implement Stacks using List.
analogy:
for push use append function of the list.
for pop use the pop function of the list.
both push and pop are done at the same end of the list i.e the end of the list.
"""

stack = []
stack.append(10)
stack.append(20)
stack.append(30)
print(stack)

stack.pop()
print(stack)

stack.pop()
print(stack)

stack.append(40)
stack.append(50)

print(stack)
print('the top element: ', stack[-1])
50 changes: 50 additions & 0 deletions Stacks/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
stack = []
class Stack:
def __init__(self):
self.stack = list()

def push(self):
element = int(input("Enter a integer value: "))
self.stack.append(element)
print(self.stack)

def pop(self):
if(len(self.stack)==0):
print('Stack is Empty')
else:
self.stack.pop()
print(self.stack)

def peek(self):
if (len(self.stack) == 0):
print('Stack is Empty')
else:
print(self.stack[-1])

def isEmpty(self):
if(len(self.stack)==0):
print('Stack is Empty')
else:
print("Stack is not Empty")

stack = Stack()
while True:
print("Select the operations:\n "
"\t1.Push\n"
"\t2.Pop\n"
"\t3.Peek\n"
"\t4.IsEmpty\n"
"\t5.Quit")
choice = int(input("Enter your choice: "))
if choice == 1:
stack.push()
elif choice ==2:
stack.pop()
elif choice == 3:
stack.peek()
elif choice == 4:
stack.isEmpty()
else:
print('Entered the incorrect Choice')
break

42 changes: 42 additions & 0 deletions Stacks/3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Implement stack using Deque Module.
Deque is a double-ended queue.
Only one end of the queue id used for push and pop operations.
For push, append method is used.
For pop, pop method is used.
"""

import collections
stack = collections.deque()

while True:
print("Select the operations:\n "
"\t1.Push\n"
"\t2.Pop\n"
"\t3.Peek\n"
"\t4.IsEmpty\n"
"\t5.Quit")
choice = int(input("Enter your choice: "))
if choice == 1:
data = int(input("Enter an integer value: "))
stack.append(data)
print(stack)
elif choice ==2:
if (len(stack) == 0):
print("Stack is Empty")
else:
stack.pop()
elif choice == 3:
if(len(stack)==0):
print("Stack is Empty")
else:
print(stack[-1])
elif choice == 4:
if (len(stack) == 0):
print("Stack is Empty")
else:
print("Stack is not Empty")
else:
print('Entered the incorrect Choice')
break

49 changes: 49 additions & 0 deletions Stacks/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Implement stack using Queue Module.
For push, put method is used.
For pop, get method is used.
if the stack is empty and you are trying to "pop" the element
then it will go into the blocking stage until the stack gets a
new element. to resolve this timeout is used.
It displays the error message after a particular time.
not stack ==> length of stack is Zero.
"""
import queue

stack = queue.LifoQueue()
while True:
print("Select the operations:\n "
"\t1.Push\n"
"\t2.Pop\n"
"\t3.Peek\n"
"\t4.IsEmpty\n"
"\t5.Quit")
choice = int(input("Enter your choice: "))
if choice == 1:
data = int(input("Enter an integer value: "))
stack.put(data)
print(stack.queue)
elif choice ==2:
if (not stack):
print("Stack is Empty")
else:
stack.get(timeout=1)
print(stack.queue)
elif choice == 3:
if(not stack):
print("Stack is Empty")
else:
print(stack.queue[-1])
elif choice == 4:
if (not stack):
print("Stack is Empty")
else:
print("Stack is not Empty")
else:
print('Entered the incorrect Choice')
break


0 comments on commit 61960b3

Please sign in to comment.