#any tips on how i can learn a linked List (data structure)

1 messages ยท Page 1 of 1 (latest)

distant wraithBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Help please

To learn about linked lists in Java, there are a few steps you can take:

  1. Understand the basics: A linked list is a data structure consisting of nodes where each node contains data and a reference to the next node in the sequence. It does not have a fixed size like arrays.

  2. Study Java LinkedList class: Start by familiarizing yourself with the LinkedList class in Java, which is an implementation of a doubly-linked list. Look at the methods available such as add(), remove(), get(), and more.

  3. Implement your own linked list: Practice implementing your own version of a linked list from scratch. This will help you understand how nodes are created, connected, and manipulated within the list.

  4. Solve problems using linked lists: Practice solving coding problems that involve linked lists to solidify your understanding. Websites like LeetCode or HackerRank offer a variety of problems to work on.

  5. Seek resources and tutorials: There are plenty of online resources, tutorials, and videos available that can help you learn more about linked lists in Java. Utilize these to enhance your understanding.

  6. Ask for help: Don't hesitate to reach out to communities like this Discord server for help if you get stuck or have any questions about linked lists in Java.

By following these steps, you'll be well on your way to mastering linked lists in Java!

spark onyx
#

okay so for context

#

this is a very common starter assignment

#

and its very often given well before you are able to do it easily

#

there are a few ways of teaching linked lists that aren't bad, but they require your curriculum to be a little special (you needed to learn interfaces)

#

but its obviously not impossible to grasp

#

the key property of a linked list is that it is either

#
  • empty
  • not empty
#

the way your teacher probably wants you to represent it is with a Node class

#
Node node = null;
#

if the Node is null that means it is empty

#

if the node is not null it is not empty

#
class Node {
    int head;
    Node tail;
}
#

when a linked list is not empty it means you have at least one piece of data

#

in this case lets assume all the data is in ints so you have some int at the "head" of the list

#

the "tail" is another Node

#

and thus might be empty or not empty itself

#

@cosmic hemlock Taking a break, ask questions now and i'll continue

cosmic hemlock
#

okay so the way i started doing it is like this

#

class Node {
Node next;

#

int data

spark onyx
#

yeah next/data, head/tail, first/rest

#

take your pick

cosmic hemlock
#

public Node(int data) {
this.data = data;
this.next = null;
}
}

spark onyx
#

okay lets pause

cosmic hemlock
#

alrighrt yea sure

spark onyx
#

so writing that here

#
class Node {
    int head;
    Node tail;

    Node(int head) {
        this.head = head;
        this.tail = null;
    }
}
#

if you were to say new Node(5) then head will be 5 and tail will be null

#

this means it will be a linked list with one element

#

so not empty -> empty

#

does that track?

cosmic hemlock
#

yea so first node will be pointed as head and tail

spark onyx
#

err

cosmic hemlock
#

since there is only one element

spark onyx
#

lets use our words

cosmic hemlock
#

or number

spark onyx
#

what does "pointed as head and tail" mean

cosmic hemlock
#

since there is only one element the tail and head will point to that number since its only one

spark onyx
#

no

#

there is only one element

#

the first node has that element at the head

#

and it points to a null or empty tail

#

you aren't wrong but i really want to make sure we are specific with language

#

okay so

#

a change I will make

#
class Node {
    int head;
    Node tail;

    Node(int head, Node tail) {
        this.head = head;
        this.tail = tail;
    }
}
#

lets add the tail to the constructor

cosmic hemlock
#

sorry if i sound silly its been only a month for me

spark onyx
#

we can have more than one constructor, but its easier to focus in

cosmic hemlock
spark onyx
#

so now lets say I did this

#
new Node(1, new Node(2, null));
#

this goes

#

not empty (1) -> not empty (2) -> empty

cosmic hemlock
#

1, new Node

#

2, null

spark onyx
#

yep

#

okay so next change we will make, it might feel pointless

#

we are going to mark head and tail as final

#
class Node {
    final int head;
    final Node tail;

    Node(int head, Node tail) {
        this.head = head;
        this.tail = tail;
    }
}
#

this means that after we make a Node, the values in there won't change

#

it doesn't do anything other than make it so that if you try to do this

#
Node n = new Node(1, null);
n.head = 6;
#

Java won't let you

#

this is just to stop you from making some common mistakes and getting confused

cosmic hemlock
#

will we make a append method prepend method and delete method and print method

spark onyx
#

sure

#

lets start with append

#

what should append take as arguments?

cosmic hemlock
#

int data

#

or

#

head

spark onyx
#

so the next part is made a little annoying by the choice to use null as empty

#

but

#
class Node {
    final int head;
    final Node tail;

    Node(int head, Node tail) {
        this.head = head;
        this.tail = tail;
    }

    static Node append(Node list, int data) {
         // ...
    }
}
#

now lets address the fact that it is static

#

normally, java lets you call methods on objects like this

cosmic hemlock
#

this looks more complicated

#

i usually just create another class linkedlist

spark onyx
#

i understand

#

hold with me

cosmic hemlock
#

holding

spark onyx
#
String s = "abc";
int len = s.length();
#

so in this code, what will the value of len be

#

not a trick question

cosmic hemlock
#

2\\\\\\\\

spark onyx
#

abc

#

1 2 3

#

3

#

its 3

cosmic hemlock
#

oh

#

for strings

#

it doesnt do that right

spark onyx
#

its not indexing

cosmic hemlock
#

oh this is lenght

#

sorry sorry

spark onyx
#

its just telling you the length

#

okay so now

#
String s = "";
int len = s.length();
#

what will len be

cosmic hemlock
#

null

spark onyx
#

0

#

it has zero characters

cosmic hemlock
#

or garabe

#

garabge

#

okay oay

spark onyx
#

nope, well defined

#

its just 0

#

now what will len be

#
String s = null;
int len = s.length();
cosmic hemlock
#

nullllllllllllllllll

spark onyx
#

nope

#

it will crash

#

if you try to do something like .length() and the thing on the left is null, you get a crash

cosmic hemlock
#

rage quit

spark onyx
#

it has no feelings but sure

#
class Node {
    final int head;
    final Node tail;

    Node(int head, Node tail) {
        this.head = head;
        this.tail = tail;
    }

    static Node append(Node list, int data) {
         // ...
    }
}
#

so now here

#

the reason we take in the node as an argument

#

and the reason it is static

#

is because we have that whole "empty is represented by null" thing

#

if you tried node.append(5) and node was null, it would crash

#

sorta make sense?

cosmic hemlock
distant wraithBOT
spark onyx
#

don't share your code

cosmic hemlock
#

but this is alot easier

spark onyx
#

we are doing it from scratch

cosmic hemlock
#

yea but the way your doing it is way out of my understanding

#

like im pretty new new

spark onyx
#

your way of doing it is way out of your understanding

#

everything is out of your understanding

cosmic hemlock
#

no its from my professor

spark onyx
#

thats why we are going through this

#

if you want your professor to teach you you should reach out to him

#

if you want me to teach you follow my instructions

cosmic hemlock
#

yea but instructions should be understandable

#

you dont skip a few steps

#

yk

#

you go from easy to hard

#

not hard then easy

#

its better for people to understand that way

mortal valley
#

do you understand the concept of "linked"?

cosmic hemlock
#

yea

spark onyx
#

im out

cosmic hemlock
#

but when it comes to the coding part i kinda get lost

#

with all the this == this

#

this = this

mortal valley
#

do you understand how classes work?

cosmic hemlock
#

if this blah blah

#

yea

bitter thicket
#

u get lost bc u skipped a hand full of steps

cosmic hemlock
#

i would be eaiser if there was a class

bitter thicket
#

and are packing practice with these steps

spark onyx
#

(steps which I am trying to make you do!!)

bitter thicket
#

the concept makes sense to u but that's about it. and hence u can't put it into actual code

dire prawn
#

I think school started this week.

cosmic hemlock
#

i watched a ton and you would make a class to not have it al lcrushed in one to make it more understanble

#

i just didnt get the way ethan did it

mortal valley
#

at its most basic, you can create a list using java class Node { Node next; Object data; }that much makes sense, yeah?

cosmic hemlock
#

well always make em private

#

thats good

bitter thicket
#

bc ethan tries to step back 10 steps and fill all the gaps in the knowledge u need to know to be really comfortable with ur assignment

mortal valley
bitter thicket
#

u dont have to follow his path. but then u need someone else to help u out

cosmic hemlock
#

i was praicting making a list with just ints

#

so i would do it int data

#

but yea

#

then i would do a construcotr

#

then after that make a new class called linkedlist whcih will include all the appends and all of that

mortal valley
mortal valley
#

i mean show us how a list would be made using Node objects

#

thats the class. how would you use that class to make a list?

cosmic hemlock
#

public Node(int data) {
this.data = data;
this.next = null;
}

#

this will be the construcotr

#

in that Node class

mortal valley
#

okay, but how would you make the list?

dire prawn
# spark onyx im out

This is why I'm an advocate of just giving them answers when they are this green. It hurts no one at this stage.

cosmic hemlock
#

put methods

#

i incude head, current, next

#

i make a new node head

mortal valley
cosmic hemlock
#

referene to the first node in the linked list

mortal valley
#

with only the class i gave you

#
Node node = new Node(5);```
#

theres the first node, just to get you started

cosmic hemlock
#

i understand how to code it

mortal valley
#

this is as basic as it can possibly get

cosmic hemlock
#

then learn how to do it in one class

mortal valley
#

this is the very root of it all

cosmic hemlock
#

i wasnt looking to jumping around

mortal valley
#

if you cant do this, you wont be able to make a linked list

#

so this is where it starts

#

linking data

cosmic hemlock
#

theres many ways but i wanted to start off with the basic root

bitter thicket
mortal valley
cosmic hemlock
#

i dont feel bad im just confused

#

welps gotta go

#

food time

#

thanks for the help guys

dire prawn
#

gatekeeping information because you think "it's for their own good" drives people away.

mortal valley
#

when you get back, link nodes together

cosmic hemlock
#

okay okay i will

#

thats the next chapter im reading actually on my book

#

its old its from 2004

mortal valley
cosmic hemlock
#

i just see vids and try to read

mortal valley