aspose file tools*
The moose likes Beginning Java and the fly likes Linked List Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Linked List Help" Watch "Linked List Help" New topic
Author

Linked List Help

Alex Ba
Ranch Hand

Joined: Oct 14, 2009
Posts: 35
So I'm in a college java class, and was recently given a lab where I have to create a linked list. I understand the general concept, there is a value that has a pointer to the next item in the list, and possibly a pointer to the previous. The first is the head, the last is the tail, and if there is nothing after/before something, that value is null. My question is, how do pointers work? As in, how does one code a pointer to point to the next node. Is it some other variable I'm overlooking? Or am I missing something?

Not looking to steal code, just want some direction.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Alex,

there is no pointer in java .
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10522
    
    9

Alex,

Welcome to the Ranch.
In Java there are no pointers, which you, as a developer can manipulate and use. However you can use object references.

What you will need to do is design a class which will
1) Store objects which you pump in
2) Maintain the required sequence
3) Will be able to figure out and retrieve if required, the head and tail of any given object.



[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Alex Ba wrote:... As in, how does one code a pointer to point to the next node. Is it some other variable I'm overlooking? Or am I missing something?

You can have a "Node" class which represent a node in the list. So that Node class itself need to have two references of type Node for "next" and "previous" which, you can assign the relevent nodes. Something like this....


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10522
    
    9

Seetharaman,
I am sorry but I have edited your post.

Figuring out things is the best way to learn. Though you meant well in providing the link, it wouldn't have helped Alex to learn.
Also,
Not looking to steal code, just want some direction.

pankaj vijay
Ranch Hand

Joined: Apr 01, 2008
Posts: 75
there is nothing like pointer in java. everything is in terms of reference & object.
reference variable denote object. Pointer is nowhere in java.


Pankaj Vijay (SCJP, SCBCD)
Learn Core Java,Learn Servlet Jsp, SCJP Questions,Struts Tutorial
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Maneesh Godbole wrote:
Figuring out things is the best way to learn. Though you meant well in providing the link, it wouldn't have helped Alex to learn.
Also,
Not looking to steal code, just want some direction.



No worries . it is true
Alex Ba
Ranch Hand

Joined: Oct 14, 2009
Posts: 35
So my professor gave us two sets of code, and we have to edit both to make the program class. She gave us an IntNode class and an intcoll class.

The IntNode:



That class just needs to be the IntNode method.

Then she gave us:




To start, I guess you could say that I don't understand what is supposed to go in the parameters for the intnode part. AKA, the IntNode(int i, IntNode n). I understand what i is supposed to be, but what would go for n?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40


Alex Ba wrote:
To start, I guess you could say that I don't understand what is supposed to go in the parameters for the intnode part. AKA, the IntNode(int i, IntNode n). I understand what i is supposed to be, but what would go for n?


From your first post....



Alex Ba wrote:and was recently given a lab where I have to create a linked list. I understand the general concept, there is a value that has a pointer to the next item in the list, and possibly a pointer to the previous.... My question is, how do pointers work? As in, how does one code a pointer to point to the next node.



Basically, n is the "pointer to the next node".... BTW, I don't understand the point to the assignment. Didn't you teacher just do the assignment for you?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Alex Ba
Ranch Hand

Joined: Oct 14, 2009
Posts: 35
I need to add in a previous part to the IntNode class, and I need to code the main to make it run.. But still, I have no idea how to code a next or a previous pointer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Vijatha Kumara has shown you how to code "previous".
Alex Ba
Ranch Hand

Joined: Oct 14, 2009
Posts: 35
If that's the way to code previous, I'm even more lost than I thought.



I have that much, and apparently I'm right with that. From there, I'm completely lost.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

Alex Ba wrote:
I have that much, and apparently I'm right with that. From there, I'm completely lost.


I'm not sure what you mean by completely lost -- what else does there need to be? IntNode is just a simple data structure, what pieces are you thinking is missing?


Now... as for intcoll class, that is the implementation. That is where all the code is, that creates the nodes, and connect it up correctely, to form the link list. You have to change that implementation to now connect the previous node correctly too.

Henry
Alex Ba
Ranch Hand

Joined: Oct 14, 2009
Posts: 35
Ahhhhhh

So, I use the IntNode class is being used by the intcoll class, but not the main?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

Alex Ba wrote:So, I use the IntNode class is being used by the intcoll class, but not the main?


Well, that is completely up to you -- you are, after all, the designer of the linked list class. But yea, it would be nice to use a linked list class, without knowing what the internal node structure looks like.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linked List Help