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();
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!