File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Adding an element to the end of a linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding an element to the end of a linked list" Watch "Adding an element to the end of a linked list" New topic
Author

Adding an element to the end of a linked list

Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
Hello all, I am writing a program that requires me to add and view items from both the left and right (front and back) sides of a linked list. I have the left side figured out, but the right side is giving me trouble. I'm not really sure what to set my rightEnd equal to in the constructor.
Thanks for the help.
Here is my code:




public abstract class LinkedDeque implements DequeInterface {
private DNode leftEnd;
private DNode rightEnd;
private int size;

LinkedDeque()
{
leftEnd = new DNode(null);
rightEnd = new DNode(leftEnd);

size = 0;

}
// add to left end
public void addLeft(Disk obj)
{
leftEnd = new DNode(obj);
size++;
}
public void addRight(Disk obj)
{

}
public int getSize()
{
return size;
}

public DNode peekLeft()
{
if(size ==0)
{
throw new IllegalArgumentException("List is empty!");
}

return leftEnd;
}
public DNode peekRight()
{
if(size ==0)
{
throw new IllegalArgumentException("List is empty!");
}
return rightEnd;

}
}
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
The "right end" would point to the last node in the list. If the list is empty, then null I suppose. If there is one node in the list but head and tail would point to the same node

You don't need to add a tail to the linked list to insert at the end. With a tail node, then you have 2 references to mess around with. Unless the list gets very large the gain in having quick access to the end of the list is very minor.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

I would also say like David, if the linked list is constructed , left end and right end should point to the same element. But wouldn' the size then be 1?
eg constructor like

But I'm not sure about this, as I don't know what new DNode(null); will do.



David also cites that
"C++ is history repeated as tragedy. Java is history repeated as farce." -Scott McKay
You can put these two together as:
"The history of all hitherto existing society is the history of class struggles." - Karl Marx, Communist Manifesto


Yours,
Bu.


all events occur in real time
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
With a dummy head node, both would initially point to the same node, but the size would still be 0.

I hope that quote doesn't offend anyone, I didn't add it to my sig for the purpose of offending anyone or ripping on Java. I just find it amusing that people can turn langauge preference into something akin to a holy war. Sure, we all have languages we don't like but few languages are so flawed to the point of uselessness.

The quotes came from this page, especially amusing are the opinions of the creator of Eiffel, Bertrand Meyer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding an element to the end of a linked list