Reputation: 132
I tried to implement a linked list in java using the code below:
class Linkedlist<T>{
private node head, tail;
public Linkedlist(){
head = new node(null);
tail = head;
}
public boolean insert(T value){
if(head.getValue() == null){
this.head.setValue(value);
head.setNext(null);
return true;
}
node insertNode = new node(value);
tail.setNext(insertNode);
tail = insertNode;
return true;
}
public boolean insert(T value, int index) {
if ( sizeOfList() == index + 1 ) return false;
node temp = this.head.getNext();
node prvtmp = this.head;
for (int i = 0; i <= index; i++) {
temp = temp.getNext();
prvtmp = temp;
System.out.println("for loop");
}
node insertNode = new node(value);
System.out.println("node created");
prvtmp.setNext(insertNode);
insertNode.setNext(temp);
System.out.println("temps");
return true;
}
public int sizeOfList(){
int size = 0;
node temp = this.head;
while(temp.getNext() != null){
temp = temp.getNext();
size++;
}
return size;
}
public String[] rtrnList(){
node temp = this.head;
int listSize = sizeOfList();
String[] resualt = new String[listSize + 1];
for (int i = 0;i <= listSize;i++){
resualt[i] = String.valueOf(temp.getValue());
temp = temp.getNext();
}
return resualt;
}
}
Class node:
public class node<T> {
private T value;
private node next;
public node(T value){
this.value = value;
this.next = null;
}
public void setValue(T value) {
this.value = value;
}
public void setNext(node next) {
this.next = next;
}
public T getValue() {
return value;
}
public node getNext() {
return next;
}
}
When I try to run the insert
method with one argument it works fine, but when I run it with two arguments:
import java.util.Arrays;
public class main {
public static void main(String[] args){
Linkedlist list = new Linkedlist();
list.insert(2);
list.insert(2);
list.insert(2);
list.insert(2);
list.insert(2);
list.insert(2);
list.insert(11, 3);
System.out.println(Arrays.toString(list.rtrnList()));
System.out.println("finished");
}
}
The program doesn't reach the line that prints finished, but if we delete the line: list.insert(11, 3);
Then the program works just fine.
output:
for loop
for loop
for loop
for loop
node created
finish insert
Upvotes: 0
Views: 138
Reputation: 496
while inserting there is a cyclic refrence made , you can use this code for above purpose
public boolean insert(T value, int index) {
if(index > sizeOfList())
{
return false;
}
else if(index == sizeOfList())
{
if(insert(value))
return true;
else
return false;
}
else
{
node previous = this.head;
for (int i = 1; i <= index; i++) {
if(i==index-1)
{
node newnode = new node(value);
newnode.setNext(previous.getNext());
previous.setNext(newnode);
break;
}
previous = previous.getNext();
}
}
return true;
}
Output
[2, 2, 11, 2, 2, 2, 2]
Upvotes: 1
Reputation: 2385
The problems is inside the for loop of your insert(T value, int index) method when you are updating the value of prvtm and temp. In your code you are first setting the value of temp to temp.getnext() and as the temp is already changed setting prvtmp to temp creating a loop which is why when you are trying to print the list it's getting into an infinite loop while calculating the size of the list.
Just put the prvtmp = temp line before temp = temp.getNext() like following code snippet. This will solve the problem.
public boolean insert(T value, int index) { // 2 2 2 2 2 2
if ( sizeOfList() == index + 1 ) return false;
node temp = this.head.getNext();
node prvtmp = this.head;
for (int i = 0; i <= index; i++) {
prvtmp = temp;
temp = temp.getNext();
System.out.println("for loop");
}
node insertNode = new node(value);
System.out.println("node created");
prvtmp.setNext(insertNode);
insertNode.setNext(temp);
System.out.println("temps");
return true;
}
Also if you are trying to insert in the given index update the condition of the for loop from:
i <= index to i < index - 1
Happy coding!
Upvotes: 1