Dequeue Is not functioning well

Hello Guys, I'm new to this awesome helpful site. I've been watching alot of tutorial on Queue array implementation and read a lot of answer. But could not find a solution to my problem this is my Enqueue & Dequeue functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#define next(x) ((x + 1) % MAX_SIZE) 

template<class T>
bool Queue<T>::Enqueue(T elem)
{
   LogStart(elem);
   bool status;
        if(isFull()){
            status = false;
            LogEndReturning(status);
            return status;
        }
        queueArray[backIndex] = elem;
        backIndex = next(backIndex);
        status = true;
        LogEndReturning(status);
        return status;
     }


template<class T>
bool Queue<T>::Dequeue()
{
    LogStart();
    bool status;
       if (isEmpty()) {
           status = false;
           LogEndReturning(status);
           return status;
       }

       else {
          for (int i =0; i<next(frontIndex); i++)
              queueArray[i] = queueArray[i-1];
              frontIndex = next(frontIndex);
          status = true;
          LogEndReturning(status);
          return status;
       }

}

now when Engueue 1,2,3 my ToString output:

Queue
frontIndex:0
backIndex:3
Size:3
Empty:false
Full:false
elements
array[0]:1
array[1]:2
array[2]:3

great. But the problem is whenever I degueue() output is:
Queue
frontIndex:1
backIndex:3
Size:3
Empty:false
Full:false
elements
array[0]:0
array[1]:2
array[2]:3

if Dequeue() two more times.
out put is

Queue
frontIndex:3
backIndex:3
Size:3
Empty:true
Full:false
elements
array[0]:0
array[1]:0
array[2]:0

instead of deleting the value. Degugue replacing the value with 0. so how do I actually delete the value.

Any hint or tips.


Last edited on
You keep track of both front and back index so I assume you are trying to implement this as a circular buffer. In that case it should not be necessary to shift any elements when dequeuing.
Topic archived. No new replies allowed.