Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: help with delete method of linked list

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default help with delete method of linked list

    I am learning about linked lists, and so far find them to be very confusing. Take for example my delete method, which seems to be able to delete an object at the head of the list, but doesn't do anything if it exists anywhere else in the list. Here's my linked list class (the myNode class simply holds both the object data and myNode recurrence variables, it does nothing else):

    public class linkedList implements myLinkedList
    {
     
    	public myNode head;
    	public myNode temp = new myNode();
    	public myNode previous = new myNode();
     
    	public linkedList()
    	{
    		head = new myNode();
    		head = null;
    	}
     
    	public void insert(Object x)
    	{
    		if(lookup(x) == false)
    		{
    			myNode insert = new myNode();
    			insert.data = x;
    			insert.next = head; //place insert in front of head
    			head = insert; //points head to beginning of list
    		}
    	}
    	public void delete(Object x)
    	{
    		previous = null;
    		temp = head;
    		while(temp != null)
    		{
    			if(temp.data.equals(x))
    			{
    				if(previous == null) //item removed is first on the list
    				{
    					head = head.next;
    					return;
    				}
    				else
    				{
    					previous.next = temp.next;
    					return;
    				}
    			}
    			else
    			{
    				previous = temp;
    				temp = temp.next;
    			}
    		}
                    return;
    	}
    	public boolean lookup(Object x) //interface in lab says public Object, but directions specify for it to return boolean value
    	{
    		temp = head;
    		while(temp != null)
    		{
    			if(temp.data.equals(x)) 
    				return true;
    			temp = temp.next;
    		}
    		return false;
    	}
    	public boolean isEmpty()
    	{
    		if(head==null) return true;
    		else return false;
    	}
    	public void printList()
    	{
    		temp = head;
    		while(temp != null)
    		{
    			System.out.println(temp.data.toString());
    			temp = temp.next;
    		}
    	}
    }

    If temp points to head at the start of the method, and every time temp crawls though the list previous is declared as the previous temp, then why doesn't previous.next = temp.next effectively take temp out of the list?
    Last edited by sonicjr; February 18th, 2012 at 08:20 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help with delete method of linked list

    nevermind the code works, i made a silly mistake elsewhere. how do i delete posts, or is that the mods job?

Similar Threads

  1. undo delete method
    By Trunk Monkeey in forum Object Oriented Programming
    Replies: 7
    Last Post: February 7th, 2012, 02:49 PM
  2. please need help ... for the delete method in array
    By yanikapausini:) in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2012, 02:22 PM
  3. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  4. Linked List Help
    By BuhRock in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 08:43 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM