Pointer Queue as expected unless I remove all items from queue and start over.

Who can tell me what is wrong:

I've attempted a bunch of quick fixes but I can't seem to figure out what the problem is.

It appears, the problem is occurring here:
while (it -> next != NULL)

After creating a queue, then removing all items from it, I can then add one more item to it, before the program crashes when executing the above code.

myqueue.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef MYQUEUE_Q
#define MYQUEUE_Y

#include "node.h"
#include <cstdlib>

template <class objT>
class myqueue {
    private:
    node<objT> * it; node<objT> * head;

    public:

    objT read() {if (it != NULL) return it -> data;}

    bool lineup(objT o, bool test)
        {
            //cout << "thesize = " << thesize();

            static bool second = false;
            int q = thesize(); cout << "Q = " << q << endl;
            if (test) if (second || !second) {cout << "second = " << second << endl; } //exit(10000); }

            if (q == 0)
            //if (!thesize())
                {cout << "inside this loop\n";
                    second = false;
                    if (it == NULL)
                        {
                            it = new node<objT>;
                            it -> data = 0;
                            it -> next = NULL;
                            //second = true;
                        }
                }
                //we make it here
            if (!second)
                {
                    it -> data = o;
                    second = true;
                    return true;
                }
            //we make it here
            cout << "we make it here\n";
            //if (it -> next != NULL) {if (test) exit(42); }
            if (it -> next == NULL) ;
            //bool tb = it -> next == NULL;
            //cout << "tb = " << tb << endl;
            cout << "We don't make it here\n";

            //while (!(it -> next == NULL) )
            while (it -> next != NULL)
                {
                    cout << "we do not even make it here also.\n" << endl;
                    it = it -> next;
                }
            //we do not make it here
            it -> next = new node<objT>;
            it = it -> next;
            it -> data = o;
            it -> next = NULL;
            it = head;
            return true;
        }

    objT dequeue()
        {
            if (it != NULL)
                {
                    objT dat = read(); //it -> data;
                    node<objT> * T = it -> next;
                    it = T; head = it;
                    return dat;
                }
        }

    int thesize()
        {
            if (it != NULL)
                {//cout << "inside if\n";
                    int counter = 1;
                    node<objT> * T; T = it;
                    //cout << "after declaration\n";
                    while (T -> next != NULL)
                        {  T = T -> next; ++counter;  }
                    //cout << "after while\n";
                    it = head;
                    //cout << "counter = " << counter << endl;
                    cout << "counter = " << counter << endl;
                    return counter;
                }//
            return 0;
        }

    void discard(){
        if (it != NULL)
            {
                node<objT> * T = it -> next;
                it = T;
                head = it;
                return;
            }
        }

    myqueue()
        {
            it = new node<objT>;
            head = new node<objT>;
            head = it;
            it -> next = NULL;
        }

    myqueue(const myqueue & rhs)
        {
            it = rhs;
            head = it;
        }

    myqueue(const objT & o)
        {
            it = new node<objT>;
            head = new node<objT>;
            it -> data = o;
            it -> next = NULL;
            head = it;
        }
};
#endif


main program:
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
using namespace std;
#include <iostream>
#include "myqueue.h"
#include <stdio.h>


using namespace std;

int main()
{
    myqueue<int> list;
    //int a = 5, b = 10, c = 0, d = -3, e = 12;

    list.lineup(5, 0); list.lineup(10, 0); list.lineup(0, 0); list.lineup(-3, 0); list.lineup(12, 0); list.lineup(5, 0); list.lineup(200, 0);
    cout << "The size of the list = " << list.thesize() << endl;
    cout << "The size of the list = " << list.thesize() << endl;
    cout << "First the top of the list = " << list.read() << endl;
    list.discard();
    cout << "I am removing a " << list.dequeue() << " from the list.\n" << endl;
    cout << "I am removing a " << list.dequeue() << " from the list.\n" << endl;
    cout << "The top of the list = " << list.read() << endl;
    //cout << "Now the size = " << list.thesize() << endl;
    cout << "before discarding the size = " << list.thesize() << endl;
    int size = list.thesize();
    for (int i = 0; i < size; ++i)
        list.discard();
    cout << "after discarding the size = " << list.thesize() << endl;
    list.lineup(1000, 0); cout << "after list.lineup()\n"; list.lineup(2000, 1); exit(96); //list.lineup(3000); list.lineup(4000);


    cout << "the top = " << list.read() << endl;
    cout << "thesize = " << list.thesize() << endl;



    printf("\nYou have reached the end of test.cpp\n");
    //cout << "You have reached the end of test.cpp\n" << endl;
    return 1050;

}


node.h:
1
2
3
4
5
6
7
8
9
10
#ifndef node_h
#define node_h

template <class item>
struct node {
    node * next;
    item data;
};

#endif 
Topic archived. No new replies allowed.