Creating a circular linked list

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 7841 Views - Last Post: 04 October 2011 - 08:38 PM Rate Topic: -----

#31 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Creating a circular linked list

Posted 04 October 2011 - 12:22 PM

Dude i tried that, but am getting these two errors:


C:\JAVA>javac GenLinkedList.java
GenLinkedList.java:32: ')' expected
Node<AnyType> newNode = new Node<AnyType>(AnyType d);
^
GenLinkedList.java:32: illegal start of expression
Node<AnyType> newNode = new Node<AnyType>(AnyType d);
^
2 errors

This post has been edited by imu_1: 04 October 2011 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#32 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Creating a circular linked list

Posted 04 October 2011 - 12:24 PM

Look at your constructor. You assign the null instance field to the parameter d = data;. You want to assign the parameter to the instance field data = d;.
Was This Post Helpful? 1
  • +
  • -

#33 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Creating a circular linked list

Posted 04 October 2011 - 08:38 PM

Just change your Node(AnyType) constructor from

 public Node(AnyType d) 
      { 
         d = data; 
      } 


to
 public Node(AnyType d) 
      { 
         d = data; 
         p = this;
         n = this;
      } 



So if your linked list size is 0 simply do
  if(theSize == 0) {
      beginMarker = new Node<AnyType>(d);
  }
  else {
    ...
  }
  theSize++;



this will replace your beginMarker (which is emply) by a new node pointing to itself back and forth.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3