944,205 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5431
  • Java RSS
Oct 27th, 2004
1

Need Help to Print Doubly Linked List(DLL)

Expand Post »
A part of my hw is generate a doubly linked list which has the data from an input file ("shopping.in")

i am using tokenizer, but however, everytime i try to put the token into the list and print, it gives me a memory place (Dnote@1289321wutever)

please help me, this is due on friday. :cry:

Or if anyone has the code which is similiar to my HW will be highly appericated. thanks
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
number1tiancai is offline Offline
5 posts
since Oct 2004
Oct 27th, 2004
0

Re: Need Help to Print Doubly Linked List(DLL)

>Or if anyone has the code which is similiar to my HW will be highly appericated.
No, you do your own homework. If you post your code then we can help you with it, but we won't give you the program.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 27th, 2004
0

Re: Need Help to Print Doubly Linked List(DLL)

Quote originally posted by Narue ...
>No, you do your own homework. If you post your code then we can help you with it, but we won't give you the program.
k i am in a class right now, and i will get off around 3pm.
soon as i get home, i will post that part of my code, and please take a look it aournd 4 or 5 if possible

greatly appericated
Reputation Points: 11
Solved Threads: 0
Newbie Poster
number1tiancai is offline Offline
5 posts
since Oct 2004
Oct 27th, 2004
0

Re: Need Help to Print Doubly Linked List(DLL)

import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;

public class DoubleLinkedList {

public static void main(String args[]) {
//linkedList myList = new linkedList();
read("shopping.in");
}

public static void read(String path)
{
int tokenType;
DList myList = new DList();
try{
Reader reader = new BufferedReader(new FileReader(path));
StreamTokenizer st = new StreamTokenizer(reader);

for(tokenType=st.nextToken();tokenType!=StreamTokenizer.TT_EOF;tokenType=st.nextToken())
{
if(tokenType==StreamTokenizer.TT_NUMBER)
{
//System.out.println(""+(int)st.nval);
myList.addElement(Double.toString(st.nval), Double.toString(st.nval));
System.out.println(myList);
}
}
}
catch(Exception e)
{
System.out.println(""+e);
e.printStackTrace();
System.out.println("exiting ...");
System.exit(-1);
}
}
}
class DList{
private int numElts; //number of elements in thelist
private DNode head, tail; //keep track of the head and tail of the list

//constructor
public DList(){
numElts = 0;
head = new DNode(null,null,null,null);
tail = new DNode(head, null,null,null);
head.setNext(tail);
}

//return the size of the list
public int size(){ return numElts;}

//return true if the list is empty; otherwise, return false
public boolean isEmpty(){ return (numElts <1);}

//add an element into the list
public void addElement(Object e1, Object e2){
DNode newNode = new DNode(null,null,e1,e2);
if (!isEmpty()) {
newNode.setPrev(tail.getPrev());
tail.getPrev().setNext(newNode);
}
else {
head.setNext(newNode);
newNode.setPrev(head);
}
newNode.setNext(tail);
tail.setPrev(newNode);
numElts ++;
}

//swap two elements in the list
public void swapElements(DNode n1, DNode n2){
Object temp = n1.getElement1();
n1.setElement1(n2.getElement1());
n2.setElement1(temp);
Object temp2 = n1.getElement2();
n1.setElement2(n2.getElement2());
n2.setElement2(temp2);
}

//return the first node of the list
public DNode first(){
if(isEmpty()) return null;
return head.getNext();
}

//return the last node of the list
public DNode last(){
if(isEmpty()) return null;
return tail.getPrev();
}
}
//Linknode
class DNode{
private DNode prev, next;
private Object element1, element2;

//constructor
public DNode(DNode newPrev, DNode newNext, Object elem1, Object elem2){
prev = newPrev;
next = newNext;
element1 = elem1;
element2 = elem2;
}

//Accessor methods
public DNode getNext() {return next;}
public DNode getPrev() {return prev;}
public Object getElement1() {return element1;}
public Object getElement2() {return element2;}


//update methods
public void setNext(DNode newNext) {next=newNext;}
public void setPrev(DNode newPrev) {prev=newPrev;}
public void setElement1(Object newElement) {element1=newElement;}
public void setElement2(Object newElement) {element2=newElement;}

}




OK, this is what i have so far, if i run the program, it will return me "DList@108786b"

tokenizer for sure works, i tried it.


any help? thanks
Reputation Points: 11
Solved Threads: 0
Newbie Poster
number1tiancai is offline Offline
5 posts
since Oct 2004
Oct 27th, 2004
0

Re: Need Help to Print Doubly Linked List(DLL)

NVM!!! i figured
i am dumbass, gotta use getData().
Reputation Points: 11
Solved Threads: 0
Newbie Poster
number1tiancai is offline Offline
5 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: need help with while loop
Next Thread in Java Forum Timeline: Newbie needs help with Streams pls





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC