I have made a stack and queue implementation with OOP in Python (sorry for variable names) . It is made from scratch. I just watched a video how these two things work. I am still trying to improve in OOP so I would be thankful for every advice. Two things which bother me the most are using que and sta in the beginning and whether it is okay to just use already implemented functions in Python such as .pop() .
QUEUE
que = []
class Queue:
def __init__(self,queue):
self.queue = queue
def push(self,item):
print ("PUSH",item)
if len(self.queue)!=0:
self.queue.append(0)
for i in range(len(self.queue)-1,0,-1): ## replacing elements
self.queue[i] = self.queue[i-1]
self.queue[0] = item
else:
self.queue.append(item)
def pop(self):
print ("POP",self.queue[-1])
if len(self.queue)!=0:
self.queue.pop()
else:
raise ValueError('Cant pop from empty queue.')
def __str__(self):
return (str(self.queue))
## creating QUEUE
que = Queue(que)
que.push(5)
print (que)
que.push(6)
print (que)
que.push(7)
print (que)
que.pop()
print (que)
STACK
class Stack:
def __init__(self,stack):
self.stack = stack
def push(self,item):
print ("PUSH",item)
if len(self.stack)!=0:
self.stack.append(0)
for i in range(len(self.stack)-1,0,-1):
self.stack[i] = self.stack[i-1]
self.stack[0] = item
else:
self.stack.append(item)
def pop(self):
print ("POP",self.stack[0])
if len(self.stack)!=0:
self.stack.pop(0)
else:
raise ValueError('Cant pop from empty stack.')
def __str__(self):
return (str(self.stack))
## creating STACK
stac = Stack(sta)
stac.push(5)
print(stac)
stac.push(7)
print(stac)
stac.push(2)
print(stac)
stac.pop()
print(stac)
que? What issta? As presented, the code doesn't run. \$\endgroup\$queis defined in the first statement. Assuming thatstais similarly defined, this code is reviewable. \$\endgroup\$