Solved

creating simple queues

Posted on 2002-04-15
6
202 Views
Last Modified: 2010-03-31
How do i remove an item from the middle of a queue?

i am a newbie so please explain how this can be done and examples of simple queues would be excellent!

thanks

dd
0
Comment
Question by:digitaldestruction66
6 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 100 total points
ID: 6941548
try this class:

import java.util.*;

public class Queue {
  private Vector items;
  public Queue() {
      items = new Vector();
  }

  public void enqueue(Object o) {
      items.add(items.size(), o);
  }

  public Object dequeue(Object o) {
      return items.remove(0);
  }

  public void removeAt(int pos) {
      items.remove(pos);
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6941716
public void enqueue(Object o) {
     items.addElement(o);
 }

Works just as well
0
 
LVL 3

Expert Comment

by:jerelw
ID: 6942715
Iterate through your linked list, and when you find the item you want to remove set the previous item's next to the next item.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:mrowka
ID: 6944658
you can use the class LinkedList directly for modelling
queues, have a look at

http://java.sun.com/docs/books/tutorial/collections/implementations/general.html
and
http://java.sun.com/products/jdk/1.2/docs/api/java/util/LinkedList.html

for a more in depth explanation of collections:
http://www.mindview.net/Books/TIJ/
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7940699
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: Venci75
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:modulo
ID: 7990872
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now