954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help to Print Doubly Linked List(DLL)

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

number1tiancai
Newbie Poster
5 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>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

number1tiancai
Newbie Poster
5 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

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

number1tiancai
Newbie Poster
5 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

NVM!!! i figured
i am dumbass, gotta use getData().

number1tiancai
Newbie Poster
5 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You