Removing items from queues...

I am almost done with this program, just I cannot remove items from the end of the queue successfully. The object of this program was to remove the first 5 and last 5 items from a queue and print the remaining. I've done all but remove the last 5. There is some code in there to do it, but it doesn't work correctly. I'd appreciate any help. Thank you.
public class Library3
public static void main (String[] args)
BookList3 books = new BookList3();
System.out.println (books);
// add twenty books by addLast
for (int x = 1; x < 21; x++)
books.addLast (new Book("Java " + x));
System.out.println("The first 5 books by \'getFirst()\' are:");
for (int x = 0; x < 5; x++)
System.out.println((Book) books.getFirst());
System.out.println("\nThe first 5 books by \'getLast()\' are:");
for (int x = 0; x < 5; x++)
System.out.println((Book) books.getLast());
System.out.println("\nThere are " + books.size() + " books remaining in the library ");
System.out.println (books);
public class BookList3
private BookNode first, last;
private int size = 0;
BookList3()
first = last = null;
public void add (Book newBook)
BookNode newNode = new BookNode (newBook);
if (first == null) // the queue is empty
first = last = newNode; // first element is also the last
else
last = last.next = newNode; // last link now points to current
public void enqueue (Book newBook)
add (newBook);
public void addLast(Book newBook)
enqueue (newBook);
size++;
public Book dequeue()
if (first == null)
return new Book ("No books to remove");
else
BookNode current = first;
first = first.next;
return current.book;
public Book getFirst()
size--;
return dequeue();
public Book pop()
if (last == null)
return new Book("No books to remove");
else
BookNode current = last;
last = last.previous;
return current.book;
public Book getLast()
size--;
return pop();
public int size()
return size;
public String toString ()
String result = "";
BookNode current = first;
while (current != null)
result += current.book.toString() + "\n";
current = current.next;
return result;
private class BookNode
public Book book;
public BookNode previous, next;
public BookNode (Book theBook)
book = theBook;
previous = next = null;
public class Book
private String title;
public Book (String newTitle)
title = newTitle;
public String toString ()
return title;
}

Just a couple of notes...
- your calls to getLast, getFirst, etc. seem a little redundant. Apart from incrementing the size variable, they just call their respective class to do the action. Both methods are already public, so why the need to call another method? Just throw the size++ or size-- into the implemented method. And your enqueue just calls add() and nothing else... just an unnecessary intermediate step.
- I'm not a big fan of the return new Book ("No books to remove") ... if there are no books, why not return null, and do a check for it on the calling method. Creating a new book to return the fact that there are no books to return just doesn't seem logical. What I guess you're doing is displaying the title of the book if there's a book to return. If not, then you return a book with the title "No books to remove" and it displays that instead. All fine and dandy, but mindsets like that will eventually dig you into a hole. A quick check for null can take care of it, no problem.
Okay, now back to your problem. In what way is it not "working correctly". What is your output onto your screen. The code to pop() (thought Pop was related to Stacks, not Queues, but whatever), enqueue() and dequeue() all look fine at first glance, but I haven't traced the code extensively. I'd say throw in a bunch (and I mean a ton) of println()'s in there, and see if you can figure out where your problem lies. I'd do it, but hey.. it's your project :) If you can't figure out what's going on, post an update on your problems and i'll take another look.

Similar Messages

  • Netflix problem using Firefox 9.0.1 (mac), unable to remove items from queue but I can from Safari. Anyone had this problem? Talked to Netflix they can remove items on their end.

    iMac OS 10.5.8, 2.66 GHz, 4gb memory. I've tried with adblock on and off. I use HTTPS Everywhere.
    Netflix suggested it might be a browser issue and to try Safari. I was able to remove items from queue using Safari.

    Addendum: The .jpg file, below, shows the netflix popup I get when trying to delete a video.

  • Pick queue item to work on, check "Also remove item from queue(s)" - where did it go?

    We recently upgraded from CRM 2011 to 2015.
    When users pick a queue item to work on, and check "Also remove item from Queue" - where does it go?  I cannot find a view that lists these items anywhere???

    Hi This depends on, which entity is in the queue. If it is an opportunity it could be found in opportunities If it is an email it could be found in activities etc. If you upgraded from crm2013, where could you find it before update to crm2015? Hope it
    helps. Gr.peb

  • How can we remove items from the VF04 list?

    We have a long list of items in VF04 that, for various reasons (some process related, some due to errors), are not to be billed. We have looked at applying filters to the list but there is nothing really suitable to hide these individual documents e.g. we could filter out some dates where none of the items on that date are to be billed but other dates have a mix of items we want to bill and others we don't.
    I have done a search of this forum but didn't find a previous topic with a quick and simple method to remove items from the list .
    Is there a method, other than billing them or sorting each issue individually, to permanently remove individual documents from the VF04 list?
    Thanks in advance for any help.
    David

    Hi,
    David,
    Download a list of Pending delivers doc form VF04.
    Paste the list in T.Code : VL09 (Delivery reversal) and reverse the delivery.
    Then go to T.Code: VL06G Select the list of deliveries that has been reversed, select/delete all un- wanted deliveries.
    This way you can remove all unwanted pending deliveries forn Billing due list (VF04).
    Thanks & Regards,
    R.Janakiraman

  • How can i update/Remove Items from AR Invoice using SDK?

    Hi All,
    I have 1 problem with update or remove item from sales order. here is the source code of mine.
    If inv.GetByKey(DocumentNumber) = True Then
                        inv.CardCode = cardcode
                        Dim ercode As Integer
                        Dim ind As Integer = 1
                        For Each drow As DataGridViewRow In gv.Rows
                            If gv.Rows.Count = ind Then Exit For
                            ercode = drow.Cells("No").Value
                            inv.Lines.SetCurrentLine(ercode)
                            'inv.Lines.ItemCode = drow.Cells("itemcode").Value
                            'inv.Lines.ItemDescription = drow.Cells("itemname").Value
                            inv.Lines.Quantity = drow.Cells("qty").Value
                            inv.Lines.Price = drow.Cells("price").Value
                            ind = ind + 1
                        Next
                        errorcode = inv.Update
                        If errorcode <> 0 Then
                            PublicVariable.oCompany.GetLastError(errorcode, errorsms)
                        Else
                            errorcode = 0
                        End If
                    Else
                        errorsms = "Not found Invoice Document Number"
    End If
    After update, it error "[INV1.Quantity][line:1],'Field cannot be updated (ODBC -1029)'"
    Does anybody know about this?
    Thanks
    TONY

    Hi $riniva$ Rachumallu,
    This is my mistake that not test manually in application.
    Thanks
    TONY

  • How do I remove items from the cloud without losing them permanently?

    My icloud storage is almost full. How do I remove items from the cloud and not lose them?

    What items?
    Purchased music, movies, TV shows, apps, and books do not use up your iCloud storage.
    See the link below for how to reduce the amount of storage you're using:
    http://support.apple.com/kb/HT4847

  • How do you remove items from the assets panel that are duplicated?

    How do you remove items from the assets panel that are duplicated?

    If you add an item to a slideshow, you'll usually see 2 entries for that image in the assets panel - one represents the thumbnail, and the other represents the larger 'hero' image.
    It sounds like you may have added the same image to your slideshow twice. You can select one of the hero images or thumbnail images in your slideshow and use the delete key to remove it. Then the extra 2 entries in the assets panel should disappear.

  • How do you remove items from the start up disc

    How do you remove items from the start up disc?

    Freeing Up Space on The Hard Drive
    You can remove data from your Home folder except for the /Home/Library/ folder.
    Visit The XLab FAQs and read the FAQ on freeing up space on your hard drive.
    Also see Freeing space on your Mac OS X startup disk.

  • How do I remove items from the dock?

    How do I remove items from the dock ?

    Single click and then drag it to the desktop. It will dissapear in a puff of smoke. You also may benefit by bookmarking and using:
    Find Out How Video tutorials

  • Can I remove items from the Services menu?

    I think the subject line says it all: can I remove items from the Services menu? I have a number of items in the Services menu that I never use and I would like to make the menu shorter to make it easier to access the items that I do use.

    There are a couple of things you could try. There's an application called Service Scrubber that is supposed to do this. But the web page says it's for 10.4, though it says it might work on 10.3
    Alternatively, you can edit the Info.plist or Info-macos.plist file in each application bundle to remove its service from the Services menu. This post at Mac OS X Hints has a pretty detailed description of how to do it. I tried this on my iMac running 10.3.9 and it worked just fine.
    Don't know about the Service Scrubber app, but if you edit the plist files, you'll need to log out and log back in before the changes take effect.
    Also, if you decide to edit the files by hand, I'd suggest just renaming NSServices by adding an "x" to the front or something, rather than deleting the item completely. That makes the changes easier to reverse.
    charlie

  • How can i remove items from list that have been deleted when i click on them it keeps showing empty

    how can i remove items from the list that have been deleted when i click on them it keeps showing folder empty

    Actually, Reader SHOULD keep showing documents that no longer exist, I disagree. It's no big deal, and people will quickly realise why they can't open the file. They open more files, the old ones move off.
    The REASON why it should not check is that checking whether a file exists can take a long time when things aren't good. For instance if a file server goes down, or a memory card is unplugged. That in turn would mean delays opening the File menu, and I've seen some software that can sit there for several minutes. That would really give people something of which to complain...

  • How can I remove items from the open a recent file menue.

    How can I remove items from the "Open a Recent File" menue. Many other applications allow the clearing of that file . Is this possible in Adobe Reader 10.1.2 ? If so how?

    See this previous topic: http://forums.adobe.com/message/3759596
    It is for Acrobat, but the registry entries are similar for Reader.

  • How can I add and remove items from the bookmarks toolbar?

    I want to remove items from the bookmarks toolbar and add ones that I use on a regular basis. How can I do this?

    See:
    *https://developer.mozilla.org/en/Chrome
    *http://en.wikipedia.org/wiki/User_interface_chrome#User_interface_and_interaction_design

  • Removing items from sheet doesn't change SQL

    Discoverer desktop 10.1.2.1
    When I create a sheet, run it, then remove items from the sheet, and then refresh it, the SQL doesn't change (items/columns that were removed remain in the select statement). They (columns) are removed from the resulting report, but it's throwing off my results (number of rows due to summary items). In other words, I had detail numbers, replace them with summary numbers, now it should be grouping by the detail items, but it's isn't. Basically, the tool isn't removing those items from the SQL.
    Has anyone seen this?
    Thanks in advance.

    I'll chime in on this one - we're seeing this same behavior as well. I can reproduce the problem at will like so:
    1) Create a new sheet.
    2) Pick a table, any table, that has a data point.
    3) To this new sheet, add the DETAIL of the data point, and one other attribute.
    4) View the results - they are good.
    5) Realize you made a misteak, go to Edit Sheet, remove the DETAIL and replace it with the SUM of that same data point.
    6) View the results, and become dismayed that they are not grouping at all.
    7) Click on View->SQL Inspector, and observe the detail item is still in the SQL (along with the SUM) despite having removed it in the sheet editor.
    8) Edit sheet - remove all items leaving absolutely everything blank (no selected items, no columns, no calculations, no sorts, nothing)
    9) Click on View->SQL Inspector, and observe the detail item is still in the SQL. That's wrong!
    If that's not a bug, I'm czar of all the Russias.
    // Discoverer Desktop 10.1.2.1
    // Discoverer Desktop Client 10.1.2.48.18
    // EUL Library 10.1.2.48.18
    // EUL 5.1.1.0.0.0

  • TS1702 how do i delete from the startup disk? I am trying to get Mountain Lion from the App store and it says that the purchase was not complete because i need 4.06 GB space to download OS x 10.8. and to remove items from my startup disk to increase space

    how do i delete from the startup disk? I am trying to get Mountain Lion from the App store and it says that the purchase was not complete because i need 4.06 GB space to download OS x 10.8. and to remove items from my startup disk to increase space. How?
    Thanks

    You can use iCloud with 10.7.5. You do not need to upgrade to Yosemite. You should provide a minimum of 15 GBs of free space or 10% of the hard drive's capacity, whichever is greater.
    Freeing Up Space on The Hard Drive
      1. See Lion/Mountain Lion/Mavericks' Storage Display.
      2. You can remove data from your Home folder except for the /Home/Library/ folder.
      3. Visit The XLab FAQs and read the FAQ on freeing up space on your hard drive.
      4. Also see Freeing space on your Mac OS X startup disk.
      5. See Where did my Disk Space go?.
      6. See The Storage Display.
    You must Empty the Trash in order to recover the space they occupied on the hard drive.
    You should consider replacing the drive with a larger one. Check out OWC for drives, tutorials, and toolkits.
    Try using OmniDiskSweeper 1.8 or GrandPerspective to search your drive for large files and where they are located.

Maybe you are looking for