circular queue

Hi there :)

I am working on circular array, but I don't think I understand it fully (yet)
I have created this code but I don't think it is working as it should, specially part that needs to remove item from queue.
As i understand this should work as FIFO (First in First Out). I am not sure how to remove items from array. At the moment I am replacing it with different number and in my code it just work same way as ADD to array but it replace number from last element (if that makes sense??) where as when I add new element it adds from Top to bottom. (I might have to switch names ;) top and bottom )



is my understanding correct that variables front and back decide if array is empty?

I am not looking for a fix ;) more advise where are problems with my code :)

thank you

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
// Chapter7-ex9.cpp : Defines the entry point for the console application.
//
#include <iostream>

class Queue
{
private:
	static const int MAX = 10; // non-standard syntax
	int number[MAX]{ 0 }; // queue array of integers
	int front;
	int back;
public:
	Queue() //constructor
	{
		front = 9;
		back = 0;
	}
	void put(Queue& queue1_) // put member on array
	{
		std::cout << "Number: ";
		back = (back % MAX);
		std::cin >> number[back];
		back++;
	}
	void get(Queue& queue_) // take number off array
	{
		std::cout << "Number: ";
		front = (front % MAX);
		std::cin >> number[front];
		front--;
	}
	bool fullQueue(Queue queue1_) // check if the queue is full
	{
		if (front == (back % MAX)-1) 
		{
			std::cout << "Full queue!!" << std::endl;
			return 1;
		}
		else return 0;
	}
	void showQueue()
	{
		for (int i = 0; i < MAX; i++)
		{
			std::cout << number[i] << std::endl;
		}
	}
};


int main()
{
	Queue queue1;

	while (!(queue1.fullQueue(queue1)))
	{
		std::cout << "Do you want to Add or Remove data? (A)dd (R)remove: ";
		char addRem = 'o';
		std::cin >> addRem;
		if (addRem == 'a' || addRem == 'A')
		{
			queue1.put(queue1);
			queue1.showQueue();
			
		}
		else
		{
			queue1.get(queue1);
			queue1.showQueue();
		}
	}

	system("pause");
	return 0;
}


1. Try to not do I/O in your functions (expect the ones whose purpose is I/O, like `showQueue()')
a. If you are not going to use a parameter, then don't ask for it.

1
2
3
4
5
6
	void put(int item) // put member on array
	{
		this->back = (this->back % Queue::MAX);
		this->number[this->back] = item;
		this->back++;
	}
(this is not needed, just wanted to remark that you are working on the caller object)


1
2
3
	void showQueue()
	{
		for (int i = 0; i < MAX; i++)
wrong, show the queue from front till back.
that would also solve the remove part, you don't need to replace the item, just adjust the indices to leave it outsite.


> void get(Queue& queue_) // take number off array
http://www.cplusplus.com/reference/queue/queue/
¿that should be `.pop()', `.front()' or a mix?
Hi @ne555

thank you for that!!! I am sorry about late reply I was in work and now I am at my c++ evening course I will read/re read and re read again :) and try to sort my code :)

void get function is to (as per my book) to get away item from array. but I think pop is more readable.

Topic archived. No new replies allowed.