Created
November 12, 2016 11:39
-
-
Save mohitmakhija1/c163108bc810a4a0caefede9d05590a9 to your computer and use it in GitHub Desktop.
Simple Stack implementation in Python
This file contains hidden or 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
__author__ = 'MOHIT' | |
# program to implement stack in python using python lists | |
# create a simple empty list | |
stax = [] | |
# top variable | |
top = -1 | |
# push function for stack | |
def push(element): | |
stax.append(element) | |
global top | |
top += 1 | |
print("ELEMENT PUSHED") | |
return | |
def pop(): | |
global top | |
if top == -1: | |
print("STACK EMPTY!!") | |
return | |
else: | |
element = stax[top] | |
del stax[top] | |
global top | |
top -= 1 | |
print("THE ELEMENT HAS BEEN POPPED") | |
return element | |
cont = 1 | |
while cont == 1: | |
# set continue to false | |
cont = 0 | |
# show choices | |
print("FOLLOWING CHOICES ARE AVAILIABLE : ") | |
print("1.PUSH ELEMENT IN STACK") | |
print("2.POP ELEMENT FROM STACK") | |
print("3.DISPLAY THE CONTENTS OF STACK") | |
choice = int(input("ENTER YOUR CHOICE : ")) | |
print(choice) | |
if (choice == 1): | |
element = int(input("ENTER THE ELEMENT TO BE PUSHED : ")) | |
push(element) | |
elif choice == 2: | |
element = pop() | |
print("THE ELEMENT POPPED IS " + str(element)) | |
elif choice == 3: | |
print(stax) | |
cont = int(input("DO YOU WANT TO CONTINUE : ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment