Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,273 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,257 people online right now. Registration is fast and FREE... Join Now!




problem with circular singly link list

 
Reply to this topicStart new topic

problem with circular singly link list, lab 9 -CList

redzuan
10 Oct, 2008 - 05:13 AM
Post #1

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
CODE
public class CList<T>
{
    //header of the list
    private CNode<T> header;
    //number of elements in the list
    private int count;
    private String str;
  
    
    //constructor. initialises instance variables
    public CList()
    {
        header = null;
        count = 0;
    }
    
    //return a string that displays the elements
    //in a comma separated list enclosed in brackets
    
    public<T> String toString(CNode<T> header)
    {
        
        if(header==null)
            return null;
        CNode<T> curr =header;
        //start with the left bracket and
        //value of first node
        String str="[" + header.getNodeValue();
        //append all but last node, separating
        //items with a comma polymorphism calls
        //toString() for hte nodeValue type
        
        while(curr!=null)
        {
            curr =  header.getNext();
            str += ","+ header.getNodeValue();
        }
        str +="]";
        return str;
    }
    
    //return true if the circular list is empty
    //and false otherwise
    
    public  boolean isEmpty()
    {
        if(isEmpty())
        {
          return header.getNext() == null;
        }
        return false;

        
    }
    
    //insert item at the front of the circular list
    public void addFirst(T item)
    {
        //to insert, create new node with item a value
        CNode<T> newNode = new CNode<T>(item);
        CNode<T> curr = header;
        
        
        if(isEmpty())
        {   //set the header and the newNode.setNext
            header=newNode;
            newNode.setNext(header);
        }
        else
            ////check if the curr.getNext is not the header
            while(curr.getNext()!=header)
            { //what happen to the curr, newNode.setNext and header
               curr=curr.getNext();
              //post condition: curr is referencing the last node
            
            }
               curr.setNext(newNode);
               newNode.setNext(header);
               header = newNode;
               count++;
    }
    
    //erase the item at the front of the circular list and
    //return its value. return null if the list is empty
    
    public T removeFirst()
    {
        if(header == null)
            return null;
        
       T item = header.getNodeValue();
       header = header.getNext();
       count --;
        
        return item;
    }
    
    //remove all elements from list
    public void clear()
    {
        
    }

}


the problem is here
CODE

public  boolean isEmpty()
    {
        if(isEmpty())
        {
          return header.getNext() == null;
        }
        return false;

        
    }


once compile it shows this:

Exception in thread "main" java.lang.StackOverflowError
at Lab9.CList.isEmpty(CList.java:58)
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Problem With Circular Singly Link List
10 Oct, 2008 - 05:42 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Well, of course, the problem with circles is finding the end. wink2.gif

I assumed you'd be spinning, the StackOverflowError usually means it got caught in an infinite loop where it was allocating something and eventually ran our of space.

However, the spinning is unrelated to the list
java

public boolean isEmpty() {

if( isEmpty() /* call myself*/ ) {
// you never get here
// more specifically, you never get to the if condition
// it just keeps asking and asking and asking...



User is offlineProfile CardPM
+Quote Post

redzuan
RE: Problem With Circular Singly Link List
10 Oct, 2008 - 06:35 AM
Post #3

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
QUOTE(baavgai @ 10 Oct, 2008 - 06:42 AM) *

Well, of course, the problem with circles is finding the end. wink2.gif

I assumed you'd be spinning, the StackOverflowError usually means it got caught in an infinite loop where it was allocating something and eventually ran our of space.

However, the spinning is unrelated to the list
java

public boolean isEmpty() {

if( isEmpty() /* call myself*/ ) {
// you never get here
// more specifically, you never get to the if condition
// it just keeps asking and asking and asking...



ok now i try like this is find now
CODE

public  boolean isEmpty()
    {
        if(header==null)
            return true;
        return false;
          
    }


but there still problem again here

CODE

public void addFirst(T item)
    {
        //to insert, create new node with item a value
        CNode<T> newNode = new CNode<T>(item);
        CNode<T> curr = header;
        
        
        if(isEmpty())
        {   //set the header and the newNode.setNext
            header=newNode;
            newNode.setNext(header);
        }
        else
            ////check if the curr.getNext is not the header
            while(curr.getNext()!=header)
            { //what happen to the curr, newNode.setNext and header
               curr=curr.getNext();
              //post condition: curr is referencing the last node
              curr.setNext(newNode);// curr now at newNode
              newNode.setNext(header);//
              header = newNode;
            }
        
              
        count++;
        
    }


does this method is correct

this is the main method
CODE

public class Main {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        
        CList<Integer> aList = new CList<Integer>();
        CList<Integer> headerA = new CList<Integer>();
        int t;
        Integer[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90};
        for(t=0;t<arr.length;t++)
            headerA.addFirst(arr[t]);
        
        System.out.println("The list: " + aList.toString());
        System.out.println("\n Remove first 3 elements in the list");
        
        //use the for loop to remove the first 3 elements
       /* for(int i=0;i<3;i++)
            System.out.println("Removing " + headerA..removeFirst());
        
        System.out.println("Current list" + headerA..toString());
        
        System.out.println("\n Clear List\n");
        headerA..clear();
        
        System.out.println("Resulting list: "+ headerA..toString());*/
        
    }

}


the output is:

The list: Lab9.CList@addbf1

why it show like this?
User is offlineProfile CardPM
+Quote Post

redzuan
RE: Problem With Circular Singly Link List
11 Oct, 2008 - 08:40 PM
Post #4

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
does anyone here is clever to solve my problem...
User is offlineProfile CardPM
+Quote Post

redzuan
RE: Problem With Circular Singly Link List
13 Oct, 2008 - 06:59 AM
Post #5

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
any help here...maybe a clue
User is offlineProfile CardPM
+Quote Post

pbl
RE: Problem With Circular Singly Link List
13 Oct, 2008 - 08:35 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(redzuan @ 11 Oct, 2008 - 09:40 PM) *

does anyone here is clever to solve my problem...

Clever than Baavgay ? You are insultant and do not desserve anymore help
User is online!Profile CardPM
+Quote Post

redzuan
RE: Problem With Circular Singly Link List
13 Oct, 2008 - 08:54 PM
Post #7

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
QUOTE(pbl @ 13 Oct, 2008 - 09:35 PM) *

QUOTE(redzuan @ 11 Oct, 2008 - 09:40 PM) *

does anyone here is clever to solve my problem...

Clever than Baavgay ? You are insultant and do not desserve anymore help

??????? whats the problem...do i mention Baavgay...? wutever...n please don't think i'm insultant anyone...damn this forum sometime get me sick....wtf
User is offlineProfile CardPM
+Quote Post

pbl
RE: Problem With Circular Singly Link List
13 Oct, 2008 - 09:04 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(redzuan @ 13 Oct, 2008 - 09:54 PM) *

damn this forum sometime get me sick....wtf

so get out of it

User is online!Profile CardPM
+Quote Post

redzuan
RE: Problem With Circular Singly Link List
14 Oct, 2008 - 08:33 AM
Post #9

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
QUOTE(pbl @ 13 Oct, 2008 - 10:04 PM) *

QUOTE(redzuan @ 13 Oct, 2008 - 09:54 PM) *

damn this forum sometime get me sick....wtf

so get out of it

why should i care....i'm here wanna learn not get involved with stupid excuse just coz i'm insult someone like you probably....
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 05:04AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month