1 Replies - 2164 Views - Last Post: 10 February 2006 - 11:31 AM Rate Topic: -----

#1 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Linked Lists

Post icon  Posted 09 February 2006 - 10:42 PM

Hoping someone can tell me what I am doing wrong here. When compiled it gives me an <identifier> expected error at the line I remarked on. This is a school assignment in creating a double linked list.

This is the line giving the error:
node.setPrevious(node.setNext(new DLLNode("Two",node.getNext())));



Basically I am trying to add a second node to my list. I have tried 2 different approaches to adding a new node. But I get the same error message either way. I left the second approach commented out, so you can see what else I was trying.

It works fine with the initial node that I created, if I dont try to add any other nodes. Obviously I am not sure as to the correct method to use to add more nodes, so any and all help is appreciated.

Here is the code:

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

public class LinkedLists extends JFrame
{
	private JTextArea showList;	
	private JButton print, reverse;
	DLLNode node = new DLLNode("One",null);
	
	// error message HERE on this next line	
	node.setPrevious(node.setNext(new DLLNode("Two",node.getNext())));
//	node.setNext(new DLLNode("Two",node.getNext()));
  
	public LinkedLists()
	{
    
  setTitle("Lab 5 - Double Linked Lists");
  
  showList = new JTextArea(4,1);
    
  print = new JButton("Print to Screen");
  print.addActionListener(new ActionListener(){
  	
  	public void actionPerformed (ActionEvent e)
  	{    
    printList(node);    
  	}
  });
  
    	
  reverse = new JButton("Reverse List");
  reverse.addActionListener(new ActionListener(){
  	
  	public void actionPerformed (ActionEvent e)
  	{    
    //reverseList();    
      	
  	}
  });
  
    
  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));
  setVisible(true);
	}
    
    public void printList(DLLNode head)
    {
    	DLLNode current = head;    	
        
        while (current != null) 
        {        	
  	showList.append(current.toString());              
            current = current.getNext();
            if (current == head) break;
        }        
    }	
    
    public DLLNode reverseList(DLLNode head)
    {
    	DLLNode temp = head;
    	
    	return temp;
    }
    
    public static void main(String[] args) 
    {
                     
        LinkedLists ll = new LinkedLists();
  
  ll.setDefaultCloseOperation(
  	JFrame.EXIT_ON_CLOSE);                        
    }      
}





Here is the DLLNode class, this class was provided by the instructor for us to use:

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;  
	}
}


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


Is This A Good Question/Topic? 0
  • +

Replies To: Linked Lists

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: Linked Lists

Posted 10 February 2006 - 11:31 AM

Well... I finally figured it out. You cannot instantiate the nodes at the class level. Has to be done inside the constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1