, поэтому у меня есть интерфейс стека и очереди на основе интерфейса списка. Теперь я должен создать интерфейс стека только с использованием интерфейса очереди без использования каких-либо операций со списком или стека.
вот мой код queuestackm.py
from queuem import *
class Stack(Queue):
def __init__(self):
self.s1=Queue()
self.s2=Queue()
def isemptys(s):
return isemptyq(s.s1)
def push(s,x):
enqueue(s.s1,x)
def pop(s):
while not isemptys(s):
tmp=dequeue(s. s1)
enqueue(s.s2, tmp)
x = dequeue (s.s2)
while not isemptyq(s.s2):
tmp2=dequeue (s. s2)
enqueue (s. s1, tmp2)
return x
def gettop(s):
while not isemptys(s):
enqueue(s.s2, dequeue(s.s1))
tmp3 = getfront(s.s2)
while not isemptyq(s.s2):
enqueue(s. s1, dequeue(s.s2))
return tmp3
def displays(s):
displayq (s. s1)
Я могу использовать только операцию из этого файла queuem.py для реализации всех этих функций. я просто не могу понять, что gettop и pop работают правильно.
здесь коды, которые имеют отношение
queuem.py
from listm import *
class Queue(List):
def __init__(self, que=None):
List.__init__(self, que)
self.rear = length(self)
def isemptyq(q):
if isempty(q):
return True
else:
return False
def enqueue(q, x):
q.rear= q.rear + 1
insert(q, q.rear, x)
def dequeue(q):
if isemptyq(q):
print("Dequeue error, queue underflow!!!\n")
return -1
else:
tmp = get(q, 1)
delete(q, 1)
q.rear = q.rear - 1
return tmp
def getfront(q):
if isemptyq(q):
print("Getfront error, queue underflow!!!\n")
return -1
else:
tmp = get(q, 1)
return tmp
def displayq(q):
display(q)
listm.py
class List:
def __init__(self, extlist=None):
if extlist is None:
self.inlist = []
else:
self.inlist = extlist
def length(L):
return len(L.inlist)
def isempty(L):
if len(L.inlist)==0:
return True
else:
return False
def get(L, i):
if isempty(L):
print("The get() is unsuccessful!")
print("The list is empty!")
return -1
elif (i<1) or (i>length(L)):
print("The get() is unsuccessful!")
print("The index given is out of range!")
return -1
else:
return(L.inlist[i-1])
def locatebyvalue(L, x):
position = 1
for item in L.inlist:
if x == item:
return position
else:
position = position+1
return -1
def locatebyid(L, idd):
position = 1
for item in L.inlist:
if item.id == idd:
return position
else:
position = position+1
return -1
def insert(L, i, x):
if (i<1) or (i>length(L)+1):
print("The insert() is unsuccessful!")
print("The index given is out of range!")
else:
L.inlist.insert(i-1, x)
def delete(L, i):
if isempty(L):
print("The delete() is unsuccessful!")
print("The list is empty!")
return
elif (i<1) or (i>length(L)):
print("The delete() is unsuccessful!")
print("The index given is out of range!")
return
else:
del L.inlist[i-1]
def display(L):
print(L.inlist)
stackm.py
from listm import *
class Stack(List):
def __init__(self, stk=None):
List.__init__(self, stk)
self.top = 0
def isemptys(s):
if isempty(s):
return True
else:
return False
def push(s, x):
s.top = s.top + 1
insert(s, s.top, x)
def pop(s):
if isemptys(s):
print("Pop error, stack underflow!!!\n")
return -1
else:
tmp = get(s, s.top)
delete(s, s.top)
s.top = s.top - 1
return tmp
def gettop(s):
if isemptys(s):
print("Gettop error, stack underflow!!!\n")
return -1
else:
tmp = get(s, s.top)
return tmp
def displays(s):
display(s)
И я использую этот файл для запуска queuestackm.py
qstest.py
from queuestackm import *
def main():
qs = Stack()
if(isemptys(qs)):
print("The stack is empty now.")
else:
print("The stack is not empty now.")
push(qs, "a")
push(qs, "b")
push(qs, "c")
print("The push operations has been performed three times.")
if(isemptys(qs)):
print("The stack is empty now.")
else:
print("The stack is not empty now.")
top = gettop(qs)
print(f"Now, the top element is '{top}'.")
print("Now, the stack is containing ", end="")
displays(qs)
pop(qs)
de = pop(qs)
print("The pop operations has been performed two times.")
print(f"The last popped element is '{de}'.")
push(qs, "d")
push(qs, "e")
top = gettop(qs)
print(f"Now, the top element is '{top}'.")
print("Now, the stack is containing ", end="")
displays(qs)
main()