Queue is a linear data structure. Queue has First In First Out (FIFO) policy. In Queue elements are inserted at one end and deleted at other end. Queue does the insertion by using rear variable and deletion by using front variable. Rear value increases by one unit at insertion and Front value increases by one unit at deletion.
Source Code:
MyStack = []
size=int(input("enter the size of stack"))
def display():
print("Stack currently contains:")
for Item in MyStack:
print(Item)
def push():
if len(MyStack) < size:
value=int(input("enter number"))
MyStack.append(value)
else:
print("Stack is overflow!")
def pop():
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is underflow.")
def stop():
print("you are about to terminate the program")
exit(0)
def menulist():
print("1.push\n2.pop")
print("3.display")
print("4.exit")
def default():
print("check your input")
menulist()
while True:
menu= {
1: push,
2: pop,
3: display,
4: stop}
option = int(input("Please enter your choice"))
menu.get(option,default)()
Output:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
enter the size of stack3
1.push
2.pop
3.display
4.exit
Please enter your choice1
enter number10
Please enter your choice1
enter number20
Please enter your choice1
enter number30
Please enter your choice3
Stack currently contains:
10
20
30
Please enter your choice1
Stack is overflow!
Please enter your choice2
Please enter your choice3
Stack currently contains:
10
20
Please enter your choice2
Please enter your choice3
Stack currently contains:
10
Please enter your choice2
Please enter your choice3
Stack currently contains:
Please enter your choice2
Stack is underflow.
Please enter your choice4
Source Code:
MyStack = []
size=int(input("enter the size of stack"))
def display():
print("Stack currently contains:")
for Item in MyStack:
print(Item)
def push():
if len(MyStack) < size:
value=int(input("enter number"))
MyStack.append(value)
else:
print("Stack is overflow!")
def pop():
if len(MyStack) > 0:
MyStack.pop()
else:
print("Stack is underflow.")
def stop():
print("you are about to terminate the program")
exit(0)
def menulist():
print("1.push\n2.pop")
print("3.display")
print("4.exit")
def default():
print("check your input")
menulist()
while True:
menu= {
1: push,
2: pop,
3: display,
4: stop}
option = int(input("Please enter your choice"))
menu.get(option,default)()
Output:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
enter the size of stack3
1.push
2.pop
3.display
4.exit
Please enter your choice1
enter number10
Please enter your choice1
enter number20
Please enter your choice1
enter number30
Please enter your choice3
Stack currently contains:
10
20
30
Please enter your choice1
Stack is overflow!
Please enter your choice2
Please enter your choice3
Stack currently contains:
10
20
Please enter your choice2
Please enter your choice3
Stack currently contains:
10
Please enter your choice2
Please enter your choice3
Stack currently contains:
Please enter your choice2
Stack is underflow.
Please enter your choice4
No comments:
Post a Comment