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 8 of 8

Thread: [Linked List] Problems deleting items from a linked list

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Linked List] Problems deleting items from a linked list

    Hello all!

    I am having a terrible time trying to figure out how to delete an item from a linked list. I understand that if it is the first item, then you need to get the link to the next item and then assign that to the node so that nothing is no longer pointing to the first item and thus ends up in the garbage. If it's in the middle, then the previous item will link to the item after the item to be deleted to once again be left for garbage. If it's the last then the previous item linking to it should be null and that the node pointing to the last item should now point to the previous one since it is indeed the last item. I cannot figure this out, I've drawn pictures and tried to conceive of possible solutions with no success. I need some help and direction -thanks!

    My current results when deleting the item lies in sorting. I call out a for loop that sorts the randomly generated list of strings and then grabs that item for comparison once it finds the smallest it insert it first in a new linked list to be then referenced later. When i call the delete method, it runs but the results is not expected. The new linked list shows the lowest item (i.e. 66) but repeats it over and over again. Thus when the smallest method calls out, its not finding the next smallest, instead it find the smallest which is the first number it found in the first place but never deleted. Ultimately, this leads to the final conclusion that my delete method is not working like it should and its driving me mad trying to figure out. I understand something needs to change in the if statements, but what exactly.

    PS: The items within the list are randomly generated integers that are parsed into string to be inserted into the linked list. Everything works fine except when i try and delete a list item.

    Please find my code below:

    UseStringLog.java
    //----------------------------------------------------------------------------
    // UseStringLog.java           by Dale/Joyce/Weems                   Chapter 2
    //								updated by Lee
    //
    // Example of applying a brute force sorting routine to the ArrayStringLog
    //----------------------------------------------------------------------------
     
    import ch2pt2.*;
    import java.util.Date;
    import java.util.Random;
     
    // add imports as directed by comments below
     
    public class UseStringLog
    {
      public static void main(String[] args)
      {
        final int LOG_SIZE = 10;
        final int NUM_SIZE = 100;
     
        System.out.println("Array: ");
     
        // increments the log size and random number size used in the driven method
        // by multiples of 10
       	System.out.println("Log Size: " + LOG_SIZE);
    	System.out.println("Random Number Size: " + NUM_SIZE);
    	System.out.println("Average Time: " + ((driven(LOG_SIZE, NUM_SIZE) + driven(LOG_SIZE, NUM_SIZE) + driven(LOG_SIZE, NUM_SIZE)) / 3));
      }
     
      public static long driven(int numElements, int maxValue)
      {
        // declare and instantiate a sampleCopy and sampleSorted string logs
    	StringLogInterface sampleCopy = new LinkedStringLog("Work Array");
    	StringLogInterface sampleSorted = new LinkedStringLog("Sort Array");
     
     
     
        // declare some timing variables startClock and stopClock
        // don't forget to import java.util.Date prior to UseStringLog
        long startClock;
        long stopClock;
     
     
    	// declare a variable named elapsedTime of type long
    	// elapsedTime will hold the time differential in milliseconds
    	long elapsedTime;
     
     
        // declare other variables
        // two String variables named min and rand
        //		since a string log contains methods passing strings,
        //		the int's will need to be converted
        String min;
        String rand;
        int temp;
     
     
    	// declare three integer variables named numElements, minValue, and maxValue
    	// set numElements and maxValue to 10000
    	// set minValue to 1
    	int minValue = 1;
     
     
        // declare a variable named random of type Random and instantiate it.
    	Random random = new Random();
     
     
        // Load the string log sample with 10000 random numbers ranging from 1-10000
        // don't forget to import java.util.Random prior to UseStringLog
     
     
     
    	for(int i = 0; i < numElements; i++)
    	{
    		rand = Integer.toString(random.nextInt(maxValue - minValue) - minValue + 1);
    		sampleCopy.insert(rand);
    	}
     
        // start the clock
    	// instantiate the timer Date().getTime() for startClock
    	startClock = new Date().getTime();
     
     
    	// insert a sorting method here to sort the array of numbers represented
        // Hint: 	1. identify the smallest element in sampleCopy and assign it to min
        //			2. insert min into sampleSort
        //			3. delete min from sampleCopy
        //			4. repeat steps 1-3 until sampleCopy is empty
     
    	for(int i = 0; i < numElements; i++)
    	{
    		min = sampleCopy.smallest();
    		sampleSorted.insert(min);
    		sampleCopy.delete(min);
    	}
     
        // stop the clock
    	stopClock = new Date().getTime();
     
     
    	// print out the time it took to do the sort in milliseconds
    	elapsedTime = stopClock - startClock;
     
     
    	// print out the sample string log with the .toString method
    		System.out.println(sampleCopy.toString());
     
        // print out the sampleSort string log with the .toString method
    		System.out.println(sampleSorted.toString());
     
    	return elapsedTime;
      }
    }

    StringLogInterface.java
    //----------------------------------------------------------------------
    // StringLogInterface.java     by Dale/Joyce/Weems             Chapter 2
    //
    // Interface for a class that implements a log of Strings.
    // A log "remembers" the elements placed into it.
    //
    // A log must have a "name".
    //----------------------------------------------------------------------
     
    package ch2pt2;
     
    public interface StringLogInterface
    {
      	// Precondition:   This StringLog is not full.
      	//
      	// Places element into this StringLog.
      	void insert(String element);
     
      	// Returns true if this StringLog is full, otherwise returns false.
      	boolean isFull();
     
    	// Returns the number of Strings in this StringLog.
      	int size();
     
      	// Returns true if element is in this StringLog,
      	// otherwise returns false.
      	// Ignores case differences when doing string comparison.
      	boolean contains(String element);
     
      	// Makes this StringLog empty.
      	void clear();
     
    	// Returns the name of this StringLog.
      	String getName();
     
      	// Returns a nicely formatted string representing this StringLog.
      	String toString();
     
      /*----------------Newly adden------------------*/
     
      	// Returns true if this StringLog is empty, otherwise returns false.
     	boolean isEmpty();
     
      	// Returns how many times element occurs in this StringLog.
      	int howMany(String element);
     
      	// Places element into this StringLog unless an
      	// identical string (case insensitive match)
      	// is already in this StringLog.
      	//
    	// Assumes StringLog not full if insertion required.
    	public boolean uniqInsert(String element);
     
    	// Deletes one occurence of element from this StringLog,
    	// if possible, and returns true. Otherwise returns false.
    	public boolean delete(String element);
     
    	// Deletes all occurences of element from this StringLog,
    	// if any, and returns the number of deletions.
    	public int deleteAll(String element);
     
    	// Precondition: this log contains at least one string
    	//
    	// Returns smallest element in this StringLog
    	public String smallest();
    }

    LLStringNode.java
    //----------------------------------------------------------------------------
    // LLStringNode.java            by Dale/Joyce/Weems                  Chapter 2
    //
    // Implements String nodes for a Linked List.
    //----------------------------------------------------------------------------
     
    package ch2pt2;
     
    public class LLStringNode 
    {
      private String info;
      private LLStringNode link;
     
      public LLStringNode(String info)
      {
        this.info = info;
        link = null;
      }
     
      public void setInfo(String info)
      // Sets info string of this LLStringNode.
      {
        this.info = info;
      }
     
      public String getInfo()
      // Returns info string of this LLStringNode.
      {
        return info;
      }
     
      public void setLink(LLStringNode link)
      // Sets link of this LLStringNode.
      {
        this.link = link;
      }
     
      public LLStringNode getLink()
      // Returns link of this LLStringNode.
      {
        return link;
      }
    }

    LinkedStringLog.java
    //----------------------------------------------------------------------
    // LinkedStringLog.java       by Dale/Joyce/Weems              Chapter 2
    //
    // Implements StringLogInterface using a linked list 
    // of LLStringNode to hold the log strings.
    //----------------------------------------------------------------------
     
    package ch2pt2;
     
    public class LinkedStringLog implements StringLogInterface 
    {
      protected LLStringNode log; // reference to first node of linked 
                                  // list that holds the StringLog strings
      protected String name;      // name of this StringLog
     
      public LinkedStringLog(String name)
      // Instantiates and returns a reference to an empty StringLog object 
      // with name "name".
      {
        log = null;
        this.name = name;
      }
     
      public void insert(String element)
      // Precondition:   This StringLog is not full.
      //
      // Places element into this StringLog.
      {      
        LLStringNode newNode = new LLStringNode(element);
        newNode.setLink(log);
        log = newNode;
      }
     
      public boolean isFull()
      // Returns true if this StringLog is full, false otherwise.
      {              
        return false;
      }
     
      public int size()
      // Returns the number of Strings in this StringLog.
      {
        int count = 0;
        LLStringNode node;
        node = log;
        while (node != null)
        {
          count++;
          node = node.getLink();
        }
        return count;
      }
     
      public boolean contains(String element)
      // Returns true if element is in this StringLog,
      // otherwise returns false.
      // Ignores case difference when doing string comparison.
      {                 
        LLStringNode node;
        node = log;
     
        while (node != null) 
        {
          if (element.equalsIgnoreCase(node.getInfo())) {
                return true;
            }
          else {
                node = node.getLink();
            }
        }
     
       return false;
      }
     
      public void clear()
      // Makes this StringLog empty.
      { 
        log = null;
      }
     
      public String getName()
      // Returns the name of this StringLog.
      {
        return name;
      }
     
      public String toString()
      // Returns a nicely formatted string representing this StringLog.
      {
        String logString = "Log: " + name + "\n\n";
        LLStringNode node;
        node = log;
        int count = 0;
     
        while (node != null)
        {
          count++;
          logString = logString + count + ". " + node.getInfo() + "\n";
          node = node.getLink();
        }
     
        return logString;
      }
     
      /*-----------------HW3_Linked_List_application------------------------*/
     
      public boolean isEmpty()
      {
          LLStringNode node = log;
          if(node == null)
          {
              return true;
          }
          else 
          {
              return false;
          }
      }
     
      public int howMany(String element) 
      {
          LLStringNode node = log;
          int count = 0;
     
          while(node != null)
          {
              if(element.equalsIgnoreCase(node.getInfo()))
              {
                  count++;
              }
              node = node.getLink();
          }
     
          return count;
      }
      public boolean uniqInsert(String element)
      {
          if(this.contains(element))
          {
              return false;
          }
          else 
          {
              this.insert(element);
              return true;
          }
      }
     
      public boolean delete(String element)
      {
         LLStringNode node = log;
         LLStringNode secondNode;
     
         if(element.equalsIgnoreCase(node.getInfo()))
         {
             log = node.getLink();
             return true;
         }
     
         secondNode = node;
         node = node.getLink();
         while(node != null)
         {
             if(element.equalsIgnoreCase(node.getInfo()))
             {
                 secondNode.setLink(node.getLink());
                 return true;
             }
     
             node.getLink();
             secondNode = node;
         }
     
         return false;
      }
     
     
      public int deleteAll(String element) 
      {
            int count = 0;
            while(this.delete(element))
            {
                count++;
            }
     
            return count;
      }
     
      public String smallest()
      {
          LLStringNode node = log;
          int smallest = Integer.parseInt(node.getInfo());
          String small = node.getInfo();
          node = node.getLink();
     
          while(node != null)
          {
              int compareValue = Integer.parseInt(node.getInfo());
              if(compareValue < smallest)
              {
                  small = node.getInfo();
                  smallest = compareValue;
              }
     
              node = node.getLink();
          }
            return small;
      }
    }

    PS I revised my original delete method to see if a new way would work when deleting items, to no luck, it goes into an infinite loop.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Linked List] Problems deleting items from a linked list

    Please post the code you are having problems with here in the thread.

    Be sure to wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Linked List] Problems deleting items from a linked list

    Sorry, I meant for all the code needed for compiling and executed for testing be posted.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Linked List] Problems deleting items from a linked list

    UseStringLog.java
    //----------------------------------------------------------------------------
    // UseStringLog.java           by Dale/Joyce/Weems                   Chapter 2
    //								updated by Lee
    //
    // Example of applying a brute force sorting routine to the ArrayStringLog
    //----------------------------------------------------------------------------
     
    import ch2pt2.*;
    import java.util.Date;
    import java.util.Random;
     
    // add imports as directed by comments below
     
    public class UseStringLog
    {
      public static void main(String[] args)
      {
        final int LOG_SIZE = 10;
        final int NUM_SIZE = 100;
     
        System.out.println("Array: ");
     
        // increments the log size and random number size used in the driven method
        // by multiples of 10
       	System.out.println("Log Size: " + LOG_SIZE);
    	System.out.println("Random Number Size: " + NUM_SIZE);
    	System.out.println("Average Time: " + ((driven(LOG_SIZE, NUM_SIZE) + driven(LOG_SIZE, NUM_SIZE) + driven(LOG_SIZE, NUM_SIZE)) / 3));
      }
     
      public static long driven(int numElements, int maxValue)
      {
        // declare and instantiate a sampleCopy and sampleSorted string logs
    	StringLogInterface sampleCopy = new LinkedStringLog("Work Array");
    	StringLogInterface sampleSorted = new LinkedStringLog("Sort Array");
     
     
     
        // declare some timing variables startClock and stopClock
        // don't forget to import java.util.Date prior to UseStringLog
        long startClock;
        long stopClock;
     
     
    	// declare a variable named elapsedTime of type long
    	// elapsedTime will hold the time differential in milliseconds
    	long elapsedTime;
     
     
        // declare other variables
        // two String variables named min and rand
        //		since a string log contains methods passing strings,
        //		the int's will need to be converted
        String min;
        String rand;
        int temp;
     
     
    	// declare three integer variables named numElements, minValue, and maxValue
    	// set numElements and maxValue to 10000
    	// set minValue to 1
    	int minValue = 1;
     
     
        // declare a variable named random of type Random and instantiate it.
    	Random random = new Random();
     
     
        // Load the string log sample with 10000 random numbers ranging from 1-10000
        // don't forget to import java.util.Random prior to UseStringLog
     
     
     
    	for(int i = 0; i < numElements; i++)
    	{
    		rand = Integer.toString(random.nextInt(maxValue - minValue) - minValue + 1);
    		sampleCopy.insert(rand);
    	}
     
        // start the clock
    	// instantiate the timer Date().getTime() for startClock
    	startClock = new Date().getTime();
     
     
    	// insert a sorting method here to sort the array of numbers represented
        // Hint: 	1. identify the smallest element in sampleCopy and assign it to min
        //			2. insert min into sampleSort
        //			3. delete min from sampleCopy
        //			4. repeat steps 1-3 until sampleCopy is empty
     
    	for(int i = 0; i < numElements; i++)
    	{
    		min = sampleCopy.smallest();
    		sampleSorted.insert(min);
    		sampleCopy.delete(min);
    	}
     
        // stop the clock
    	stopClock = new Date().getTime();
     
     
    	// print out the time it took to do the sort in milliseconds
    	elapsedTime = stopClock - startClock;
     
     
    	// print out the sample string log with the .toString method
    		System.out.println(sampleCopy.toString());
     
        // print out the sampleSort string log with the .toString method
    		System.out.println(sampleSorted.toString());
     
    	return elapsedTime;
      }
    }

    StringLogInterface.java
    //----------------------------------------------------------------------
    // StringLogInterface.java     by Dale/Joyce/Weems             Chapter 2
    //
    // Interface for a class that implements a log of Strings.
    // A log "remembers" the elements placed into it.
    //
    // A log must have a "name".
    //----------------------------------------------------------------------
     
    package ch2pt2;
     
    public interface StringLogInterface
    {
      	// Precondition:   This StringLog is not full.
      	//
      	// Places element into this StringLog.
      	void insert(String element);
     
      	// Returns true if this StringLog is full, otherwise returns false.
      	boolean isFull();
     
    	// Returns the number of Strings in this StringLog.
      	int size();
     
      	// Returns true if element is in this StringLog,
      	// otherwise returns false.
      	// Ignores case differences when doing string comparison.
      	boolean contains(String element);
     
      	// Makes this StringLog empty.
      	void clear();
     
    	// Returns the name of this StringLog.
      	String getName();
     
      	// Returns a nicely formatted string representing this StringLog.
      	String toString();
     
      /*----------------Newly adden------------------*/
     
      	// Returns true if this StringLog is empty, otherwise returns false.
     	boolean isEmpty();
     
      	// Returns how many times element occurs in this StringLog.
      	int howMany(String element);
     
      	// Places element into this StringLog unless an
      	// identical string (case insensitive match)
      	// is already in this StringLog.
      	//
    	// Assumes StringLog not full if insertion required.
    	public boolean uniqInsert(String element);
     
    	// Deletes one occurence of element from this StringLog,
    	// if possible, and returns true. Otherwise returns false.
    	public boolean delete(String element);
     
    	// Deletes all occurences of element from this StringLog,
    	// if any, and returns the number of deletions.
    	public int deleteAll(String element);
     
    	// Precondition: this log contains at least one string
    	//
    	// Returns smallest element in this StringLog
    	public String smallest();
    }

    LLStringNode.java
    //----------------------------------------------------------------------------
    // LLStringNode.java            by Dale/Joyce/Weems                  Chapter 2
    //
    // Implements String nodes for a Linked List.
    //----------------------------------------------------------------------------
     
    package ch2pt2;
     
    public class LLStringNode 
    {
      private String info;
      private LLStringNode link;
     
      public LLStringNode(String info)
      {
        this.info = info;
        link = null;
      }
     
      public void setInfo(String info)
      // Sets info string of this LLStringNode.
      {
        this.info = info;
      }
     
      public String getInfo()
      // Returns info string of this LLStringNode.
      {
        return info;
      }
     
      public void setLink(LLStringNode link)
      // Sets link of this LLStringNode.
      {
        this.link = link;
      }
     
      public LLStringNode getLink()
      // Returns link of this LLStringNode.
      {
        return link;
      }
    }

    LinkedStringLog.java
    //----------------------------------------------------------------------
    // LinkedStringLog.java       by Dale/Joyce/Weems              Chapter 2
    //
    // Implements StringLogInterface using a linked list 
    // of LLStringNode to hold the log strings.
    //----------------------------------------------------------------------
     
    package ch2pt2;
     
    public class LinkedStringLog implements StringLogInterface 
    {
      protected LLStringNode log; // reference to first node of linked 
                                  // list that holds the StringLog strings
      protected String name;      // name of this StringLog
     
      public LinkedStringLog(String name)
      // Instantiates and returns a reference to an empty StringLog object 
      // with name "name".
      {
        log = null;
        this.name = name;
      }
     
      public void insert(String element)
      // Precondition:   This StringLog is not full.
      //
      // Places element into this StringLog.
      {      
        LLStringNode newNode = new LLStringNode(element);
        newNode.setLink(log);
        log = newNode;
      }
     
      public boolean isFull()
      // Returns true if this StringLog is full, false otherwise.
      {              
        return false;
      }
     
      public int size()
      // Returns the number of Strings in this StringLog.
      {
        int count = 0;
        LLStringNode node;
        node = log;
        while (node != null)
        {
          count++;
          node = node.getLink();
        }
        return count;
      }
     
      public boolean contains(String element)
      // Returns true if element is in this StringLog,
      // otherwise returns false.
      // Ignores case difference when doing string comparison.
      {                 
        LLStringNode node;
        node = log;
     
        while (node != null) 
        {
          if (element.equalsIgnoreCase(node.getInfo())) {
                return true;
            }
          else {
                node = node.getLink();
            }
        }
     
       return false;
      }
     
      public void clear()
      // Makes this StringLog empty.
      { 
        log = null;
      }
     
      public String getName()
      // Returns the name of this StringLog.
      {
        return name;
      }
     
      public String toString()
      // Returns a nicely formatted string representing this StringLog.
      {
        String logString = "Log: " + name + "\n\n";
        LLStringNode node;
        node = log;
        int count = 0;
     
        while (node != null)
        {
          count++;
          logString = logString + count + ". " + node.getInfo() + "\n";
          node = node.getLink();
        }
     
        return logString;
      }
     
      /*-----------------HW3_Linked_List_application------------------------*/
     
      public boolean isEmpty()
      {
          LLStringNode node = log;
          if(node == null)
          {
              return true;
          }
          else 
          {
              return false;
          }
      }
     
      public int howMany(String element) 
      {
          LLStringNode node = log;
          int count = 0;
     
          while(node != null)
          {
              if(element.equalsIgnoreCase(node.getInfo()))
              {
                  count++;
              }
              node = node.getLink();
          }
     
          return count;
      }
      public boolean uniqInsert(String element)
      {
          if(this.contains(element))
          {
              return false;
          }
          else 
          {
              this.insert(element);
              return true;
          }
      }
     
      public boolean delete(String element)
      {
         LLStringNode node = log;
         LLStringNode secondNode;
     
         if(element.equalsIgnoreCase(node.getInfo()))
         {
             log = node.getLink();
             return true;
         }
     
         secondNode = node;
         node = node.getLink();
         while(node != null)
         {
             if(element.equalsIgnoreCase(node.getInfo()))
             {
                 secondNode.setLink(node.getLink());
                 return true;
             }
     
             node.getLink();
             secondNode = node;
         }
     
         return false;
      }
     
     
      public int deleteAll(String element) 
      {
            int count = 0;
            while(this.delete(element))
            {
                count++;
            }
     
            return count;
      }
     
      public String smallest()
      {
          LLStringNode node = log;
          int smallest = Integer.parseInt(node.getInfo());
          String small = node.getInfo();
          node = node.getLink();
     
          while(node != null)
          {
              int compareValue = Integer.parseInt(node.getInfo());
              if(compareValue < smallest)
              {
                  small = node.getInfo();
                  smallest = compareValue;
              }
     
              node = node.getLink();
          }
            return small;
      }
    }

    PS I revised my original delete method to see if a new way would work when deleting items, to no luck, it goes into an infinite loop.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Linked List] Problems deleting items from a linked list

    it goes into an infinite loop.
    Time to do some debugging to find out where and why there is an infinite loop.
    Add lots of println statements to print out messages to show where the program is executing and what the values of variables are as their values are changed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Linked List] Problems deleting items from a linked list

    I figured out what was causing the infinite loop, it was the fact that I had to assign node to equal node.getLink() for the next condition to prove false. Now that my program is running, the results are not favorable. I keep getting the smallest number and not a sorted list of randomly generated numbers. There's something going wrong in my delete method and I'm not to sure as to what. Many have recommended that I draw pictures, which I did. Logically, the program should work, but there's something doing wrong when it comes to relinking the linked list so as to send the desired deleted item to the garbage collector.
    Here is my new delete method still with problems
     public boolean delete(String element)
      {
         LLStringNode node = log;
         LLStringNode secondNode;
         int index = 0;
     
         if(element.equalsIgnoreCase(node.getInfo()))
         {
             log = node.getLink();
             return true;
         }
     
         secondNode = node;
         node = node.getLink();
         index++;
         while(node != null)
         {
             if(element.equalsIgnoreCase(node.getInfo()))
             {
                 if(index == this.size() - 1)
                 {
                     node = secondNode;
                     node.setLink(null);
                     return true;
                 }
     
                 node = node.getLink();
                 secondNode.setLink(node);
                 return true;
             }
     
             node = node.getLink();
             secondNode = node;
             index++;
         }
     
         return false;
      }

    A sample of the output is:
    Array:
    Log Size: 10
    Random Number Size: 100
    Log: Work Array

    1. 36
    2. 23
    3. 41
    4. 21
    5. 54
    6. 88
    7. 10
    8. 8
    9. 26
    10. 52

    Log: Sort Array

    1. 8
    2. 8
    3. 8
    4. 8
    5. 8
    6. 8
    7. 8
    8. 8
    9. 8
    10. 8

    As you can see, I want the list sorted but because of this delete method, it isn't deleting them from the linked list.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Linked List] Problems deleting items from a linked list

    Can you add some comments to the delete method describing what the variables are used for and what logic it is using to do its task?


    Some suggestions for easier testing and debugging:
    Use fewer elements: 4 vs 10
        final int LOG_SIZE = 4;           //<<<<<<<<<< small for easier debugging
    Use the same data each time until it works:
       String[] sA = {"23", "12", "44", "45"}; //<<<<<<<<<<<<< element source
     
     
    	for(int i = 0; i < numElements; i++)
    	{
    		rand = Integer.toString(random.nextInt(maxValue - minValue) - minValue + 1);
    		sampleCopy.insert(sA[i]); //rand);
    	}
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  2. deleting all linked list elements using ADT
    By memo1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 25th, 2011, 02:01 PM
  3. linked list
    By javasohard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:22 AM
  4. 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
  5. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 06:54 PM