• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Linked List Help

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alex,

there is no pointer in java .
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic