count function for queues

I need some help with setting up a count function for this code that i have. I'm not at all sure how to edit the code in there and my teacher explained that i have to edit a few functions to get it to work, but i don't understand why i need to.

main cpp file
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
#include <iostream>
#include "header.h"

using namespace std;
 
void testCopyConstructor(queueType<int> otherQueue);
 
int main()
{
	queueType<int> queue1, queue2;
	int x, y;

	x = 4;
	y = 5;
	queue1.addQueue(x);
	queue1.addQueue(y);
	x = queue1.front();
	queue1.deleteQueue();
	queue1.addQueue(x + 5);
	queue1.addQueue(16);
	queue1.addQueue(x);
	queue1.addQueue(y - 3);

	testCopyConstructor(queue1);	

	queue2 = queue1;

	cout << "queue1: ";

	while (!queue1.isEmptyQueue())
	{
		cout << queue1.front() << " ";
		queue1.deleteQueue();
	}

	cout << endl;

	cout << "queue2: ";

	while (!queue2.isEmptyQueue())
	{
		cout << queue2.front() << " ";
		queue2.deleteQueue();
	}

	cout << endl;
	cin.get();
	return 0;
}

void testCopyConstructor(queueType<int> otherQueue)
{
	if (!otherQueue.isEmptyQueue())
	{
		cout << "Other Queue is not empty" << endl;
		cout << "Front element of Other Queue : " 
			 << otherQueue.front() << endl;
		otherQueue.deleteQueue();
	}
}


heres the header file
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//Header file QueueAsArray

#ifndef H_HEADER
#define H_HEADER
  
#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
class queueType
{
public:
    const queueType<Type>& operator=(const queueType<Type>&); 
            // overload the assignment operator
    void initializeQueue();
    int isEmptyQueue() const;
    int isFullQueue() const;

    Type front() const;

    Type back() const;
	int queueCount();
    void addQueue(Type queueElement);
    void deleteQueue();

    queueType(int queueSize = 100); 
    queueType(const queueType<Type>& otherQueue); // copy constructor
    ~queueType(); //destructor

private:
    int maxQueueSize;
    int queueFront;
    int queueRear;
	int Count;
    Type *list;  //pointer to array that holds queue elements 
};

template<class Type>
void queueType<Type>::initializeQueue()
{
    queueFront = maxQueueSize - 1;
    queueRear = maxQueueSize - 1;
}

template<class Type>
int queueType<Type>::isEmptyQueue() const
{
    return (queueFront == queueRear);
}

template<class Type>
int queueType<Type>::isFullQueue() const
{
    return ((queueRear + 1) % maxQueueSize == queueFront);
}

template<class Type>
void queueType<Type>::addQueue(Type newElement)
{
    if (!isFullQueue())
    {   
        queueRear = (queueRear + 1) % maxQueueSize; 
        list[queueRear] = newElement;
    }
    else
        cout << "Cannot add to a full queue." << endl; 
}

template<class Type>
void queueType<Type>::deleteQueue()
{
    if (!isEmptyQueue())
        queueFront = (queueFront + 1) % maxQueueSize; 
    else
        cout << "Cannot remove from an empty queue." << endl;
}

template<class Type>
Type queueType<Type>::front() const
{
    assert(!isEmptyQueue());
    return list[(queueFront + 1) % maxQueueSize]; 
}

template<class Type>
Type queueType<Type>::back() const
{
    assert(!isEmptyQueue());
    return list[(queueRear + 1) % maxQueueSize];
}

template<class Type>
queueType<Type>::queueType(int queueSize)   //constructor
{
    if (queueSize <= 0)
    {
        cout << "Size of the array to hold the queue must "
             << "be positive."<<endl;
        cout << "Creating an array of size 100." << endl;

        maxQueueSize = 100;
    }
    else
        maxQueueSize = queueSize;  //set maxQueueSize to queueSize

    queueFront = maxQueueSize - 1;  //initialize queueFront
    queueRear = maxQueueSize - 1;   //initiaize queueRear
    list = new Type[maxQueueSize];  //create the array to
				                    //hold queue elements
}

template<class Type>
queueType<Type>::~queueType()   //destructor
{
    delete [] list;
}

template<class Type>
const queueType<Type>& queueType<Type>::operator=
	                   (const queueType<Type>& otherQueue)
{
    int j;

    if (this != &otherQueue) //avoid self-copy
    {
        maxQueueSize = otherQueue.maxQueueSize;
        queueFront = otherQueue.queueFront;
        queueRear = otherQueue.queueRear;	

        delete [] list;
        list = new Type[maxQueueSize];

        if (queueFront != queueRear)						//if other queue is not empty
        for (j = (queueFront + 1) % maxQueueSize; j <= queueRear; 
                                j = (j + 1) % maxQueueSize)  //copy other queue in this queue
            list[j] = otherQueue.list[j];
    } //end if

    return *this;
}

template<class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
	maxQueueSize = otherQueue.maxQueueSize;
	queueFront = otherQueue.queueFront;
	queueRear = otherQueue.queueRear;

	list = new Type[maxQueueSize];

	if (queueFront != queueRear)	//if other queue is not empty
		for (int j = (queueFront + 1) % maxQueueSize; j <= queueRear; 
								   j = (j + 1) % maxQueueSize)  //copy other queue in this queue
			list[j] = otherQueue.list[j];
	
}

#endif 


ive added the variable count in there already but i also have to add a queueCount function
Think about it like this:
If you add an Item onto your queue, and count was keeping track of that, what would count need to do? (in terms of increasing or decreasing).
Also, if you're removing something from your queue, what would count need to do? (same terms).

Let's say I have 8 items in my queue, and then I remove two, and add one:
count - 1; //for the first removal
count - 1; //for the second
cout + 1; //for the third

Now that that's all done, what would you do to show how many items are in your queue? (Hint: If I gave you a function that was supposed to do nothing but return a variable, how would I do that?)
Last edited on
Also, when Count is increasing and decreasing: where would be the best place for that to be tracked?
Okay i got a program running but its giving me some crazy numbers


header file
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//Header file QueueAsArray

#ifndef H_HEADER
#define H_HEADER
  
#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
class queueType
{
public:
    const queueType<Type>& operator=(const queueType<Type>&); 
            // overload the assignment operator
    void initializeQueue();
    int isEmptyQueue() const;
    int isFullQueue() const;

    Type front() const;

    Type back() const;
	int queueCount();
    void addQueue(Type queueElement);
    void deleteQueue();

    queueType(int queueSize = 100); 
    queueType(const queueType<Type>& otherQueue); // copy constructor
    ~queueType(); //destructor

private:
    int maxQueueSize;
    int queueFront;
    int queueRear;
	int count;
    Type *list;  //pointer to array that holds queue elements 
};


template<class Type>
int queueType<Type>::queueCount()
{
	return count;
}

template<class Type>
void queueType<Type>::initializeQueue()
{
    queueFront = maxQueueSize - 1;
    queueRear = maxQueueSize - 1;
}

template<class Type>
int queueType<Type>::isEmptyQueue() const
{
    return (queueFront == queueRear);
}

template<class Type>
int queueType<Type>::isFullQueue() const
{
    return ((queueRear + 1) % maxQueueSize == queueFront);
}

template<class Type>
void queueType<Type>::addQueue(Type newElement)
{
    if (!isFullQueue())
    {   
        queueRear = (queueRear + 1) % maxQueueSize; 
        list[queueRear] = newElement;
    }
    else
        cout << "Cannot add to a full queue." << endl;
	count = count + 1;
}

template<class Type>
void queueType<Type>::deleteQueue()
{
    if (!isEmptyQueue())
        queueFront = (queueFront + 1) % maxQueueSize;
    else
        cout << "Cannot remove from an empty queue." << endl;
	count = count - 1;
}

template<class Type>
Type queueType<Type>::front() const
{
    assert(!isEmptyQueue());
    return list[(queueFront + 1) % maxQueueSize]; 
}

template<class Type>
Type queueType<Type>::back() const
{
    assert(!isEmptyQueue());
    return list[(queueRear + 1) % maxQueueSize];
}

template<class Type>
queueType<Type>::queueType(int queueSize)   //constructor
{
    if (queueSize <= 0)
    {
        cout << "Size of the array to hold the queue must "
             << "be positive."<<endl;
        cout << "Creating an array of size 100." << endl;

        maxQueueSize = 100;
    }
    else
        maxQueueSize = queueSize;  //set maxQueueSize to queueSize

    queueFront = maxQueueSize - 1;  //initialize queueFront
    queueRear = maxQueueSize - 1;   //initiaize queueRear
    list = new Type[maxQueueSize];  //create the array to
				                    //hold queue elements
}

template<class Type>
queueType<Type>::~queueType()   //destructor
{
    delete [] list;
}

template<class Type>
const queueType<Type>& queueType<Type>::operator=(const queueType<Type>& otherQueue)
{
    int j;

    if (this != &otherQueue) //avoid self-copy
    {
        maxQueueSize = otherQueue.maxQueueSize;
        queueFront = otherQueue.queueFront;
        queueRear = otherQueue.queueRear;	

        delete [] list;
        list = new Type[maxQueueSize];

        if (queueFront != queueRear)						//if other queue is not empty
        for (j = (queueFront + 1) % maxQueueSize; j <= queueRear; 
                                j = (j + 1) % maxQueueSize)  //copy other queue in this queue
            list[j] = otherQueue.list[j];
    } //end if

    return *this;
}

template<class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
	maxQueueSize = otherQueue.maxQueueSize;
	queueFront = otherQueue.queueFront;
	queueRear = otherQueue.queueRear;

	list = new Type[maxQueueSize];

	if (queueFront != queueRear)	//if other queue is not empty
		for (int j = (queueFront + 1) % maxQueueSize; j <= queueRear; 
								   j = (j + 1) % maxQueueSize)  //copy other queue in this queue
			list[j] = otherQueue.list[j];
	
}

#endif 


main file
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
#include <iostream>
#include "header.h"

using namespace std;
 
void testCopyConstructor(queueType<int> otherQueue);
 
int main()
{
	queueType<int> queue1, queue2;
	int x, y;

	x = 4;
	y = 5;
	queue1.addQueue(x);
	queue1.addQueue(y);
	x = queue1.front();
	queue1.deleteQueue();
	queue1.addQueue(x + 5);
	queue1.addQueue(16);
	queue1.addQueue(x);
	queue1.addQueue(y - 3);

	testCopyConstructor(queue1);	

	queue2 = queue1;

	cout << "queue1: ";

	while (!queue1.isEmptyQueue())
	{
		cout << queue1.front() << " ";
		queue1.deleteQueue();
	}

	cout << endl << "the number of elements in queue 1 is " << queue1.queueCount() << endl;

	cout << "queue2: ";

	while (!queue2.isEmptyQueue())
	{
		cout << queue2.front() << " ";
		queue2.deleteQueue();
	}

	cout << endl << "the number of elements in queue 1 is " << queue2.queueCount() << endl;

	cin.get();
	return 0;
}

void testCopyConstructor(queueType<int> otherQueue)
{
	if (!otherQueue.isEmptyQueue())
	{
		cout << "Other Queue is not empty" << endl;
		cout << "Front element of Other Queue : " 
			 << otherQueue.front() << endl;
		otherQueue.deleteQueue();
	}
}
Sometimes -- a lot of times, actually -- teachers confuse me.

In order to keep a queue, you must have some sort of inbuilt count. In this class's design, the count is (the difference from the index of the first and last items, plus the max queue size) mod max queue size.
1
2
3
4
5
6
7
8
9
10
11
12
template<class Type>
void queueType<Type>::addQueue(Type newElement)
{
    if (!isFullQueue())
    {   
        queueRear = (queueRear + 1) % maxQueueSize; 
        list[queueRear] = newElement;
    }
    else
        cout << "Cannot add to a full queue." << endl;
	count = count + 1;
}


^^^This function doesn't work as I think you're intending it to. Here, regardless if it actually adds or not, you have it increase.. Shouldn't you instead have it increase only when actually adding?

1
2
3
4
5
6
7
8
9
template<class Type>
void queueType<Type>::deleteQueue()
{
    if (!isEmptyQueue())
        queueFront = (queueFront + 1) % maxQueueSize;
    else
        cout << "Cannot remove from an empty queue." << endl;
	count = count - 1;
}

^^^The same problem here. Whether or not you remove anything, it decreases its size when called.

Other than that, you wrote a pretty solid modification :)
Last edited on
i think i got it, i looked up different types of solutions to this problem and the simplest one i found was to just get the absolute value of the queue and return that, it seems to work but i wanted to know if this is a practical solution to this problem?

new modified header file
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//Header file QueueAsArray

#ifndef H_HEADER
#define H_HEADER
  
#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
class queueType
{
public:
    const queueType<Type>& operator=(const queueType<Type>&); 
            // overload the assignment operator
    void initializeQueue();
    int isEmptyQueue() const;
    int isFullQueue() const;

    Type front() const;

    Type back() const;
	int queueCount();
    void addQueue(Type queueElement);
    void deleteQueue();

    queueType(int queueSize = 100); 
    queueType(const queueType<Type>& otherQueue); // copy constructor
    ~queueType(); //destructor

private:
    int maxQueueSize;
    int queueFront;
    int queueRear;
	int count;
    Type *list;  //pointer to array that holds queue elements 
};


template<class Type>
int queueType<Type>::queueCount()
{
        count = abs(queueRear);
	return count;
}

template<class Type>
void queueType<Type>::initializeQueue()
{
    queueFront = maxQueueSize - 1;
    queueRear = maxQueueSize - 1;
}

template<class Type>
int queueType<Type>::isEmptyQueue() const
{
    return (queueFront == queueRear);
}

template<class Type>
int queueType<Type>::isFullQueue() const
{
    return ((queueRear + 1) % maxQueueSize == queueFront);
}

template<class Type>
void queueType<Type>::addQueue(Type newElement)
{
    if (!isFullQueue())
    {   
        queueRear = (queueRear + 1) % maxQueueSize; 
        list[queueRear] = newElement;
    }
    else
	{
        cout << "Cannot add to a full queue." << endl;
	}
}

template<class Type>
void queueType<Type>::deleteQueue()
{
    if (!isEmptyQueue())
	{
        queueFront = (queueFront + 1) % maxQueueSize;
	}
    else
	{
        cout << "Cannot remove from an empty queue." << endl;
	}
}

template<class Type>
Type queueType<Type>::front() const
{
    assert(!isEmptyQueue());
    return list[(queueFront + 1) % maxQueueSize]; 
}

template<class Type>
Type queueType<Type>::back() const
{
    assert(!isEmptyQueue());
    return list[(queueRear + 1) % maxQueueSize];
}

template<class Type>
queueType<Type>::queueType(int queueSize)   //constructor
{
    if (queueSize <= 0)
    {
        cout << "Size of the array to hold the queue must "
             << "be positive."<<endl;
        cout << "Creating an array of size 100." << endl;

        maxQueueSize = 100;
    }
    else
        maxQueueSize = queueSize;  //set maxQueueSize to queueSize

    queueFront = maxQueueSize - 1;  //initialize queueFront
    queueRear = maxQueueSize - 1;   //initiaize queueRear
    list = new Type[maxQueueSize];  //create the array to
				                    //hold queue elements
}

template<class Type>
queueType<Type>::~queueType()   //destructor
{
    delete [] list;
}

template<class Type>
const queueType<Type>& queueType<Type>::operator=(const queueType<Type>& otherQueue)
{
    int j;

    if (this != &otherQueue) //avoid self-copy
    {
        maxQueueSize = otherQueue.maxQueueSize;
        queueFront = otherQueue.queueFront;
        queueRear = otherQueue.queueRear;	

        delete [] list;
        list = new Type[maxQueueSize];

        if (queueFront != queueRear)						//if other queue is not empty
        for (j = (queueFront + 1) % maxQueueSize; j <= queueRear; 
                                j = (j + 1) % maxQueueSize)  //copy other queue in this queue
            list[j] = otherQueue.list[j];
    } //end if

    return *this;
}

template<class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
	maxQueueSize = otherQueue.maxQueueSize;
	queueFront = otherQueue.queueFront;
	queueRear = otherQueue.queueRear;

	list = new Type[maxQueueSize];

	if (queueFront != queueRear)	//if other queue is not empty
		for (int j = (queueFront + 1) % maxQueueSize; j <= queueRear; 
								   j = (j + 1) % maxQueueSize)  //copy other queue in this queue
			list[j] = otherQueue.list[j];
	
}

#endif 
Last edited on
Topic archived. No new replies allowed.