Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Question Problem with output from DoubleLinkedList data structure

    I have been working on a two reference Linked List data structure that has references to both ends of a the data structures. The [DoubleNode] class that is the reference to each end of the nodes is an subclass of normal [Node] class, I thought having a subclass of the [Node] class was a good next step because this two referenced linked list should in the be a subclass of a normal Linked List data structure. The only part of the subclassing I have done is just the nodes, after I got this part to work I will begin to implement the subclass of the Linked List data structure inheritance.

    The problem is not a syntax, but yet a run-time error.
    The output is as follows:
    List (first->last)
    0 0 0 0 0 0
    List (last->first)
    0 0 0 0 0 0
    List (first->last)
    0 0 0 0
    List (first->last)
    0 0 0 0

    And The complete source code is below.
    XML Code:
    // DoubleLinkedList class
    public class DoubleLinkedList{
    	private DoubleNode first, last;
    
    	// DoubleLinkedList() method
    	public DoubleLinkedList(){
    		first=null;
    		last=null;
    	} // end constructor
    
    	// isEmpty() method
    	public boolean isEmpty(){
    		return first==null;
    	} // end method
    
    	// insertFirst(int) method
    	public void insertFirst(int value){
    		DoubleNode temp=new DoubleNode(value);
    		if(isEmpty()){
    			last=temp;
    		}else{
    			first.setPrevious(temp);
    		} // end condition
    		temp.setNext(first);
    		first=temp;
    	} // end method
    
    	// insertLast(int) method
    	public void insertLast(int value){
    		DoubleNode temp=new DoubleNode(value);
    		if(isEmpty()){
    			first=temp;
    		}else{
    			last.setNext(temp);
    			temp.setPrevious(last);
    		} // end condition
    		last=temp;
    	} // end method
    
    	// deleteFirst() method
    	public DoubleNode deleteFirst(){
    		DoubleNode temp=first;
    		if(first.getNext()==null){
    			last=null;
    		}else{
    			first.next.previous=null;
    		} // end condition
    		first=first.getNext();
    		return temp;
    	} // end method
    
    	// deleteLast() method
    	public DoubleNode deleteLast(){
    		DoubleNode temp=last;
    		if(first.getNext()==null){
    			first=null;
    		}else{
    			last.previous.next=null;
    		} // end condition
    		last=last.getPrevious();
    		return temp;
    	} // end method
    
    	// insertAfter(int, int) method
    	public boolean insertAfter(int key, int value){
    		DoubleNode current=first;
    		while(current.getData()!=key){
    			current=current.getNext();
    			if(current==null){
    				return false;
    			} // end condition
    		} // end loop
    		DoubleNode temp=new DoubleNode(value);
    		if(current==last){
    			temp.setNext(null);
    			last=temp;
    		}else{
    			temp.setNext(current.getNext());
    			current.next.previous=temp;
    		} // end condition
    		temp.setPrevious(current);
    		current.setNext(temp);
    		return true;
    	} // end method
    
    	// deleteKey(int) method
    	public DoubleNode deleteKey(int key){
    		DoubleNode current=first;
    		while(current.getData()!=key){
    			current=current.getNext();
    			if(current==null){
    				return null;
    			} // end condition
    		} // end condition
    		if(current==first){
    			first=current.getNext();
    		}else{
    			current.previous.next=current.getNext();
    		} // end condtion
    		current.next.previous=current.getPrevious();
    		return current;
    	} // end method
    
    	// displayForward() method
    	public void displayForward(){
    		System.out.println("List (first->last)");
    		DoubleNode current=first;
    		while(current!=null){
    			current.display();
    			current=current.getNext();
    		} // end loop
    		System.out.println();
    	} // end method
    
    	// displayBackward() method
    	public void displayBackward(){
    		System.out.println("List (last->first)");
    		DoubleNode current=last;
    		while(current!=null){
    			current.display();
    			current=current.getPrevious();
    		} // end loop
    		System.out.println();
    	} // end method
    
    	// main(String[]) method
    	public static void main(String[] args){
    		DoubleLinkedList list=new DoubleLinkedList();
    		list.insertFirst(2);
    		list.insertFirst(4);
    		list.insertFirst(6);
    		list.insertLast(1);
    		list.insertLast(3);
    		list.insertLast(5);
    		list.displayForward();
    		list.displayBackward();
    		list.deleteFirst();
    		list.deleteLast();
    		list.deleteKey(1);
    		list.displayForward();
    		list.insertAfter(2, 7);
    		list.insertAfter(3, 8);
    		list.displayForward();
    	} // end method
    } // end class
    
    // DoubleNode class
    public class DoubleNode extends Node{
    	public int data;
    	public DoubleNode next, previous;
    
    	// DoubleNode(int) constructor
    	public DoubleNode(int data){
    		super(data);
    	} // end constructor
    	
    	// DoubleNode(int, DoubleNode) constructor
    	public DoubleNode(int data, DoubleNode next, DoubleNode previous){
    		super(data, next);
    		this.previous=previous;
    	} // end constructor
    	
    		// getPrevious()
    		public DoubleNode getPrevious(){
    			return previous;
    		} // end method
    		
    		// setPrevious(DoubleNode) method
    		public void setPrevious(DoubleNode previous){
    			this.previous=previous;
    		} // end method
    	
    		// getNext() method
    		public DoubleNode getNext(){
    			return next;
    		} // end method
    		
    		// setNext(Node) method
    		public void setNext(DoubleNode next){
    			this.next=next;
    		} // end method
    		
    		// getData() method
    		public int getData(){
    			return data;
    		} // end method
    		
    		// setData(int) method
    		public void setData(int data){
    			this.data=data;
    		} // end method
    
    	// display() method
    	public void display(){
    		System.out.print(data+" ");
    	} // end method)
    } // end class
    
    // Node class
    public class Node{
    	public int data;
    	public Node next;
    
    	// Node() constructor
    	public Node(){
    		next=null;
    	} // end constructor
    	
    	// Node(int)
    	public Node(int data){
    		this.data=data;
    		next=null;
    	} // end constructor
    	
    	// Node(int, Node) constructor
    	public Node(int data, Node next){
    		this.data=data;
    		this.next=next;
    	} // end constructor
    	
    	// getNext() method
    	public Node getNext(){
    		return next;
    	} // end method
    	
    	// setNext(Node) method
    	public void setNext(Node next){
    		this.next=next;
    	} // end method
    	
    	// getData() method
    	public int getData(){
    		return data;
    	} // end method
    	
    	// setData(int) method
    	public void setData(int data){
    		this.data=data;
    	} // end method
    
    	// display() method
    	public void display(){
    		System.out.print(getData()+" ");
    	} // end method)
    } // end class
    Last edited by smoothedatol412; 06-04-2012 at 12:20 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Problem with output from DoubleLinkedList data structure

    The problem is not a syntax, but yet a run-time error.
    Can you explain what the problem is and post what the output should look like.

    For debugging, add lots of print statements to show the values of variables as the are changed and used.
    Add a toString() method to the Node class that returns the data and next values. That makes it easy to print out a node for debugging.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: Problem with output from DoubleLinkedList data structure

    List (first->last)
    2 4 6 1 3 5
    List (last->first)
    2 4 6 1 3 5
    List (first->last)
    5 3 1 6 4 2

    The thing the the program worked fine until I subclasses [DoubleNode] from [Node].

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Problem with output from DoubleLinkedList data structure

    what did the new class do to change how the code works?
    Did you try adding some printlns to show the values of the variables as they are set and used?
    For example why are all the values that print out 0? Where is a non-zero value stored in a variable? Where is it retrieved from that variable?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Semantic data structure
    By dacoolest in forum Advanced Java
    Replies: 6
    Last Post: 01-12-2012, 07:25 PM
  2. Which data structure to use?
    By malaguena in forum New To Java
    Replies: 4
    Last Post: 04-05-2011, 05:41 PM
  3. Which data structure to choose ?
    By lumpy in forum New To Java
    Replies: 2
    Last Post: 02-20-2010, 05:43 AM
  4. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 06:48 AM
  5. data file structure
    By Nicholas Jordan in forum Advanced Java
    Replies: 2
    Last Post: 01-07-2009, 05:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •