File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Doubly Linked List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubly Linked List" Watch "Doubly Linked List" New topic
Author

Doubly Linked List

Mark Nibert
Ranch Hand

Joined: Jul 01, 2009
Posts: 42
I am working on a program for a doubly linked list that allows for forward and backward links and the content of the node needs to be an object. I also need to allow for insert, delete, getPrev and getNext. I have the Introduction to Algorithms book (3rd edition) and I read up on linked lists. I think I kind of understand how to write it out but I am not sure. Does anyone know of any good sites or links that would help me complete this task? I am very new to programming and just have recently got back in to it after a break from work.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Google is the most used search engine....

http://www.google.co.in/search?q=doubly+linked+list+in+java&ie=utf-8&oe=utf-8&aq=t&client=firefox-a&rlz=1R1GGLL_en-GB___IN415

http://leepoint.net/notes-java/data/collections/lists/simple-linked-list.html

http://www.java-tips.org/java-se-tips/java.lang/linked-list-implementation-in-java.html

Hope that helps


Java Experience
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

What exactly are you not sure about? Did you write some code already? Did it work, or not? You learn programming by experimenting - write code, see if it works, and if it doesn't then learn how to fix it.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mark Nibert
Ranch Hand

Joined: Jul 01, 2009
Posts: 42
I have been using google, I just dont know what I am looking for and it is making it a lot harder. I appreciate the response and will look at the links you sent...I am not a good programmer at all and this is tough for me.
Mark Nibert
Ranch Hand

Joined: Jul 01, 2009
Posts: 42
Jesper de Jong wrote:What exactly are you not sure about? Did you write some code already? Did it work, or not? You learn programming by experimenting - write code, see if it works, and if it doesn't then learn how to fix it.


I am a terrible programmer, I barely have the basics down and I am being tasked w/ this program and I am just having trouble getting it started. I understand what a linked list is and can understand the logic it is just implementing it in java that is harder for me.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Oracle's Java Tutorials is a good place to start learning Java. Especially look at the Covering the Basics trails.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Mark Nibert wrote:I understand what a linked list is and can understand the logic it is just implementing it in java that is harder for me.

Then here's a good trick: first right it down in pseudo code, and then try to translate that, in small pieces, to Java.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mark Nibert
Ranch Hand

Joined: Jul 01, 2009
Posts: 42
I know that for a doubly linked list it is going to allow for forward and backward searching of a list of objects instead of only going in one direction like a singly linked list. I know that it will have a head and tail and I will need to create a node pointer for prev and next. If the prev node does not point to anything that would be the head and if the next node is empty and does not point to anything then it is the tail. This is the part that I am not sure on how to set up in Java. Do I need to make the nodes of type Object?


Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

Hi Mark, welcome to JavaRanch!

First of all, yes, Link class that you put by default extends Object class, everything in Java except primitive types extend Object class.

However for your given exercise, I would recommend you not to look anywhere for ready code, but try to code everything yourself using pseudocodes from intro to alg. book. I remember having the exercise with doubly linked lists myself, and it was a good experience to be able to do it myself, I learned a lot.

As of hint, think of following: first you would put your Link list to data structures, that would be Link objects, referencing each other. Now think what will happen with every link, and more precisely with connections of those Links, when there is a delete and insert. Try to draw Links and connections between them with pencil on paper, and then erase and reconnect nodes, this will help!


Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
Mark Nibert
Ranch Hand

Joined: Jul 01, 2009
Posts: 42
I will try that and see what I come up with.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

You may be able to use one single object for the head and tail. The next element is the first element, the previous element is the last element. You'll need to differentiate this element from the rest. I believe LinkedList does this by storing a reference to the head and then using == to compare nodes against the head node.
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 570
    
    7

At this risk of possibly adding more confusion.... you could make the code much easier by making a circular linked list where you start our with a special node that points to itself (both next and prev). You then dont need to worry about all of the boundary conditions in your add and delete methods (ie: adding at head, adding at tail, adding at head and tail all resolve to the nominal case - adding between two nodes). --- same with delete.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubly Linked List