Need Help to Print Doubly Linked List(DLL)

Reply

Join Date: Oct 2004
Posts: 5
Reputation: number1tiancai is an unknown quantity at this point 
Solved Threads: 0
number1tiancai number1tiancai is offline Offline
Newbie Poster

Need Help to Print Doubly Linked List(DLL)

 
1
  #1
Oct 27th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Oct 27th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: number1tiancai is an unknown quantity at this point 
Solved Threads: 0
number1tiancai number1tiancai is offline Offline
Newbie Poster

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

 
0
  #3
Oct 27th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: number1tiancai is an unknown quantity at this point 
Solved Threads: 0
number1tiancai number1tiancai is offline Offline
Newbie Poster

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

 
0
  #4
Oct 27th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: number1tiancai is an unknown quantity at this point 
Solved Threads: 0
number1tiancai number1tiancai is offline Offline
Newbie Poster

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

 
0
  #5
Oct 27th, 2004
NVM!!! i figured
i am dumbass, gotta use getData().
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC