Join 132,354 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,141 people online right now. Registration is fast and FREE... Join Now!
here my problem: example: the queue is [10,12,11,10,5]
after delete 10 is [12,11,5]
what i understand is first need to check all the elements in the queue then need to compare if same with the number then dequeue it
can anyone show me the better way to do it,
CODE
public void delete(T item)//deletes all the occurences of a given and order of the remaining elements is unchanged {
for(int i=0;i<count;i++) { if(elements[i]==item)//example if elements[0]=90 equals to 0 { temp[i]=item;//send to
} }
}
this my full coding:
CODE
public class ArrayQueue<T> {
private T[] temp; private T[]elements;//array in generic private int front;//first element or front of the queue private int back;//last element or back of the queue private int capacity;//capacity of the queue private int count;//indicates number of elements currently stored in the queue
public ArrayQueue(int size) { capacity = size; count = 0; back=count; front=count; elements =(T []) new Object[size];//array empty temp = (T[]) new Object[size]; }
public boolean isEmpty()//returns true if the queue is empty or false { return count==0;//means its true }
public boolean isFull() { return count == capacity;//count on index same as the length of the queue }
public void enqueue(T item)//add elements to the queue { back =(back+1)%capacity;//example back=(0+1)%10=1 elements[back]=item;
//elements[0]=0 //item=elements[count]; if(count == capacity) { System.out.println("Queue is full");
public T dequeue() { front = (front +1)%capacity;//example front=(0+1)%10=1
if(isEmpty()) { throw new NoSuchElementException("Dequeue:Queue is empty"); //System.out.println("Pop:Stack is empty"); //return null; } else return elements[--count]=elements[--front]; }
public T poll()//peek the first element { if(isEmpty()) { throw new NoSuchElementException("Poll:Queue is empty"); //System.out.println("Peek:Stack is empty"); //return null; } else return elements[front]; }
public void delete(T item)//deletes all the occurences of a given and order of the remaining elements is unchanged {
for(int i=0;i<count;i++) { if(elements[i]==item)//if elements[0]=90 equals to 0 { temp[i]=item;
} }
}
public String toString()//return the string element { String s = "["; for(int i = 0; i <count; i++) { if(i != 0) s += ", "; s = s + elements[i];// [value1,value2,....] }
s +="]"; return s; }
public static void main(String[] args) { int capacity=10; ArrayQueue<Integer> ARRAYQ = new ArrayQueue<Integer>(capacity);
int i; for(i=0;i<100;i+=10) { ARRAYQ.enqueue(i); }
System.out.println("Queue: "+ARRAYQ); ARRAYQ.dequeue(); System.out.println("Dequeue: "+ARRAYQ); System.out.println(ARRAYQ.poll()); ARRAYQ.delete(80); System.out.println(ARRAYQ); /*ARRAYQ.enqueue(70); System.out.println("Remove 70 from the queue:"+ARRAYQ); System.out.println("Adding 100,200,300,400,500");
I dunno exactly what you wanted but this method will delete all instances of a given element and reduce the length of the queue but leave the order of the remaining elements unchanged.
CODE
public void delete(T item) {
int t = 0; for (int i=0; i < elements.length; i++) { elements[i - t] = elements[i]; if(elements[i] == item) { t++; } } for (int j = 0; j < t; j++) elemets[elements.length - (j + 1)] = null; }
To show you what happens in a few (6) steps I'll show you what happens to the queue [1 2 2 5 2 6] if you choose to remove the number 2.
1. 1 2 2 5 2 6 //Nothing actually happens this iteration. (1 is exchanged for 1) 2. 1 2 2 5 2 6 //Nothing happens to the queue but t is incremented. (2 is exchanged for 2) 3. 1 2 2 5 2 6 //Still nothing visually happens to the queue but the second 2 is put in the first 2's place and t is incremented 4. 1 5 2 5 2 6 //Finally the 5 is put in the first 2's position. 5. 1 5 2 5 2 6 //Again nothing visually happens to the queue but the third 2 is put in the second 2's place and t is incremented 6. 1 5 6 5 2 6 //The 6 is put in the second 2's place and the loop is concluded
Now you have to overwrite the posts in the array that are no longer used with 'null'. Starting from the end.
This post has been edited by Gloin: 23 Sep, 2008 - 12:35 PM
there some logical error with my program can someone fix it
CODE
public class ArrayQueue<T> {
private T[]elements;//array in generic private int front;//first element or front of the queue private int back;//last element or back of the queue private int capacity;//capacity of the queue private int count;//indicates number of elements currently stored in the queue
public ArrayQueue(int size) { capacity = size; count = 0; back=count; front=count; elements =(T []) new Object[size];//array empty
}
public boolean isEmpty()//returns true if the queue is empty or false { return count==0;//means its true }
public boolean isFull() { return count == capacity;//count on index same as the length of the queue }
public void enqueue(T item)//add elements to the queue { back =(back)%capacity;//example back=(0+1)%10=1 elements[back]=item;
if(count == capacity) { System.out.println("Queue is full");
public T dequeue()//method that removes and returns an elements from the queue { front = (front +1)%capacity;//example front=(0+1)%10=1
if(isEmpty()) { throw new NoSuchElementException("Dequeue:Queue is empty"); //System.out.println("Pop:Stack is empty"); //return null; } else return elements[--count]=elements[--front]; }
public T poll()//peek the first element { if(isEmpty()) { throw new NoSuchElementException("Poll:Queue is empty"); //System.out.println("Peek:Stack is empty"); //return null; } else return elements[front]; }
public void delete(T item) {
int t = 0; for (int i=0; i < elements.length; i++) { elements[i - t] = elements[i];// if(elements[i] == item) { t++; } } for (int j = 0; j < t; j++) elements[elements.length - (j + 1)] = null; }
/*public void delete(T item)//deletes all the occurences of a given and order of the remaining elements is unchanged {
for(int i=0;i<count;i++) { if(elements[i]==item)//if elements[0]=90 equals to 0 { temp[i]=item;
} }
}*/
public String toString()//return the string element { String s = "["; /*for(int i = 0; i <count; i++) { if(i != 0) s += ", "; s = s + elements[i];// [value1,value2,....] } */ for(int i=0; i<count; i++) {
s = s + elements[i];// [value1,value2,....] if(i<count-1) { s +=","; } } s +="]"; return s; }
public static void main(String[] args) { int capacity=10; ArrayQueue<Integer> ARRAYQ = new ArrayQueue<Integer>(capacity);
int i; for(i=0;i<100;i+=10) { ARRAYQ.enqueue(i); }
System.out.println("Queue: "+ARRAYQ);
for(int j=3;j<10;j++)//remove the elements { ARRAYQ.dequeue(); }
System.out.println("The first three elements of the queue: "+ARRAYQ); ARRAYQ.enqueue(70);//add 70 to the queue System.out.println("Queue: "+ARRAYQ); System.out.println("First element of the queue:"+ARRAYQ.poll()); ARRAYQ.delete(70); System.out.println("Remove 70 from the queue:"+ARRAYQ); System.out.println("Adding 100,200,300,400,500");
Inserting 0 at [0] Inserting 10 at [1] Inserting 20 at [2] Inserting 30 at [3] Inserting 40 at [4] Inserting 50 at [5] Inserting 60 at [6] Inserting 70 at [7] Inserting 80 at [8] Inserting 90 at [9] Queue: [0,10,20,30,40,50,60,70,80,90] The first three elements of the queue: [0,10,20] Inserting 70 at [3] Queue: [70,10,20,70]---------->this is incorrect suppose like this [0,10,20,70] First element of the queue:70-----> the first elements is 0 Remove 70 from the queue:[10,20,0,0]------> this also incorrect Adding 100,200,300,400,500 Inserting 100 at [4] Inserting 200 at [5] Inserting 300 at [6] Inserting 400 at [7] Inserting 500 at [8] Queue: [10,100,200,300,400,500,300,400,500]---->incorrect
Your problem is you have a delete in a queue. In a queue you have enqueue and dequeue and maybe a count and a peek. Having a delete fundamentally breaks the design.
Your results are wrong, but not in the way you expect. Here's my analysis of your code:
CODE
Queue: [0,10,20,30,40,50,60,70,80,90] // dequeue 7 items, results should be Queue: [70,80,90] The first three elements of the queue: [0,10,20] <-- this is incorrect Inserting 70 at [3] Queue: [70,10,20,70]---------->this is incorrect suppose like this [70,80,90,70]
Your own code is confusing you. You don't need a front and back, you just need to understand what front and back means. e.g.
java
class ArrayQueue<T> { private T[]elements; //array in generic private int capacity; //capacity of the queue private int count; //indicates number of elements currently stored in the queue // elements[0] is front, elements[count-1] is back
Your results are wrong, but not in the way you expect. Here's my analysis of your code:
CODE
Queue: [0,10,20,30,40,50,60,70,80,90] // dequeue 7 items, results should be Queue: [70,80,90] The first three elements of the queue: [0,10,20] <-- this is incorrect Inserting 70 at [3] Queue: [70,10,20,70]---------->this is incorrect suppose like this [70,80,90,70]
Your own code is confusing you. You don't need a front and back, you just need to understand what front and back means. e.g.
java
class ArrayQueue<T> { private T[]elements; //array in generic private int capacity; //capacity of the queue private int count; //indicates number of elements currently stored in the queue // elements[0] is front, elements[count-1] is back
i tried it but it won't work coz if i don't use the front and back...i can't store the element....i use the front and back to store the elements so it can be call back....can u show me more how it works
I suppose there's little point in explaining. Please try to understand what's going on.
This is a complete example:
java
class ArrayQueue<T> { private T[]elements; //array in generic private int capacity; //capacity of the queue private int count; //indicates number of elements currently stored in the queue // elements[0] is front, elements[count-1] is back
public ArrayQueue(int capacity) { this.capacity = capacity; count = 0; elements = (T[])new Object[capacity]; }
public int getSize() { return count; } public boolean isEmpty() { return count==0; } public boolean isFull() { return count == capacity; }
public void enqueue(T item) { if(isFull()) { System.out.println("Queue is full"); return; } System.out.println("Inserting " + item + " at [" + count + "]"); elements[count++]=item; }
public T poll() { if(isEmpty()) { throw new NoSuchElementException("Poll:Queue is empty"); } return elements[0]; }
public T dequeue() { T item = poll(); count--; // shift elements up for(int i=0; i<count; i++) { elements[i] = elements[i + 1]; } return item; }
public String toString() { String s = "["; for(int i = 0; i <count; i++) { if (i>0) { s +=","; } s = s + elements[i]; } return s + "]"; } }
I suppose there's little point in explaining. Please try to understand what's going on.
This is a complete example:
java
class ArrayQueue<T> { private T[]elements; //array in generic private int capacity; //capacity of the queue private int count; //indicates number of elements currently stored in the queue // elements[0] is front, elements[count-1] is back
public ArrayQueue(int capacity) { this.capacity = capacity; count = 0; elements = (T[])new Object[capacity]; }
public int getSize() { return count; } public boolean isEmpty() { return count==0; } public boolean isFull() { return count == capacity; }
public void enqueue(T item) { if(isFull()) { System.out.println("Queue is full"); return; } System.out.println("Inserting " + item + " at [" + count + "]"); elements[count++]=item; }
public T poll() { if(isEmpty()) { throw new NoSuchElementException("Poll:Queue is empty"); } return elements[0]; }
public T dequeue() { T item = poll(); count--; // shift elements up for(int i=0; i<count; i++) { elements[i] = elements[i + 1]; } return item; }
public String toString() { String s = "["; for(int i = 0; i <count; i++) { if (i>0) { s +=","; } s = s + elements[i]; } return s + "]"; } }
how about the delete method for example i wanna delete this no [0,2,3,2] ->delete 2 became [0,3]
do i just need to use dequeue instead of delete method
how about the delete method for example i wanna delete this no [0,2,3,2] ->delete 2 became [0,3]
Well there is a process to do that
[0,2,3,2] -> dequeue first element and compare it with the element to be deleted. [2,3,2] -> element does not match with to be deleted number, enqueue it again. [2,3,2,0] -> dequeue first element and compare it with the element to be deleted. [3,2,0] -> element matched, proceed. [3,2,0] -> dequeue first element and compare it with the element to be deleted. [2,0]-> element doe not match with to be deleted number, enqueue it again. [2,0,3] -> dequeue first element and compare it with the element to be deleted. [0,3] ->element matched, end of queue reached. Finish.