-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d807b7
commit 61960b3
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|