• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Linked List Help

 
Alex Ba
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,

there is no pointer in java .
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is nothing like pointer in java. everything is in terms of reference & object.
reference variable denote object. Pointer is nowhere in java.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Alex Ba
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijatha Kumara has shown you how to code "previous".
 
Alex Ba
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhhhh

So, I use the IntNode class is being used by the intcoll class, but not the main?
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic