| | |
Need Help to Print Doubly Linked List(DLL)
![]() |
•
•
Join Date: Oct 2004
Posts: 5
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2004
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
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
•
•
Join Date: Oct 2004
Posts: 5
Reputation:
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
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
![]() |
Similar Threads
- Need some help with Doubly Linked List (Java)
- Reversing doubly linked list? (C)
- print from doubly linked list (Java)
- Doubly Linked List Problem (C++)
- doubly linked list--- help needed1 (C++)
- "doubly linked list" question (C)
- How Can I Sort a Doubly Linked List Queue? (C)
- doubly linked list implementation (Java)
Other Threads in the Java Forum
- Previous Thread: stop an wait arq
- Next Thread: Newbie needs help with Streams pls
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






