wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Static Doubly Linked Lists.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Doubly Linked Lists...." Watch "Static Doubly Linked Lists...." New topic
Author

Static Doubly Linked Lists....

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok here is the assignment description...

This program is basically the same program as 3L, but the implementation will be static rather than dynamic. In this case, all the linked lists will be taken from a single array of nodes defined as static member data.
Dll.StaticCls.cpp should be of considerable use. Your static member data
array should allocate 60 nodes. Convert your existing functions to deal with the nodes in the array. Add methods deleteNode and newNode that mirror delete and new. You should initially place all the nodes in the array in the "freelist", which may be linked how ever you please.


ok, now in this, so i would have a static member data array like the following...



all i really want to know is, all im doing is allocating memory in an array, for my nodes, instead of letting the OS do it for me right?

so everytime i call newNode, it just takes the first node reference at
theNodes[freelist] and assign that spot in the array to what ever node I
create, then freelist = theNodes[freelist].next;

where freelist points to the first node reference in the "theNodes" array;

but even though im creating my nodes in the array in order? can I still
modify next and prev to what ever node I want, or do i need to create the node and insert it in order into the "theNodes" array?

thanks,
justin


You down with OOP? Yeah you know me!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Justin Fox:
...all i really want to know is, all im doing is allocating memory in an array, for my nodes, instead of letting the OS do it for me right?

Yes, new Node[60] creates an array object containing 60 Node references -- all initialized to null at this stage. In contrast to something that can be dynamically resized like a List, this array's size is now set and won't be changing. (Note: I think you mean the VM instead of the OS.)

Originally posted by Justin Fox:
...can I still modify next and prev to what ever node I want, or do i need to create the node and insert it in order into the "theNodes" array?

The array is just a place to store references to Node instances. The "next" and "prev" instance variables in a Node have nothing to do with that Node's position in the array. For example, the Node at index 3 might have a "next" value of 11 and a "prev" value of 49. The key here is that you can randomly access array elements, so jumping from 49 to 3 to 11 should be no problem.
[ July 15, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
thanks dude!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok in the following program, i need to initially link the nodes in the array theNodes[], but how do i go about doing that when you cant do it outside a constructor, and a non-static method can't call a static method..

code:


[ July 17, 2006: Message edited by: Justin Fox ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Justin Fox:
...a non-static method can't call a static method...



A non-static (instance) member cannot be accessed from a static context, but the reverse is not true.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, i have the static memeber data Node[] theNodes = new Node[60];

i need to modify the next pointer to be the next index of the current.

for example:

theNodes[0].nextNum = 1;

so that the node at index 0 points to 1 and 1 points to 2 and so on

but the Node[] is static, so i can't

say


i get a static/non-static conflict....

plz help,
Justin
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

It would help if you provide some context (I don't see these lines anywhere in the 356 lines of code you posted earlier), along with the exact error message.

My guess is that these lines are inside a static method, and you are getting an error like...

In the context of your original code, Node is an inner class of StaticDDLL. An inner class is simply a nested class that is not static. Because they're not static, instances of inner classes are tied to an instance of the enclosing class. And in this context, when you say "new Node()," the implication is "this.new Node()," where "this" references an instance of the enclosing class. But in a static context, there is no "this" reference, which should explain the error message.
[ July 17, 2006: Message edited by: marc weber ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok in the very beginning of my StaticDDLL class, i import Scanner and io

like so:


im getting this error,



umm i'm most definately positive that you're supposed to capitalize the
's' on scanner.

help plz,
Justin
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3539
    
  15
Are you using JDK1.5 ? Scanner didn't exist in earlier versions.


Joanne
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
I believe so, i mean, I'm using the univerities computers in the Computer Science Labs, would think they'd be up to date...

its using... (jdk1.5.0_06)

so... I don't know what the deal is...

justin
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Justin Fox:
I believe so, i mean, I'm using the univerities computers in the Computer Science Labs, would think they'd be up to date...

its using... (jdk1.5.0_06)...

Are you sure? At the command prompt, try typing "javac -version" (without the quotes). You will get a long error message for not including a source file, but the compiler version should appear at the top...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Doubly Linked Lists....