Data Structures - Stacks and Queues in Python
Implementing Stack in Python using a List
def push(stack, element):
stack.append(element)
top = len(stack) - 1
def pop(stack):
if stack == []:
return 'UNDERFLOW'
element = stack.pop()
if len(stack) == 0:
top = None
else:
top = len(stack) - 1
return element
def peek(stack):
if stack == []:
return 'UNDERFLOW'
top = len(stack) - 1
return stack[top]
def display(stack):
if stack == []:
print('STACK EMPTY')
else:
top = len(stack) - 1
print(str(stack[top]) + "<--top")
for i in range(top - 1, -1, -1):
print(stack[i])
stack = []
top = None
while True:
print('1. PUSH ELEMENT')
print('2. POP ELEMENT')
print('3. PEEK STACK')
print('4. DISPLAY ELEMENTS')
choice = int(input('ENTER YOUR CHOICE: '))
if choice == 1:
element = int(input('Element to be pushed: '))
push(stack, element)
elif choice == 2:
element = pop(stack)
if element == 'UNDERFLOW':
print('STACK UNDERFLOW!')
else:
print(str(element) + ' POPPED')
elif choice == 3:
element = peek(stack)
if element == 'UNDERFLOW':
print('STACK EMPTY')
else:
print('TOPMOST ELEMENT: ' + str(element))
elif choice == 4:
display(stack)
else:
print('Bye!')
break
Implementing Queue in Python using a List
def enqueue(queue, element):
queue.append(element)
if len(queue) == 1:
front = 0
rear = 0
else:
rear = len(queue) - 1
def dequeue(queue):
if queue == []:
return 'UNDERFLOW'
element = queue.pop(0)
if len(queue) == 0:
front = None
rear = None
return element
def peek(queue):
if queue == []:
return 'UNDERFLOW'
return queue[0]
def display(queue):
if queue == []:
print('QUEUE EMPTY')
elif len(queue) == 1:
print(str(queue[0]) + "<---front, rear")
else:
front = 0
rear = len(queue) - 1
print(str(queue[front]) + "<--front")
for i in range(1, rear):
print(queue[i])
print(str(queue[rear]) + "<--rear")
queue = []
front = None
rear = None
while(True):
print('1. ENQUEUE')
print('2. DEQUEUE')
print('3. PEEK QUEUE')
print('4. DISPLAY ELEMENTS')
choice = int(input('ENTER YOUR CHOICE: '))
if choice == 1:
element = int(input('Element to enqueue: '))
enqueue(queue, element)
elif choice == 2:
element = dequeue(queue)
if element == 'UNDERFLOW':
print('QUEUE UNDERFLOW!')
else:
print(str(element) + ' REMOVED')
elif choice == 3:
element = peek(queue)
if element == 'UNDERFLOW':
print('QUEUE EMPTY')
else:
print('FIRST ELEMENT: ' + str(element))
elif choice == 4:
display(queue)
else:
print('Bye!')
break
Implementing Stack using Class and Object
class stack:
def __init__(self):
self.s = []
self.top = -1
def push(self, data):
self.s.append(data)
self.top = self.top + 1
def pop(self):
if self.top == -1:
return -9999
self.top = self.top - 1
return self.s.pop()
def peek(self):
if self.top == -1:
print('Stack empty!')
else:
data = self.s[self.top]
print('Topmost element: ' + str(data))
def display(self):
if self.top == -1:
print('Stack empty!')
else:
print(str(self.s[self.top]) + '<--top')
for i in range(self.top - 1, -1, -1):
print(self.s[i])
my_stack = stack()
while True:
print('1. Push element')
print('2. Pop element')
print('3. Peek stack')
print('4. Display elements')
choice = int(input('Enter your choice: '))
if choice == 1:
data = int(input('Element to be pushed: '))
my_stack.push(data)
elif choice == 2:
d = my_stack.pop()
if d == -9999:
print('Stack underflow!')
else:
print(str(d) + ' popped!')
elif choice == 3:
my_stack.peek()
elif choice == 4:
my_stack.display()
else:
print('Bye!')
break
Implementing Queue using Class and Object
class queue:
def __init__(self):
self.q = []
self.front = -1
self.rear = -1
def enqueue(self, data):
self.q.append(data)
if self.rear == -1:
self.front = 0
self.rear = 0
else:
self.rear = self.rear + 1
def dequeue(self):
if self.q == []:
return -9999
return self.q.pop(self.front)
def display(self):
if self.q == []:
print('Queue empty!')
elif self.front == self.rear:
print(str(self.q[self.front]) + '<--front, rear')
else:
print(str(self.q[self.front]) + '<--front')
for i in range(1, self.rear):
print(str(self.q[i]))
print(str(self.q[self.rear]) + '<--rear')
my_queue = queue()
while True:
print('1. Enqueue')
print('2. Dequeue')
print('3. Display')
choice = int(input('Enter your choice: '))
if choice == 1:
data = int(input('Data to be enqueued: '))
my_queue.enqueue(data)
elif choice == 2:
data = my_queue.dequeue()
if data == -9999:
print('Queue empty!')
else:
print(str(data) + ' deleted')
elif choice == 3:
my_queue.display()
else:
print('Bye')
break
Comments
Post a Comment