#any tips on how i can learn a linked List (data structure)
1 messages ยท Page 1 of 1 (latest)
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>.
To learn about linked lists in Java, there are a few steps you can take:
-
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.
-
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.
-
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.
-
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.
-
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.
-
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!
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
okay so the way i started doing it is like this
class Node {
Node next;
int data
public Node(int data) {
this.data = data;
this.next = null;
}
}
okay lets pause
alrighrt yea sure
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?
yea so first node will be pointed as head and tail
err
since there is only one element
lets use our words
or number
what does "pointed as head and tail" mean
since there is only one element the tail and head will point to that number since its only one
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
sorry if i sound silly its been only a month for me
we can have more than one constructor, but its easier to focus in
okay tail
so now lets say I did this
new Node(1, new Node(2, null));
this goes
not empty (1) -> not empty (2) -> empty
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
will we make a append method prepend method and delete method and print method
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
holding
String s = "abc";
int len = s.length();
so in this code, what will the value of len be
not a trick question
2\\\\\\\\
its not indexing
its just telling you the length
okay so now
String s = "";
int len = s.length();
what will len be
null
nope, well defined
its just 0
now what will len be
String s = null;
int len = s.length();
nullllllllllllllllll
nope
it will crash
if you try to do something like .length() and the thing on the left is null, you get a crash
rage quit
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?
I uploaded your attachments as Gist.
don't share your code
but this is alot easier
we are doing it from scratch
yea but the way your doing it is way out of my understanding
like im pretty new new
your way of doing it is way out of your understanding
everything is out of your understanding
no its from my professor
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
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
do you understand the concept of "linked"?
yea
im out
but when it comes to the coding part i kinda get lost
with all the this == this
this = this
do you understand how classes work?
u get lost bc u skipped a hand full of steps
i would be eaiser if there was a class
and are packing practice with these steps
(steps which I am trying to make you do!!)
the concept makes sense to u but that's about it. and hence u can't put it into actual code
I think school started this week.
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
at its most basic, you can create a list using java class Node { Node next; Object data; }that much makes sense, yeah?
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
but you understand how to make a list using a class like that, yeah?
u dont have to follow his path. but then u need someone else to help u out
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
could you show us, just so we're on the same page?
just like this
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?
public Node(int data) {
this.data = data;
this.next = null;
}
this will be the construcotr
in that Node class
okay, but how would you make the list?
This is why I'm an advocate of just giving them answers when they are this green. It hurts no one at this stage.
go make a class LinkedList
put methods
i incude head, current, next
i make a new node head
with just the Node class
referene to the first node in the linked list
with only the class i gave you
Node node = new Node(5);```
theres the first node, just to get you started
well im starting off with the basics
i understand how to code it
this is as basic as it can possibly get
then learn how to do it in one class
this is the very root of it all
i wasnt looking to jumping around
if you cant do this, you wont be able to make a linked list
so this is where it starts
linking data
theres many ways but i wanted to start off with the basic root
ur not helping the convo and only causing OP to feel bad. please hold urself back and write such comments in another channel, if at all. thank you
its okay dont worry
this is the root. this is how linked data structures work
i dont feel bad im just confused
welps gotta go
food time
thanks for the help guys
gatekeeping information because you think "it's for their own good" drives people away.
true
when you get back, link nodes together
okay okay i will
thats the next chapter im reading actually on my book
its old its from 2004
this is the first node, you gotta link other nodes
i just see vids and try to read
its been the same since the beginning of time basically