SinlgyLinkedList circular reference

package SinlgyLinkedList;

import java.util.HashSet;

//Node Class for data and next reference
class Node {
	String data;
	Node next;

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public Node getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}
}

// Singly linked list implementation
class SinglyLinkedList {
	Node head;

	public Node getHead() {
		return head;
	}

	public void setHead(Node head) {
		this.head = head;
	}

	// add Node
	public void add(String data) {
		Node newNode = new Node();
		newNode.setData(data);
		if (head == null) {
			head = newNode;
		} else {
			Node temp = head;
			while (temp.getNext() != null) {
				temp = temp.next;
			}
			temp.next = newNode;
		}
	}

	// method for explicit next reference to make loop
	public void setNext(int n) {
		Node temp1 = head;
		Node temp2 = head;
		while (temp1.next != null) {
			temp1 = temp1.next;
		}
		for (int i = 1; i < n; i++) {
			temp2 = temp2.next;
		}
		temp1.next = temp2;
	}
}

public class MainClass {
	public static void main(String[] args) {
		// create singly linked list
		SinglyLinkedList list = new SinglyLinkedList();
		// add 10 nodes where last node points(refers) to null
		for (int i = 1; i <= 10; i++) {
			list.add("a" + i);
		}
		// method changing the pointer(reference) of last node
		// to a particular node to make it kind of circular
		list.setNext(5);
		// Use of hashset to find the intersection point
		HashSet<Node> set = new HashSet<Node>();
		Node temp = list.head;
		while (temp.next != null) {
			if (!set.add(temp)) {
				System.out.println(temp.data);
				break;
			}
			temp = temp.next;
		}
	}
}

Open in new window


i tried above example from

http://www.enterjava.com/search?updated-min=2015-01-01T00:00:00%2B05:30&updated-max=2016-01-01T00:00:00%2B05:30&max-results=6

I have not understood example clearly.

what it means by last node pointing to 5th Node.

what it means by

     // method changing the pointer(reference) of last node
            // to a particular node to make it kind of circular


SinlgyLinkedList is not jdk defined standard API class right. Is it just custom class we create as above?

insertion point is node having data value as a5
what it means by above statement?


I put break point at line 71 as attached.(not at line 73 or line 74). But when i was debugging it went through for loop 10 times adding each element to the list. I wonder why eclipse debugger added in 10 different steps like a1, a2, till ...a10 to the list.

please advise
Debugger10Times.png
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
what it means by last node pointing to 5th Node.
I think the diagram for the "structure" in the link you posted explains this pretty good. It just means that instead of the list having an end (ie. a "last" node) it just circulates back around to the 5th node. So if you step through the list, rather than coming to the end of the list after 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, you would just keep on going forever and ever... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9, 10, ....... and so on.

As to why they are doing this, I have no idea. I can't think of any practical use for doing this.

SinlgyLinkedList is not jdk defined standard API class right. Is it just custom class we create as above?
No, it is not. Your are obviously creating it here because you have a line that starts the definition of it, ie. line 28 of the above...      class SinglyLinkedList {

I wonder why eclipse debugger added in 10 different steps like a1, a2, till ...a10 to the list.
Because the method call to add items to the list is inside a loop that is defined to execute 10 times. I thought that bit should be obvious by now?
0
sarabandeCommented:
As to why they are doing this, I have no idea. I can't think of any practical use for doing this.
circular lists normally were linked from last node to head what makes them a circle. this case has some good use cases if you know the number of nodes in the list, since you can now use each node for begin (as head) and don't need extra code for heandling begin and end.

your list is a lasso rather than a circle. this can make sense if the list has a fixed number of nodes which never change and a fixed number of nodes which were a matter of change. for example memory is organized like this with non-swappable and swappable parts.


insertion point is node having data value as a5
all nodes have a string value "a1", "a2", ... , "a10". the value was set when the nodes were added:

list.add("a" + i);

Open in new window


so the fifth node has string value "a5".

Sara
0
gudii9Author Commented:
I think the diagram for the "structure" in the link you posted explains this pretty good.

which diagram in the link?



I wonder why eclipse debugger added in 10 different steps like a1, a2, till ...a10 to the list.
Because the method call to add items to the list is inside a loop that is defined to execute 10 times. I thought that bit should be obvious by now?

i have not put debug point inside for loop in the method call (list.add("a" + i);) right?
stilli wonder why it addes one by one item to list.
I thought all the 10 should add one stroke in the back ground by eclipse debugger?

alwo why i got output as 'a5' for my orignially posted code. Please advise
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gudii9Author Commented:
I wonder why eclipse debugger added in 10 different steps like a1, a2, till ...a10 to the list.
Because the method call to add items to the list is inside a loop that is defined to execute 10 times. I thought that bit should be obvious by now?

if i put break point at line 8 and 15 then in between for loop add for 10 times is skipped right (in showing to us)?But i background added 10 times still by eclipse?
brkPt.png
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
which diagram in the link?
I thought that would be pretty obvious.... They have labelled it as "Image 3"

stilli wonder why it addes one by one item to list.
I thought all the 10 should add one stroke in the back ground by eclipse debugger?
I don't know.. maybe because you are pressing the "Step over" button and so it a executes one step at a time? If you want it to just add the 10 items at once, you would have needed to press "Continue" or put the cursor on a line below the loop and select "Run to cursor"

alwo why i got output as 'a5' for my orignially posted code. Please advise
Because that is the whole idea of the code, it is telling you which item is the one that is being looped back too. Once you find the "Image 3" mentioned above it should "become obvious" (I am saying that phrase quite a lot to you lately) as to why a5 is printed to console.

if i put break point at line 8 and 15 then in between for loop add for 10 times is skipped right (in showing to us)?But i background added 10 times still by eclipse?
I have NO idea of what you just asked there.
0
sarabandeCommented:
I wonder why eclipse debugger added in 10 different steps like a1, a2, till ...a10 to the list.

Open in new window


a for loop like
	for (int i = 1; i <= 10; i++) {
			list.add("a" + i);
		}

Open in new window

actually is equivalent to sequence of 10 statements
list.add("a" + 1);
list.add("a" + 2);
list.add("a" + 3);
...
list.add("a" + 10);

Open in new window

the compiler can make some optimizations but the debugger will step into each of the ten statements if a breakpoint was set at statement 'list.add("a" + i);' or if you do 'next step' 10 times after for statement.

also why i got output as 'a5' for my orignially posted code.
your original code for output is
      
Node temp = list.head;
		while (temp.next != null) {
			if (!set.add(temp)) {
				System.out.println(temp.data);
				break;
			}
			temp = temp.next;
		}

Open in new window

the sequence of statements executed is

Node temp = list.head; // temp is node 'a1'
while ( temp.next != null) // true cause a1 points to a2
if (!set.add(temp)) // set.add returns true cause a1 is not in set 
                               //==> !set.add is false 
                               //==> the if block was not entered
temp = temp.next; // temp is now a2
while ( temp.next != null) // true cause a2 points to a3
if (!set.add(temp)) // set.add returns true cause a2 is not in set 
temp = temp.next; // temp is now a3
...
temp = temp.next; // temp is now a10
while ( temp.next != null) // true cause a10 points to a5
if (!set.add(temp)) // set.add returns true cause a10 is not in set
temp = temp.next; // temp is now a5
while ( temp.next != null) // true cause a5 points to a6
if (!set.add(temp)) // set.add returns false cause a5 is already in set 
                               //==> !set.add is true 
                               //==> the if block was entered
System.out.println(temp.data); // prints "a5"  voilĂ 
break; // while loop was exited

Open in new window


Sara
0
gudii9Author Commented:
the compiler can make some optimizations but the debugger will step into each of the ten statements if a breakpoint was set at statement 'list.add("a" + i);' or if you do 'next step' 10 times after for statement.

i have put break point at line 8.
Then pressed Step Over(F6) as attached.

I expected debugger also to do optimization but it does added 10 times each value to list even without 'breakpoint was set at statement 'list.add("a" + i);' '


or if you do 'next step' 10 times after for statement.

i think here you mean if i do 'Step Over(F6)' as attached right?


if i put break point at line 8 and 15 then in between for loop add for 10 times is skipped right (in showing to us)?But i background added 10 times still by eclipse?
I have NO idea of what you just asked there.
i am referring to line numbers 8, 15 from the screenshot attachment
brkPtLine8.png
brkPtLine8-noBreakPointAtLine11StillDoes
0
sarabandeCommented:
you have to give up your wrong assumptions. the debugger will not skip any statements and 'step over' performs the statement and holds at next statement. if it is within a loop, each statement of loop body will explicitly performed as explained above.

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
a for loop like
      for (int i = 1; i <= 10; i++) {
                  list.add("a" + i);
            }

                                         
1:
2:
3:
Select all
Open in new window

actually is equivalent to sequence of 10 statements
list.add("a" + 1);
list.add("a" + 2);
list.add("a" + 3);
...
list.add("a" + 10);
above clarifies part of my confusion
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.