0 Replies - 10366 Views - Last Post: 10 February 2006 - 09:07 PM Rate Topic: -----

#1 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

reverse a doubly linked list

Posted 10 February 2006 - 09:07 PM

I need help. I cannot figure out how to reverse a doubly linked list.

Can someone take a look at my code and explain to me what exactly I am doing wrong?

When ran either nothing happens when I try to reverse the list or else I get null pointer exception error.

I've tried several different ways but I've got nothing.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LinkedLists extends JFrame
{
	private JTextArea showList;	
	private JButton print, reverse;
	DLLNode node; 

  
	public LinkedLists()
	{
  node = new DLLNode("One", null);
  new DLLNode("Four",new DLLNode("Three",new DLLNode("Two", node)));
    
  setTitle("Lab 5 - Double Linked Lists");	
  showList = new JTextArea(5,1);
      
  print = new JButton("Print to Screen");  
  print.addActionListener(new ActionListener(){
  	
  	public void actionPerformed (ActionEvent e)
  	{
    showList.selectAll();
    showList.setText("");    
    printList(node);    
  	}
  });
  
    	
  reverse = new JButton("Reverse List");
  reverse.addActionListener(new ActionListener(){
  	
  	public void actionPerformed (ActionEvent e)
  	{    
    node = reverseList(node);    
      	
  	}
  });
  
    
  Container cp = this.getContentPane();
  cp.setLayout(new ColumnLayout(1,10,10,ColumnLayout.WIDTH));	
  cp.add(new JLabel("Double Linked List"));
  JPanel buttonPanel = new JPanel();
  buttonPanel.setLayout(new FlowLayout());
  buttonPanel.add(print);
  buttonPanel.add(reverse);
  cp.add(showList);
  cp.add(buttonPanel);  
  pack();
  setSize(new Dimension(350, 200));
  setResizable(false);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
	}
    
    public void printList(DLLNode head)
    {
    	DLLNode current = head;    	
        
        while (current != null) 
        {        	
  	showList.append(current.getData()+"\n");              
            current = current.getNext();
        }        
    }	
    
    public DLLNode reverseList(DLLNode head)
    {
    	
 ¬† ¬†    DLLNode temp = head;
 ¬† ¬†    DLLNode last = null; ¬† ¬†	
    	
 ¬† ¬†    while (temp != null) 
 ¬†     ¬†{
        DLLNode second = temp.getNext();        
                
        temp.setNext(temp.getNext().getNext());
        temp.getNext().setNext(temp);
        second.setPrevious(last);
        second.getNext().setNext(second.getNext());
        second.setNext(second);        
        
        head = temp;        
              
    	}    	
    	
    	return head;      	
    }
    
    public static void main(String[] args) 
    {                     
        new LinkedLists();                         
    }      
}



DLLNode class:

public class DLLNode
{
    private String data;
    private DLLNode nextnode=null;
    private DLLNode prevnode=null;
    /** Default constructor
     */
    public DLLNode() {
    }
    /** This constructor accepts a String and the
     *  node that is the end of the list and adds
     *  this new node after that and updates the 
     *  references
     */
    public DLLNode(String str, DLLNode node) {
              
        data=str;
        if(node != null) {
        	this.setPrevious(node);
        	node.setNext(this);
        
      	}
                   
    }
    /** This method sets the next node instance data
     */
    public void setNext(DLLNode node) {
        nextnode=node;
    }
    /** This method sets the previous node instance data
     */
    public void setPrevious(DLLNode node) {
        prevnode=node;
    }
    /** This method returns the next node instance data
     */
    public DLLNode getNext() {
        return nextnode;
    }
    /** This method returns the previous node instance data
     */
    public DLLNode getPrevious() {
        return prevnode;
    }
    /** This method returns the String instance data
     */
    public String getData() {
        return data;
    }
    /** This method returns a String representation of the object
     */
    public String toString() {
        return "[DLLNode  data = "+data+" nextnode = "+ nextnode+
               " previous node = "+prevnode+" ]\n";
    }
    /** This method performs a equals comparison between this object
     * and a supplied object returning true if they are equal
     */
    public boolean equals(Object o) {
  if(!(o instanceof DLLNode))	
  {
  	return false;
  }
    
  DLLNode nxt = (DLLNode) o;
  if (getData() == nxt.getData() && 
      getNext() == nxt.getNext() &&
      getPrevious() == nxt.getPrevious()) 
  {
  	return true;
  }
  return false;  
	}
}



:crazy: :crazy: :blink: :blink:

This post has been edited by jayman9: 10 February 2006 - 09:26 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1