简体   繁体   中英

How Do You Implement “add” method to Singly Linked List?

Here is the class header along with its fields below:

public class SinglyLinkedList <E> implements MiniList <E> 
{
   protected Entry <E> head = null;

   private int size = 0; 

I need to implement this method for my lab and I'm clueless with LinkedLists:

   public boolean add(E element) 
   {

         //method should return true once element is added

         //EDIT: I THINK I SOLVED IT:
         head.element = element;
         return true;

         //Does everyone think the two lines above look correct?
   }

So very simply problem and I'm very new to non-random access data structures. Could someone solve the problem above for me? I need a base foundation so I can continue with the rest of the lab.

Initially, your head member data is set to null. If that is a requirement (versus allocating it initially), you need to check for that in your add() method and allocate it if necessary:

if ( head == null )
{
    head = new Entry<E>();
    head.element = element;
}

and if head is not null, you have to allocate a new Entry and allow for the existing data to remain a part of the list by having the new data reference it:

else
{
    Entry<E> new_entry = new Entry<E>();
    new_entry.element = element;
    new_entry.next = head;
    head = new_entry;
}

Well, a Linked List works like this:

You have the head node, this has a reference to the next node, which has a reference to the next, etc. Here's a diagram I just drew:

链表图

The head has a reference to the following nodes, but it also has the Data within. So if I create a Linked List of Strings.

LinkedList<String> groceryList = new LinkedList<String>();
groceryList.add("VEGGIES");
groceryList.add("FRUITS");

The head has the data VEGGIES, then also has a reference to the next object in the list, in other words the FRUITS term, which is also the tail.

Let's say our list is empty. No head, no tail, no data. This we assign our head as a new entry. It will be the first element in the list, and our doorway into accessing the list. If the list isn't empty, in order to add to our list, we have to get access to the current last element in the list. To do that, we have to travel down the list.

//Some Pseudo Code
Entry current = head;
Entry last = null;
while(Not Done Looping)
{
    Entry temp = current.getNext();
    if(temp is not null) //We have a reference to another element
    {
        //We have more list to see
        current = temp;
    }
    else
        last = current;
}

That will give us the last entry. Then in order to add another element, we simply set the reference of the last object to the Entry we're trying to add.

That's the gist of how it works. Now you just have to implement that based upon what your "Entry" object is.

I'm assuming add means add an element to that list? What have you tried?

There are two cases that you must handle:

  1. When the list is empty
  2. When the list is not empty

Think about what you need to do in both cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM