Can someone tell me the stupid mistake Im making here to get these return values?


Q.hpp
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
  using namespace std;

template<typename T>
Q<T>::Q() {
    m_size = 0;
    // if (m_size == 0) {
        m_front = nullptr;
        m_back = nullptr;
    // }
}



template<typename T>
bool Q<T>::isEmpty() {
    if (m_size < 1) {
        cout << "isEmpty returning true!\n";
        return true;

    }
    cout << "isEmpty returning false!\n";
    return false;
}


template <typename T>
void Q<T>::push(T entry) {
    QNode<T>* tempNode = new QNode<T>();

    tempNode->setValue(entry);

    if (m_size < 1) {
        m_front = tempNode;
    }

    // makes the current node's front pointer look to the previous back of the Q
    tempNode->setSooner(m_back);
    m_back = tempNode;

    if (m_size > 0) {
        QNode<T>* prevBack = tempNode->getSooner();
        prevBack->setLater(tempNode);
    }
    m_size++;
}

template <typename T>
T Q<T>::pop() {

    // return m_front->getValue();

    if (m_size > 0) {

        T tempValue = m_front->getValue();
        QNode<T>* temp = m_front;
        m_front = m_front->getLater();

        delete temp;

        m_size--;

        return tempValue;
    }
    return -1;
}

template <typename T>
T Q<T>::front() {
    if (!isEmpty()) {
        return m_front->getValue();
    }
    return 0;
}


template <typename T>
int Q<T>::size() {
    return m_size;
}


Q.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
31
32
33
34
35
36
37
38
39
40
#include <iostream>

#include "QNode.h"

#ifndef Q_H
#define Q_H

template<typename T>
class Q {

public:

    Q();

    // ~Q();

    bool isEmpty();

    void push(T entry);

    T pop();

    T front();

    int size();

private:

    int m_size;

    QNode<T>* m_front;
    QNode<T>* m_back;



};

#include "Q.hpp"
#endif


QNode.hpp
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

template<typename T>
QNode<T>::QNode() {

    m_value = 0;
    m_sooner = nullptr;
    m_later = nullptr;
}


template<typename T>
T QNode<T>::getValue() {
    return m_value;
}
template<typename T>
void QNode<T>::setValue(T value) {
    m_value = value;
}


template<typename T>
QNode<T>* QNode<T>::getSooner() {
    return m_sooner;
}
template<typename T>
void QNode<T>::setSooner(QNode<T>* node) {
    m_sooner = node;
}


template<typename T>
QNode<T>* QNode<T>::getLater() {
    return m_later;
}
template<typename T>
void QNode<T>::setLater(QNode<T>* node) {
    m_later = node;
}


QNode.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
31
32
33
34
35
36
37
38
39
40
41
42
43

#ifndef QNODE_H
#define QNODE_H

template<typename T>
class QNode {

public:

    QNode();

    T getValue();

    void setValue(T value);

    QNode<T>* getSooner();

    void setSooner(QNode<T>* node);

    QNode<T>* getLater();

    void setLater(QNode<T>* node);


private:

    QNode<T>* m_sooner;

    QNode<T>* m_later;

    T m_value;



private:


};

#include "QNode.hpp"
#endif



main.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
#include <iostream>

#include "Q.h"

using namespace std;

int main() {


    Q<int>* tempQ = new Q<int>();


    for (int i = 0; i < 10; i++) {
        tempQ->push(i);

    }


    cout << "Size: " << tempQ->size() << "\n";

    for (int i = 1; i < 11; i++) {
        int test = tempQ->front();
        cout << "popped value: " << test << "\n";
    }

    return 0;
}



makefile
1
2
3
4
5
6
7
8
9
test: main.o
	g++ -g -std=c++11 main.o -o test

main.o: Q.h Q.hpp QNode.h
	g++ -g -std=c++11 -c main.cpp

clean:
	rm *.o test


Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Size: 10
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
isEmpty returning false!
popped value: 0
Last edited on
I had an assignment just like this last week so allow me to weigh in. First I think you are making this more complicated than it needs to be by having to separate template classes for the node and the list. I just used a struct for the nodes and had it all in one class. Anyway, I skimmed through your code and nothing stuck out at me for being wrong(linked lists can be a pain in this way). I would start by making a method that prints out the entire list then do this to debug

1
2
3
4
5
for (int i = 0; i < 10; i++) {
        tempQ->push(i);
tempQ->print();

    }

Print the queue each time you add a number, you should see the list grow as you add. If the output doesn't look like it should, then the issue is likely in the push method.
Once that works try this
1
2
3
4
5
 for (int i = 1; i < 11; i++) {
        int test = tempQ->front();
        TempQ->Print();
        cout << "popped value: " << test << "\n";
    }

Again print each time you change the list, it should be shrinking this time, if not check the pop method.
I tried to first implement this using a template struct but was getting weird errors. How would you go about creating such a struct that could hold a template value as well as a 'next' pointer to the trailing item?
1
2
3
4
5
6
7
8
9
10
11
private:
	//nodes of the list
	struct node
	{
		type data; //the item in the list
		node* next; //next pointer
	};

	int size; //current size of list
	node* head; //points to front of the list
	node* tail; //points to the back of the list 

Like this, you shouldn't need a template struct, just your queue class should be a template.
Topic archived. No new replies allowed.