Remove 2 elements from a queue

I have a Lqueue queue from which I have to remove all elements (of type Item) with a height> 92 and also I have to remove all elements that have a greater height than the previous element (it does not apply to the first element). for example if I have:

(United States,127.2) (United States,120.1) (United States,100.2)
(United States,98.8 ) (United States,91.1) (United States,91.5) (United
States,91.6) (United States,126.7) (United States,55.5) (United States,50.6) (United States,48.9) (United States,41.2) (United States,39.1) (United
States,49.4) (United States,46.3) (United States,43.8 )

My output should be :

(United States,91.1) (United States,91.5) (United States,91.6)
(United States,126.7) (United States,55.5) (United States,50.6) (United
States,48.9) (United States,41.2) (United States,39.1) (United States,49.4) (United States,46.3) (United States,43.8 )


For the first part I tried made

for (int i = 0; i < Q1.length(); i++) {

if (temp.getcode() < '92') {

Q1.dequeue();

}
}

But in this case I made the opposite I should. The program print some ( and not all ) of the elements that it should cancel. The second part I have no idea how to do it because i can only use enqueue (inserting an element, but from the head), dequeue (removing an element, from the queue), length (indicating the length of the queue), frontValue (indicating the first element of the queue), clear to clear all elements present in the queue.
Last edited on
You haven't shown remotely enough code and your Lqueue (linked-list queue) seems a bit of a weird data structure; we have no details of it or how to test it.

In the minimal loop you have shown:
- what is the variable temp?
- why do you suppose dequeue() (which presumably removes something from the front of the queue) should act on the element you are trying to get rid of?
- for something based on a linked list it is unusual to use an index-based loop counter ... especially one terminating at a value that keeps changing, as you would not then cover all elements of the data structure.

It may be simpler to construct a new Lqueue from the elements that you wish to keep. However, you haven't given enough code to provide much of an answer to your question.


BTW. Get used to using code tags.
Last edited on
Unfortunately I can’t post all the code because it’s from a big exam theme from 4 years ago and the first step was about to open a text file and create a queue of items, too big to post it. I already made these 2 points and I need help for this third.

I have this queue of items. Every item into the queue have a name and an height
(United States,127.2) (United States,120.1) (United States,100.2)
(United States,98.8 ) (United States,91.1) (United States,91.5) (United
States,91.6) (United States,126.7) (United States,55.5) (United States,50.6) (United States,48.9) (United States,41.2) (United States,39.1) (United
States,49.4) (United States,46.3) (United States,43.8 )

My output should be :

That’s because I dequeue 2 elements from the queue i and j. If i <92 I have to remove it from the queue and if j > i I also have to remove j from the queue. This because i and j rapresents the height of the items. I have to analyse 2 items from the queue every time , If the height of the second item is bigger than the height of the first item , I have to cancel j.

(United States,91.1) (United States,91.5) (United States,91.6)
(United States,126.7) (United States,55.5) (United States,50.6) (United
States,48.9) (United States,41.2) (United States,39.1) (United States,49.4) (United States,46.3) (United States,43.8 )


The operations I can do it are enqueue ( insert a element from the top ) , dequeue ( remove an element from the bottom ) and I have length that’s the length of the queue. I hope , this time , to be more specific. Sorry for my terrible English, I’m Italian
Last edited on
This because i and j rapresents the height of the items.

Not if i is a loop variable it isn't.


The simplest way using only enqueue, dequeue, frontvalue and length is to traverse your original loop from the front (using frontvalue and dequeue) and construct a new queue with the desired elements (using enqueue).
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
int l = Q1.length();
	int	t = 0;
	int	min = 92;
	Item i;
	Item j;
	
				 i = Q1.dequeue();
				 j = Q1.dequeue();

		while (t < l) {
			if (i.getcodice() > min && j.getcodice() > min) {
				i = Q1.dequeue();
				j = Q1.dequeue();
				l = l - 2;
				t = t + 2;
			}


			if (i.getcodice() > min && j.getcodice() < i.getcodice()) {
				Q1.enqueue(j);
				i = j;
				j = Q1.dequeue();
				l = l - 1;
				t = t - 1;
		}

			if (i.getcodice() < min && j.getcodice() > i.getcodice()) {
				Q1.enqueue(i);
				i = j;
					j = Q1.dequeue();
					l = l - 1;
					t = t - 1;
			}

			else {
				Q1.enqueue(i);
				Q1.enqueue(j);
				i = Q1.dequeue();
				j = Q1.dequeue();
				t = t + 2;
			}


		}




  Lqueueprint(Q1);


This is what I wrote , the output is correct only partially. This print all the correct items but also some items that are not correct. I have to use only one queue. I have to insert some loop at the end of if but with break I obtained the same things


Last edited on
Topic archived. No new replies allowed.