In case the example data structure is considered to be a Linked List, how does one write a function which does print out all of its members?

Viewed 168

I just started learning Linked List for a technical interview, so this question might seem a little weird.

I was reading an introduction article about Linked List from freecodecamp, and this is what the article

In JavaScript, a linked list looks like this:

const list = {
    head: {
        value: 6
        next: {
            value: 10                                             
            next: {
                value: 12
                next: {
                    value: 3
                    next: null 
                    }
                }
            }
        }
    }
};

My question is, is this a real Linked List? Say I get a question "Print out all the elements in the following linked list, and implement a Linked List yourself." Can I just use the above code? I do know how to use classes to implement a linked list, but I am just wondering if the above code counts as a linked List.

I am asking this question because I only know how to solve the array algorithm question so far. Say I want to print out all the elements in the array. I will need three steps.

  1. Create an array. // Nums = [1,2,3];
  2. write a function to print it out. // function printNums(Nums){ for (...){console.log(Nums[i]}}
  3. call the function. // printNums(Nums);

So now, I want to do a Linked List version of this. How should I do it?

New Update:

So this is my LinkedList version of printing out all the elements. As a comment mentioned, what I did in the code is in fact a linked list, but it's not called implementation. But what if I just want to test my function? Does the following code make sense to you guys?

enter image description here

3 Answers

My question is, is this a real Linked List?

Yes, it is. You have a collection of data elements, where each data element points to the next.

Say I get a question "Print out all the elements in the following linked list, and implement a Linked List yourself." Can I just use the above code?

When asked to print a specific linked list, we should assume that it is made clear what the exact interface is of that linked list (e.g. nodes are linked via a property called next, and their values are accessed via a property called value). In case of the example list, that is clear. So yes, you could use the piece of code you provided.

The question to implement a Linked List yourself, is a different one. Although it could be understood to define one particular list, that is not how most would interpret that question. It is more likely that this means you should implement a linked list interface: i.e. write code that not only provides easy ways to construct any linked list, but also for using it (find a value, insert a node, delete a node, move a node, ...)

I am just wondering if the above code counts as a linked List.

Yes, the object literal you provided is (one particular instance of) a linked list.

Say I want to print out all the elements in the array. I will need three steps.

    Create an array. // Nums = [1,2,3];

Here you make use of a feature of the JavaScript language. Implicitly a constructor is behind this: new Array(1, 2, 3). This constructor is provided as part of the core language. You also get access to methods like push, pop, indexOf, slice, splice, ...etc. All out of the box.

The difference with linked lists is that core JavaScript does not offer an implementation for it. You have to throw your own. Sure, you can use an object literal to create one linked list, but it is quite a verbose way (imagine a list with 100 numbers), and error prone (what if you misspelled next somewhere half way?)

So now, I want to do a Linked List version of this. How should I do it?

If the purpose is only to print the content of a linked list, and nothing else, you can do it like you did. But to get something that offers an interface like you get out of the box for arrays, you would write a class with some useful methods, so you could write:

let myList = LinkedList.from(6, 10, 12, 3);
console.log(...myList.values()); // outputs 6 10 12 3

But what if I just want to test my function? Does the following code make sense to you guys?

Yes, that is fine. If you know that your print function will get either null or an object that has value and next properties, where the next property can be null or another such object, ... then it is fine. In other words: you need to know the interface.

var l = head;
while (l != null) {
    console.log(l.value);
    l = l.next;
}

Trick is to use while loop until next element is null.

let prev: LinkedListElement<T> | null = this._start;
let finalEl: LinkedListElement<T> = prev;
let counter = 0;
while (prev !== null) {
    const retVal = callback(prev, counter);
    if (!retVal) {
    break;
    }
    finalEl = prev;
    prev = prev.next;
    counter++;
}

Let me know if this works.
Also look at this LinkedList class I have created, traverse function is matching the requirement you have.
Class GitHub
NPM package - @dsinjs/linked-list
Complete documentation

Related