- #1
Chromium
- 56
- 0
Hey Everyone,
I recently got done learning about Linked Lists (well, actually only singly-linked lists) in my computer science class. It’s still pretty new to me, but I really want to make sure I understand this concept correctly. So I’ll present my knowledge about it, and hopefully, if there is a flaw or something incomplete someone will let me know. Feel free to give constructive criticism.
Again, my code will be Java-ish, and also I don't know if this is limited to Java, but I guess when you build a generic class, putting the <E> is what makes it generic. I'm not really sure. Thanks again!
Okay, so really a Linked List is made up of one field, the node reference commonly known as head.
public class LinkedList<E>
{
The head of the linked list is not a node, but only a node reference (note I did not use the keyword new, so no new object has been created).
LinkedList methods normally include methods to do the following:
1. Add a node to the front
2. Add a node to the back
3. Removing a node
4. Returning the size of the entire list
5. Clearing the list
Now, inside this LinkedList class there are two inner classes, a Node class and an Iterator class. Nodes are what make up a linked list, and they have two fields:
1. A data type (String, Integer, etc…)
2. A node reference that points to either the next node in the list, or null (implying that it is the last node in the list).
private class Node<E>
{
Next, we have the Iterator class, which provides a way to go through the list in sequential order, and “grab” items (nodes) out of the list. An Iterator object also contains two fields:
1. A linked list
2. A node reference to keep track of iterating through the list.
public class Iterator
{
Iterator class methods normally include two methods
1. A hasNext() method, which returns true if there is another node in the element, and false if there isn’t.
2. A Next() method, which returns the next object in the list.
I've provided a visual representation of a singly-linked list.
head nodeReference --> Node 1 (String, next nodeReference)--> Node 2 (String, next nodeReference)-->null
I recently got done learning about Linked Lists (well, actually only singly-linked lists) in my computer science class. It’s still pretty new to me, but I really want to make sure I understand this concept correctly. So I’ll present my knowledge about it, and hopefully, if there is a flaw or something incomplete someone will let me know. Feel free to give constructive criticism.
Again, my code will be Java-ish, and also I don't know if this is limited to Java, but I guess when you build a generic class, putting the <E> is what makes it generic. I'm not really sure. Thanks again!
Okay, so really a Linked List is made up of one field, the node reference commonly known as head.
public class LinkedList<E>
{
private Node<E> head;
}The head of the linked list is not a node, but only a node reference (note I did not use the keyword new, so no new object has been created).
LinkedList methods normally include methods to do the following:
1. Add a node to the front
2. Add a node to the back
3. Removing a node
4. Returning the size of the entire list
5. Clearing the list
Now, inside this LinkedList class there are two inner classes, a Node class and an Iterator class. Nodes are what make up a linked list, and they have two fields:
1. A data type (String, Integer, etc…)
2. A node reference that points to either the next node in the list, or null (implying that it is the last node in the list).
private class Node<E>
{
public ObjectType objectName;
public Node<E> next;
}public Node<E> next;
Next, we have the Iterator class, which provides a way to go through the list in sequential order, and “grab” items (nodes) out of the list. An Iterator object also contains two fields:
1. A linked list
2. A node reference to keep track of iterating through the list.
public class Iterator
{
private LinkedList<E> list;
private Node<E> currentPosition;
}private Node<E> currentPosition;
Iterator class methods normally include two methods
1. A hasNext() method, which returns true if there is another node in the element, and false if there isn’t.
2. A Next() method, which returns the next object in the list.
I've provided a visual representation of a singly-linked list.
head nodeReference --> Node 1 (String, next nodeReference)--> Node 2 (String, next nodeReference)-->null