INDEXERROR on Enqueue function (Python)

19 views Asked by At

I'm writing the functions for enqueue and dequeue. I know I can use the built in functions, but I have to focus on the logic and working of these (am a 12 grade student) and so I need to write the program instead of using built-in functions. Here's my code:

#queues
global fptr, rptr, MaxQsize, MyQ
fptr = 0
rptr = -1
MaxQsize = 8
MyQ = []

def deq(MyQ, fptr):
    #check for underflow
    if (rptr == fptr + 1) and (MyQ[fptr] == ""):
        print("ERROR: Underflow")
    else:
        x = MyQ[fptr]
        del(MyQ[fptr])
        print("You have deleted", x)

def enq(MyQ, rptr, dataitem):
    #check for overflow
    if (rptr == MaxQsize - 1) and (MyQ[fptr] != ""):
        print("ERROR: Overflow")
    elif (rptr == MaxQsize - 1) and (MyQ[fptr] == ""):
        rptr = 0
        MyQ[rptr] = dataitem
        print("You have added", dataitem, "to the queue")
    else:
        rptr += 1
        MyQ[rptr] = dataitem
        print("You have added", dataitem, "to the queue")
        
        
        
enq(MyQ, -1, "apple")
print(MyQ)

When run, it shows the following error:

Traceback (most recent call last):
  File "<string>", line 32, in <module>
  File "<string>", line 27, in enq
ERROR!
IndexError: list assignment index out of range
> 

What is exactly wrong with my code? And how do I fix it?

0

There are 0 answers