Академический Документы
Профессиональный Документы
Культура Документы
DS002L1
Definition of LinkedList?
A Linked List is a data structure that makes it easy
to rearrange data without having to move data in
memory.
It is a data structure composed of nodes, each node
holding some information and a reference to
another node in the list.
Definition of LinkedList?
a collection of nodes storing data and links to other
nodes
a sequential data structure, collection of items
accessible one after another beginning at the head
and ends at the tail.
Components of LinkedList
Links - pointers or references
- next link, next pointer
Data / Value fields
- the remaining filed of a node
Singly Linkedlist
- contain nodes which have a data field as well as a
next field, which points to the next node in the linked list.
- is a list that enables a program to move through the list in one
direction, which is usually from the front node of the list moving to the
end node of the list. It is a node that has a link only to its successor in
the sequence. The last node on the list can be recognized by the null
reference field.
Classification of Linked List
According to Number of Links
- An empty list is a list that contains no data records. This is usually the same
as saying that it has zero nodes. If sentinel nodes are being used, the list is usually said
to be empty when it has only sentinel nodes.
Hash linking
- The link fields need not be physically part of the nodes. If the data records
are stored in an array and referenced to by their indices, the link field may be stored
in a separate array with the same index as the data record.
Using LinkedList in Java
Implementation - A LinkedList is implemented using
the LinkedList class which is found in the java.util
package. So that at the start of your application,
you should import the class as follows:
import java.util.LinkedList;
Using LinkedList in Java
Creating a LinkedList – declare a LinkedList
variable, then, call one of the LinkedList constructors
to create an object.
LinkedList name= new LinkedList( );
Below is a statement that will hold string values in a
list
LinkedList<String> name=new
LinkedList<String>( );
Using LinkedList in Java
Methods:
Fundamental (Basic)
.add() - is the most basic operation to put / insert items into
the list. The add( ) method requires an item when called.
Syntax
<linkedlist>.add(element E) : adds an element to the end of the list
<linkedlist>.add(int index, element E) : adds an element to the
specific index of the list
Using LinkedList in Java
Methods:
Fundamental (Basic)
.remove() - is an operation that removes the first item from
the list. The remove( ) method requires an item when called.
Syntax
<linkedlist>.remove(int index) : removes an element in the
specified index
<linkedlist>.remove(element E) : removes the specified
element
Using LinkedList in Java
Methods:
Fundamental (Basic)
.set()
- is an operation to replace an item in a linked list with
another item. The new item is reflected in the list when the
change is effected.
Syntax
<linkedlist>.set(int index, element E) : replaces an element in
the specified index with
the specified element
Using LinkedList in Java
Methods:
Fundamental (Basic)
.get() – used to retrieve items from the list.
Syntax
<linkedlist>.get(int index) : retrieves the element in the
specified index
Using LinkedList in Java
Methods:
Specific
Methods for First Element
.addFirst() : inserts the specified item in the beginning of the list
.getFirst() : retrieves the first item in the list
: throws NoSuchElement – Exception, if the list is
empty.
peekFirst() : retrieves the first item in the list
: throws null, if the list is empty.
.removeFirst() : retrieves and deletes the first item in the list
: throws NoSuchElement – Exception, if the list is
empty.
.pollFirst() : retrieves and deletes the first item in the list
: throws null, if the list is empty.
Using LinkedList in Java
Methods:
Specific
Methods for Last Element
.addLast() : inserts the specified item in the end of the list
.getLast() : retrieves the last item in the list
: throws NoSuchElement – Exception, if the list is
empty.
peekLast() : retrives the last item in the list
: throws null, if the list is empty.
.removeLast() : retrieves and deletes the last item in the list
: throws NoSuchElement – Exception, if the list is
empty.
.pollLast() : retrieves and deletes the last item in the list
: throws null, if the list is empty.
Using LinkedList in Java
Methods:
Special
.size(): counts the number of elements in the list
.clear() : removes all elements in the list
.contains() : searches for an element in the list
Sample Program using LinkedList
//Program that will simulate a line in a Cafeteria
import java.util.*; //imports everything from util package, includes LinkedList and Scanner
public class Cafeteria{
public static void main(String[]args){
LinkedList<String> customers = new LinkedList<String>(); //list that will hold customers’ name
Scanner in = new Scanner(System.in); //used for users’ input
int choice=0; //variable holding the decision of the user.
do{
System.out.println(“DS Cafeteria System”); //displays the commands used for the system
System.out.println(“[1]Add Last Customer”); //adds customer at the end of the line
System.out.println(“[2]Remove First Customer”); //removes customer at the start of the line
System.out.println(“[3]Count Customers”); //counts the number of customers in the line
System.out.println(“[4]Exit”); //exits the program
Sample Program using LinkedList
System.out.print(“Enter choice”); //ask for users’ input
choice = in.nextInt(); //accept users’ input
switch(choice){ //handles the choice made by the user
case 1: //new customer came in
System.out.print(“Whats your name ma’am or sir?”); //ask for name of customer
String name = in.next(); //accept customers’ name
customers.addLast(name); //adds the name of the customer in the end of the list
break;
case 2: //serving customer in the line
System.out.print(“Good day ”+customers.removeFirst()+”!”); //greets the customer
//also removing him/her in the line
System.out.println(“Menu\n[1]Adobo : P30\n[2]Tinola : P35\n[3]Kare-Kare : P35\n”);
//display menu to the customer
Sample Program using LinkedList
System.out.print(“What is your order? ”); //ask customer’s order
int order = in.nextInt(); //accept customers choice
switch(order){ //handles the customer’s order
case 1: System.out.println(“You ordered Adobo, plase pay P30.”); break;
case 2: System.out.println(“You ordered Tinola, plase pay P35.”); break;
case 3: System.out.println(“You ordered Kare-Kare, plase pay P35.”); break;
default: System.out.println(“Your choice is not in the menu!”);
}
break;
case 3: //count the number of customers in the line
System.out.println(“There are “+customers.size()+” customer/s in the line.”);
//prompt the user with the number of customers
break;
Sample Program using LinkedList
case 4: //Prompt the user that he exited the system
System.out.println(“Thank you for using the System. The system will now exit.”);
break;
default: //handles invalid choice made by the user
System.out.println(“You have made an invalid choice, please choose again.”);
}
}
while(choice!=4); //repeats instructions if the user did not select “[4]Exit”.
}
}