Que functions

Guys any ideas on how to complete these code including part 2-8 in int main instructions?

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
#include<iostream>
#include <queue>


template <typename T>
class queueArr{
private:
T *q_array;
int q_capacity;
int q_size = 0, q_front = 0, q_back = 0;
public:
queueArr(int newCapacity);
bool isEmpty();
int qSize();
void displayFront();
void displayBack();
void enqueue(T newData);
void dequeue();
void clear();

//big three
};

template <typename T>
queueArr<T>::queueArr(int newCapacity) {
q_capacity = newCapacity;
q_array = new T[q_capacity];
}

template <typename T>
bool queueArr<T>::isEmpty(){
return q_size == 0;
}

template <typename T>
int queueArr<T>::qSize() {
return q_size;
}

template <typename T>
void queueArr<T>::displayFront() {
std::cout << "Front: " << q_array[q_front%q_capacity] << std::endl;
}

template <typename T>
void queueArr<T>::displayBack() {
std::cout << "Back: " << q_array[q_back%q_capacity] << std::endl;
}

template <typename T>
void queueArr<T>::clear() {
q_size = 0;
q_front = 0;
q_back = 0;
}

template <typename T>
void queueArr<T>::enqueue(T newData) {
//check if full
if(q_size==q_capacity){
std::cout << "Queue is Full.\n";
return;
}

//if empty
if(isEmpty()){
q_back = q_front;
q_array[q_front%q_capacity] = newData;
std::cout << newData << " was successfully enqueued.\n";
q_size++;
return;
}

//if normal
q_back++;
q_array[q_back%q_capacity] = newData;
std::cout << newData << " was successfully enqueued.\n";
q_size++;
}

template<typename T>
void queueArr<T>::dequeue() {
T temp;
//check if empty
if(isEmpty()){
std::cout << "Queue is Empty.\n";
return;
}

//check if only one element
if((q_front == q_back) && !(isEmpty())){
temp = q_array[q_front%q_capacity];
std::cout << temp << " was successfully dequeued.\n";
clear();
return;
}
//if normal
temp = q_array[q_front++%q_capacity];
//q_front++;
std::cout << temp << " was successfully dequeued.\n";
q_size--;
}

//6 Separate implementation of class queueArr from main.cpp
//7 create function prototype for display all

int main(){
//1 create an array of students with the names of your favorite classmates
std::string students [5] = {"Christian", "Thirdy", "MJ", "Maverick", "Niemo"};

//2 create a queue

std::queue<std::string> stud;
stud.push("Christian");
stud.push("Thirdy");
stud.push("MJ");
stud.push("Maverick");
stud.push("Niemo");
//3 loop structure to enqueue all students

//4 show front
//5 show end

//9 call display all
}

//8 define display all
Why do you:
1
2
3
#include <queue>

std::queue<std::string> stud;

Surely you should use the queue that you have created, the queueArr<std::string>?

Indentation helps reading; scopes are easier to see.
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
// ... something ...

//6 Separate implementation of class queueArr from main.cpp
// You do know the #include and what it does?

//7 create function prototype for display all
void displayall( /*???*/ );

int main(){
    //1 create an array of students with the names of your favorite classmates
    std::string students [5] = {"Christian", "Thirdy", "MJ", "Maverick", "Niemo"};

    //2 create a queue
    queueArr<std::string> stud( 42 );

    //3 loop structure to enqueue all students
    // enqueue from students into stud
    for ( auto s : students ) {
        // something
    }

    //4 show front
    //5 show end
    // You have member functions for these

    //9 call display all
    displayall( stud )
}

//8 define display all
void displayall( /*???*/ )
{
    // whatever it takes
}
Thank you man for pointing out the error! BTW we solved this one. We have really appreciated it!
If you have to implement your own queue - rather than using std::queue - why not base it around a std::list? It removes all the memory handling/array stuff - unless that was a requirement of the exercise?
enqueue/dequeue are over-complicated. Also there is a memory issue as allocated memory isn't deleted in the class destructor and a copy-constructor and copy assignment hasn't been provided. displayFront() and displayBack() also need to consider if the stack is empty. Consider:

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

template <typename T>
class queueArr {
private:
	T* q_array {};
	size_t q_capacity {}, q_size {}, q_front {}, q_back {};

public:
	queueArr(size_t newCapacity);
	bool isEmpty() const;
	bool isFull() const;
	size_t qSize() const;
	void displayFront() const;
	void displayBack() const;
	void enqueue(T newData);
	void dequeue();
	void clear();
	void displayAll() const;

	~queueArr() {
		delete[] q_array;
	}

	// These need to be provided if needed
	queueArr(const queueArr&) = delete;
	queueArr& operator=(const queueArr&) = delete;
};

template <typename T>
queueArr<T>::queueArr(size_t newCapacity) : q_capacity { newCapacity }, q_array { new T[newCapacity] } {}

template <typename T>
bool queueArr<T>::isEmpty() const {
	return q_size == 0;
}

template <typename T>
bool queueArr<T>::isFull() const {
	return q_size == q_capacity;
}

template <typename T>
size_t queueArr<T>::qSize() const {
	return q_size;
}

template <typename T>
void queueArr<T>::displayFront() const {
	if (isEmpty())
		std::cout << "Queue is empty\n";
	else
		std::cout << "Front: " << q_array[q_front] << '\n';
}

template <typename T>
void queueArr<T>::displayBack() const {
	if (isEmpty())
		std::cout << "Queue is empty\n";
	else
		std::cout << "Back: " << q_array[q_back ? (q_back - 1): q_capacity - 1] << '\n';
}

template <typename T>
void queueArr<T>::clear() {
	q_size = 0;
	q_front = 0;
	q_back = 0;
}

template <typename T>
void queueArr<T>::enqueue(T newData) {
	if (isFull())
		std::cout << "Queue is Full.\n";
	else {
		q_array[q_back] = newData;
		q_back = (q_back + 1) % q_capacity;
		++q_size;
		std::cout << newData << " was successfully enqueued.\n";
	}
}

template<typename T>
void queueArr<T>::dequeue() {
	if (isEmpty())
		std::cout << "Queue is Empty.\n";
	else {
		const auto temp { q_array[q_front] };

		q_front = (q_front + 1) % q_capacity;
		--q_size;
		std::cout << temp << " was successfully dequeued.\n";
	}
}

template<typename T>
void queueArr<T>::displayAll() const {
	//std::cout << "front: " << q_front << "  back: " << q_back << '\n';
	for (size_t i { q_front }, c {}; c < q_size; i = (i + 1) % q_capacity, ++c)
		std::cout << q_array[i] << '\n';

	std::cout << '\n';
}

int main() {
	//1 create an array of students with the names of your favourite classmates
	const std::string students[] { "Christian", "Thirdy", "MJ", "Maverick", "Niemo" };

	//2 create a queue
	queueArr<std::string> stud(5);

	//3 loop structure to enqueue all students
	for (const auto& s : students)
		stud.enqueue(s);

	//4 show front
	stud.displayFront();

	//5 show end
	stud.displayBack();

	//9 call display all
	stud.displayAll();

	stud.dequeue();
	stud.dequeue();

	stud.enqueue("qwer");
	stud.enqueue("asd");
	stud.dequeue();

	stud.displayAll();
}


which displays:


Christian was successfully enqueued.
Thirdy was successfully enqueued.
MJ was successfully enqueued.
Maverick was successfully enqueued.
Niemo was successfully enqueued.
Front: Christian
Back: Niemo
Christian
Thirdy
MJ
Maverick
Niemo

Christian was successfully dequeued.
Thirdy was successfully dequeued.
qwer was successfully enqueued.
asd was successfully enqueued.
MJ was successfully dequeued.
Maverick
Niemo
qwer
asd

Last edited on
Topic archived. No new replies allowed.