Error in circular queue

Hi, I'm doing a program to insert and remove a circular queue of integers of size 5 and printing on the screen the list with the elements who are in it.
When I run the program it runs normal, but after a insertions and deletions gives it fails to run.
I can not find where is the problem. Could anyone help me?

Here are the source code: http://www.4shared.com/rar/3IL4ny0T/FilaCircular.html

Thanks
You should Sign Up or Login to download this file
can anybody help me?
You can't expect someone to sign up for some service just to help you with your code. The strange message I posted earlier is what I get when I attempted to download your code.

If you would like someone here to help you, you'll need to post your code here. Also, if you post your code here, the thread can serve as help for someone else in the future.

Also, remember to use the code format tag around your code so your formatting is preserved and so we can read it.

Thanks.
Sorry kbw, I do not placed the code here because the code are subdivided in class and it is very large to place here. But I agree with you about the link.
So, follows all the code:

fila.h:
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
#ifndef FILA_H
#define	FILA_H
#include <iostream>
#include <iomanip>
#include <stdlib.h>

#define MAX 5 //tamanho da fila
using namespace std;

class Fila {
private:
    int elemento[MAX]; //elemento da Fila    
    int inicio, fim, total;
public:

    Fila() {//Fila Circular
        inicio = fim = total = 0;
    }

    ~Fila(){};
    bool cheia();
    bool vazia();
    void entra(int, bool&);
    void sai(int&, bool&);
    void imprime();
    void imprimeMenu();
};

#endif	/* FILA_H */



fila.cpp

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include "Fila.h"

bool Fila::cheia() {
    if (this->total == MAX)
        return true;
    else
        return false;
}

bool Fila::vazia() {
    if (this->total == 0)
        return true;
    else
        return false;
}

void Fila::entra(int elem, bool &deuCerto) {
    if (this->cheia())
        deuCerto = false;
    else {
        if (this->total < MAX) {//fim no ultimo elemento e inicio não no inicio
            this->elemento[this->fim] = elem;
            this->total++;

            if (this->fim == MAX - 1)
                this->fim = 0;
            else
                this->fim++;

            deuCerto = true;
        } else {
            deuCerto = false;
        }
    }
}

void Fila::sai(int &elem, bool &deuCerto) {
    if (this->vazia()) {
        this->inicio = 0;
        this->fim = 0;
        deuCerto = false;
    } else {
        elem = this->elemento[this->inicio]; //elem recebe o valor apontado por inicio

        if (this->fim == this->inicio) {//fila cheia
            this->inicio = this->inicio++;
            total--;
            deuCerto = true;
        } else {
            deuCerto = true;
            total--;
            this->inicio = this->inicio++;
        }
    }
}

void Fila::imprime() {
    if (this->vazia())
        cout << "Fila vazia" << endl;
    else {
        cout << "Fila: [ " << " ";
        int atual;
        atual = inicio;
        do {
            cout << this->elemento[atual] << " ";
            atual++;
            if (atual == MAX)
                atual = 0;
        } while (atual != fim);
        cout << " ]";
    }
    cout << endl;
}

void Fila::imprimeMenu() {
    int elemento, op;
    bool deuCerto;
    bool done = true;
    while (done) {
        cout << "Para adicinor elemento digite 1, para remover elemento digite 2, para sair digite 3." << endl;
        cin >> op;
        switch (op) {
            case 1://adiciona elemento
                if (!this->cheia()) {
                    cout << "Entre com um elemento para ser inserido na fila:" << endl;
                    cin >> elemento;
                    this->entra(elemento, deuCerto);
                    if (!deuCerto)
                        cout << "nao foi possivel" << endl;
                    this->imprime();
                } else
                    cout << "Fila Cheia" << endl;
                break;
            case 2://remove elemento
                if (!this->vazia()) {
                    this->sai(elemento, deuCerto);
                    this->imprime();
                } else
                    cout << "Fila Vazia" << endl;
                break;
            case 3:
                this->imprime();
                done = false;
        }
    }
}


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Fila.h"
#include <new>

int main(int argc, char** argv) {
    Fila *F = (Fila*) operator new (sizeof(Fila));
    new (F) Fila;
    
    F->imprimeMenu();
    
    delete F;
    
    return (EXIT_SUCCESS);
}


Thanks for your attention kbw.
closed account (DSLq5Di1)
Lines 46-54.. I'm not sure what you are trying to do there, but inicio should be reset when the value is equivalent to MAX-1 (e.g. lines 26-29).
Last edited on
Let's take a look at entra(). The code is correct, but it's more complicated than it needs to be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Fila::entra(int elem, bool &deuCerto) {
    if (this->cheia())
        deuCerto = false;
    else {
        if (this->total < MAX) {//fim no ultimo elemento e inicio não no inicio
            this->elemento[this->fim] = elem;
            this->total++;

            if (this->fim == MAX - 1)
                this->fim = 0;
            else
                this->fim++;

            deuCerto = true;
        } else {
            deuCerto = false;
        }
    }
}
First, you don't need to keep using this-> to reference class variables. It's necessary in other languages, but just using total implies this->total.

if (this->total < MAX) This code is unnecessary as you know that the queue has space for another element because you're already tested it with if (this->cheia()).


In method sai(), you use the construct this->inicio = this->inicio++; This is really bad, if you want to increment a value, ++inicio is the correct method.

Finally main can be rewritten as:
1
2
3
4
5
6
7
8
9
#include "Fila.h"

int main(int argc, char** argv)
{
    Fila F;
    F.imprimeMenu();

    return (EXIT_SUCCESS);
}
You don't need to dynamically allocate objects in C++.
hi guys, thanks for help.
The error was in lines 46-54 really, slopy9.
The new code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Fila::sair(int &elem, bool &deuCerto) {
    if (vazia()) {
        inicio = 0;
        fim = 0;
        deuCerto = false;
    } else {
        elem = elemento[inicio]; //elem recebe o valor apontado por inicio

        if (fim ==inicio) {//fila cheia
            ++inicio;
            total--;
            deuCerto = true;
        } else {
            deuCerto = true;
            total--;
            if (inicio == MAX - 1)
                inicio=0;
            else
                ++inicio;
        }
    }
}


kbw, thanks for your explanation about this-> and about the constructor this->inicio=this->inicio and other things.

Guys, very thanks!
Topic archived. No new replies allowed.