The first and last
We have two fields in this class, they are called the first and the last.
Constructors and Definition of the class
As we discussed, we only have two fields in this class that make the class unique: the first and the last. In code it looks as follows:
public class MyLinkedList{
Node first, last;
public MyLinkedList(){
first = last = null;
}
}
/**
* isEmpty()
* The method will check if the list is empty
* @returns true if the list is empty, false otherwise
**/
public boolean isEmpty(){
return first == null;
}
/**
* add
* The method will take a String s as a parameter
* and it will add it to the end of the list
* @param s, a String desired to be added to the list
**/
public void add(String s){
Node n = new Node(s);
if(isEmpty()){
first = last = n;
}
else{
last.next = n;
last = n;
}
}
/**
* Size
* The method will count how many nodes are
* in the list.
* @returns the total number of nodes in the list
**/
public int size(){
int total = 0;
Node dummy = first;
while(dummy != null){
total++;
dummy = dummy.next;
}
return total;
}
/**
* find
* The method will check for every item on the Linked List
* if a given target is located
* @param t a String which value need to check in the list
* @returns true if the target was found in the list,
* false otherwise
**/
/**
* countOccurrences:
* The method will take a target, a String, as a parameter,
* and it will count and return the total number of times
* the target appears in the list.
* @param s, a String interested to know how many times
* appears in the list
* @returns the total times the String s appear in the list
*/
ACM CCECC
[Operations on Linked Lists]
SDF-10. Create simple programs that include each of the following data structures: lists, stacks, queues, hash tables, graphs, and trees.
CS2
[Operations on Linked Lists]
130.422.c.3.h Identify and use a list object data structure to traverse, search, insert, and delete data.
Structured data types available in the chosen programming language like sequences (e.g., arrays, lists), associative containers (e.g., dictionaries, maps), others (e.g., sets, tuples) and when and how to use them.
Standard abstract data types such as lists, stacks, queues, sets, and maps/dictionaries [Shared with: AL]
Lists: Single and Doubly Linked Lists