Problems understanding the implementation of a linked list

Update: Many thanks to everyone who responded !!! It made me feel that I was not completely alone in my efforts to learn Java. Please excuse me, but I think I didn’t clarify what I’m not getting about linked lists and exercises app -

at first - how a class definition can contain an object in itself, I know this is recursion, but for me it is still a very strange and alien concept.

second - how exactly can a linked list object "reference" to another node?

the third - if two objects are separated by an equal sign, does this mean that - that the second object disappears, and the rest is a "name" that now points to the first object or vice versa?

then what I am not getting about the program below is the following: after the linkList class instance has been called, its constructor is called and it gives the Link class private link object null value first, i.e. indicates that it does not indicate anything. Then, when the first new node is created, the public void insertFirst method is called, it assigns values ​​to the objects of its variables, and then something absurd happens - the object that first points to nothing is assigned to the new element, thereby making both objects point to nothing and with the first = newLink; I completely lost ...

I take a college course on algorithms and data structures, and since the professor is really poor and his explanations are useless, I try to learn on my own from a book called Algorithms and Data Structures by Robert Laford.

Now I study Linked Lists, and the book provides an example of code for implementing a linked list:

Link.java:

class Link { public int iData; // data item public double dData; // data item public Link next; // next link in list public Link(int id, double dd) { // constructor iData = id; // initialize data dData = dd; // ('next' is automatically } // set to null) public void displayLink() { // display ourself System.out.print("{" + iData + ", " + dData + "} "); } } 

LinkList.java:

 class LinkList { private Link first; // ref to first link on list public LinkList() { // constructor first = null; // no links on list yet } public boolean isEmpty() { // true if list is empty return (first==null); } // insert at start of list public void insertFirst(int id, double dd) { // make new link Link newLink = new Link(id, dd); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink } public Link deleteFirst() { // delete first item // (assumes list not empty) Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp; // return deleted link } public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } } 

LinkListApp.java:

 class LinkListApp { public static void main(String[] args) { LinkList theList = new LinkList(); // make new list theList.insertFirst(22, 2.99); // insert four items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99); theList.displayList(); // display list while( !theList.isEmpty() ) { // until it empty, Link aLink = theList.deleteFirst(); // delete link System.out.print("Deleted "); // display it aLink.displayLink(); System.out.println(""); } theList.displayList(); // display list } } 

I just CAN'T understand the code that inserts and displays items in a linked list.

How can it be that newLink.next = first; and first = newLink; newLink.next = first; and first = newLink; after creating a new object?

Please, help!

+4
source share
5 answers

Each Link contains an .next link for the next Link element (except for the last element having .next = null .

A LinkList contains a link ( .first ) to the first Link object that it contains.

To insert a new Link at the beginning of LinkList , we need to do the following:

  • Create a new Link object to insert in front ( newLink ).
  • Let the newly created Link point to the previous first Link object as its .next
  • Reset the link .first of the LinkList to the newLink , effectively overwriting the previous link (marked with a cross below).

Source: See cacoo.com/diagrams/SRZoHdJ4GEn5PKAF

This is what happens:

 public void insertFirst(int id, double dd) { Link newLink = new Link(id, dd); newLink.next = first; first = newLink; } 
+7
source

What may confuse you is that this implementation of the list is LIFO (Last In First Out), so the last element inserted is the first that is returned when it passes.

newLink.next = first; puts the previous first element in a new element (so the second)

first = newLink; puts a new item at the top of the list

There are many ways in which linked lists can behave differently, depending on which collection they want (queue, stack) or different templates (single or double, circular), etc.

+2
source

Suppose we have a linked list:

  first -> Link("monday") Link("monday").next -> Link("tuesday") Link("tuesday").next -> Link("wednesday") 

Now we want the week to start with Sunday.

First we create a new link for "sunday":

  Link("sunday") # newLink = new Link(id,dd) 

and say his follower is Monday

  first -> Link("monday") Link("sunday").next -> Link("monday") # newLink.next = first Link("monday").next -> Link("tuesday") Link("tuesday").next -> Link("wednesday") 

Finally, we will fix the beginning of the week

  first -> Link("sunday") # first = newLink Link("sunday").next -> Link("monday") Link("monday").next -> Link("tuesday") Link("tuesday").next -> Link("wednesday") 
+2
source

You add a new Link to the head of the LinkList object by setting head = newLink;

Add an existing list to the new next element by setting newLink.next = first;

+1
source

As your new node is added, it is added to the beginning of the LinkedList, since the First pointer pointed to the first node in the list, but when newNode arrives at the same list, this pointer must first point to the newly added node, and the newly added node must point to the previously the first node installed so that it can become the first node of the linked list.

Hope this diagram can explain a little:

LINKED LIST IMAGE

That is why you should write newLink.next = first; and first = newLink; newLink.next = first; and first = newLink;

+1
source

Source: https://habr.com/ru/post/1399183/


All Articles