For this code to delete an element from a linked list how do I get the head to point to the first element in the list(which is 0)?

public class IntNode1
{
 public int item;
 public IntNode1 next;
 public IntNode1 head;

 //////Constructor///////
 public IntNode1(int item1, IntNode1 next1)
 {

  next=next1;
  item=item1;
 }

 public void delete_nth(int n)
 {
 IntNode1 prev = null;
 IntNode1 curr = head;
 
  for(curr=head; curr!=null; prev=curr, curr=curr.next)
  {
    if(curr.item == n)
    {
      if(prev==null){
        head = curr.next;
      }else
      {
        prev.next=curr.next;
      }
    }
  }
 }

For this code to delete an element from a linked list how do I get the head to point to the first element in the list(which is 0)?

public class IntNode1
{
 public int item;
 public IntNode1 next;
 public IntNode1 head;

 //////Constructor///////
 public IntNode1(int item1, IntNode1 next1)
 {

  next=next1;
  item=item1;
 }

 public void delete_nth(int n)
 {
 IntNode1 prev = null;
 IntNode1 curr = head;
 
  for(curr=head; curr!=null; prev=curr, curr=curr.next)
  {
    if(curr.item == n)
    {
      if(prev==null){
        head = curr.next;
      }else
      {
        prev.next=curr.next;
      }
    }
  }
 }

From the way you're doing it, I'd assume that you'd want to declare/define head to equal the calling class--

IntNode1 head = this;

The way that I declare the list in the main method is:

public static void main(String [] args)
 {
  int n;

  //Declare a list
  IntNode1 list1 = new IntNode1(0, new IntNode1(1, new IntNode1(2, new IntNode1(3, new IntNode1
  (4, new IntNode1(5, new IntNode1(6, new IntNode1
  (7, new IntNode1(8, null)))))))));

  Scanner sc = new Scanner(System.in);

  System.out.println("number of the element to delete: ");

  n=sc.nextInt();

  list1.delete_nth(n);

  System.out.println(list1);
 }
}

I am trying to write a program that will ask the user for an element to delete in the list and then display the list, but I keep getting a NPE, I think it is because the head is not pointing to anything? I didnt know if there was anything special I had to do when declaring the list?

The way that I declare the list in the main method is:

public static void main(String [] args)
 {
  int n;

  //Declare a list
  IntNode1 list1 = new IntNode1(0, new IntNode1(1, new IntNode1(2, new IntNode1(3, new IntNode1
  (4, new IntNode1(5, new IntNode1(6, new IntNode1
  (7, new IntNode1(8, null)))))))));

  Scanner sc = new Scanner(System.in);

  System.out.println("number of the element to delete: ");

  n=sc.nextInt();

  list1.delete_nth(n);

  System.out.println(list1);
 }
}

I am trying to write a program that will ask the user for an element to delete in the list and then display the list, but I keep getting a NPE, I think it is because the head is not pointing to anything? I didnt know if there was anything special I had to do when declaring the list?

The null pointer error is probably being thrown in the for loop because of the following--

//...

IntNode1 head; //declared but not defined, so it is initially null

//...

IntNode1 curr = head; // now curr points to null
// ...

for(curr=head; curr!=null; prev=curr, curr=curr.next)

// trying to access curr.next when curr is null, which is like calling null.next
// therefore nullpointer

If you want to get rid of the null pointer, you need to make curr point to a valid object that isn't null.


Here is another example of a linked list using your method. Notice that each time an element is added from the calling object, its "head" node points to the node that added it to the "list"

public class Node<T>{

	Node<T> head = this;
	Node<T> next = null;
	T obj;

	public Node(T obj){
		this.obj = obj;
	}

	public static void main(String... args){

		Node<Integer> n = new Node<Integer>(1);
		n.add(2);
		n.add(3);
		n.add(15);
		n.add(33);
		n.displayList();

	}

	public void add(T theNext){

		Node<T> temp = head;

		while(temp.next != null){
			temp = temp.next;
		}

		temp.next = new Node<T>(theNext);
		temp.head = this;
	}

	public void displayList(){

		String display = "";

		Node<T> temp = this;

		while(temp != null){
			display += temp.toString() + "\n";
			temp = temp.next;
		}

		System.out.println(display);
	}

	public String toString(){
		return obj.toString();
	}
}

I can also help you with your method but I'm not quite familiar with creating lists in that way. I guess learning new things doesn't hurt, so I'll re-analyze your post and try to come up with a solution.

Edit: close to a solution--

I think this is what you're looking for--

public class Node<T>{

	Node<T> head = this;
	Node<T> next = null;
	T obj;
	private int listBoundary = 0;
	private static int amountCreated = 0;

	public Node(T obj){
		this.obj = obj;
		amountCreated = 0;
	}

	public static void main(String... args){

		Node<Integer> n = new Node<Integer>(1);
		n.add(2);
		n.add(3);
		n.add(15);
		n.add(33);
		n.delete(1); // delete value at indice 1 (2)
		n.displayList();
	}

	public void delete(int index){
		if(index >= 0 && index < listBoundary){
			int num = 0;
			Node<T> temp = this; // for current
			Node<T> previous = null; // for previous
			Node<T> otherNext = null; // for next
			while(num != index){
				previous = temp; // set the previous node to temp before incrementing
				temp = temp.next; // index wasn't value at temp, so increment temp to next node
				otherNext = temp.next; // the value pointed by temp's increment
				num++; // let's try a different number!
			}
			previous.next = otherNext; // make the previous value point to the value after the deleted node
			listBoundary--; // decrement the boundary
		}
	}

	public void add(T theNext){

		Node<T> temp = head;

		while(temp.next != null){
			temp = temp.next;
		}
		listBoundary++;
		temp.next = new Node<T>(theNext);
		temp.head = this;
	}

	public void displayList(){

		String display = "";

		Node<T> temp = this;

		while(temp != null){
			display += temp.toString() + "\n";
			temp = temp.next;
		}

		System.out.println(display);
	}

	public String toString(){
		return obj.toString();
	}
}

I personally don't like this kind of list because you're relient on the root node either being the calling node or some other node of your choice.

I suppose you can make your first node (root node) an independent node of the class, but it isn't recommended unless your nodes are separate from the actual list.

I see what you mean with it being null. Maybe it is the way that I am writing the code. Here is what the assignment is and there is one part of it that might be throwing everything off.

"Write a program that takes a linked list and an integer n, and deletes the n-th element. Assume that the head of the list is number zero. Note that you can't write a method that takes the head of a list and deletes the n-th element. To delete the first element, you must change the value of "head" -- and Java methods can't change the values of their arguments. I suggest you put the code in your main method."

The last part where he says to put the code in the main method. I dont really know what he means by this?

It also says the program should take a linked list and integer n. So I think I have to write it like this:
public void delete_nth(IntNode list, int n)?

Pretty much, unless I've missed something else.

If you need to delete the head node, refer to one of the previous posts - I'm sure it's mentioned there somewhere. Node<T> head = head.next; To delete the head.

do you know what he means by putting the code in the main method? I cant put a method in a main method?

public class LinkedList<T>{

	private Node<T> root = null;

	public static void main(String... args){
		LinkedList<Integer> myLL = new LinkedList<Integer>();

		myLL.add(3);
		myLL.add(22);
		myLL.add(13);
		DeleteElement(myLL, 0);
		myLL.displayList();

	}

	public static <T> void DeleteElement(LinkedList<T> ll, int n){
		if(n >= 0 && n < ll.length()){
			int num = 0;
			LinkedList<T>.Node<T> temp = ll.getRoot();
			LinkedList<T>.Node<T> previous = null;
			LinkedList<T>.Node<T> otherNext = null;
			if(num != 0){
				while(num != n){
					previous = temp; // set the previous node to temp before incrementing
					temp = temp.next; // index wasn't value at temp, so increment temp to next node
					otherNext = temp.next; // the value pointed by temp's increment
					num++; // let's try a different number!
				}
				previous.next = otherNext; // make the previous value point to the value after the deleted node
			}
			else ll.root = ll.root.next; // most likely what your instructor wanted
		}
		else System.out.println("Index " + n + " is not within list-range!");
	}

	public LinkedList(){

	}

	public Node<T> getRoot(){
		return root;
	}

	public void add(T item){

		Node<T> temp = root;

		if(root != null){
			while(temp.next != null){
				temp = temp.next;
			}
			temp.next = new Node<T>(item);
		}
		else root = new Node<T>(item);
	}

	public int length(){
		int size = 0;
		Node<T> temp = root;
		while(temp != null){
			size++;
			temp = temp.next;
		}
		return size;
	}


	public void displayList(){

		String display = "";

		Node<T> temp = root;

		while(temp != null){
			display += temp.toString() + "\n";
			temp = temp.next;
		}

		System.out.println(display);
	}

	class Node<T>{
		Node<T> next = null;
		T type;

		public Node(T other){
			type = other;
		}

		public String toString(){
			return type.toString();
		}
	}
}


"Write a program that takes a linked list and an integer n, and deletes the n-th element. Assume that the head of the list is number zero. Note that you can't write a method that takes the head of a list and deletes the n-th element. To delete the first element, you must change the value of "head" -- and Java methods can't change the values of their arguments. I suggest you put the code in your main method."

I'm assuming that he means placing a reference to the method won't be enough since you can't change what the actual argument reference is pointing to, within the method.

For example--

public void changeRootFALSE(IntNode1 root){
    root = new IntNode1(2);
    System.out.println(root);
}
IntNode1 myNode = new IntNode1(2);
System.out.println(myNode);
changeRootFALSE(myNode);
System.out.println(myNode);

Notice that even after the method call, myNode will print out the same address AFTER the method because you cannot change the address the method argument is pointing to.

I believe that is what your instructor was getting at, he just said it in a different way.

Basically there would be no way to change what the root is pointing to, so doing something like-- root = root.next; --in the method would be pointless.

You can use the method mentioned in my previous post or code some kind of way to alter the root node statically within your main method. It should be ok since you're within static context of the calling object, so any objects of the same class that are created should be private-accessible.

If this isn't entirely correct then i'd like someone to correct me, since the previous example does show some proof of this.

This article has been dead for over six months. Start a new discussion instead.