0% found this document useful (0 votes)
60 views34 pages

Linkedlist: Writing Generic Llist Additional Options To Llist Linkedlist Collection Class Binary Search

The document discusses linked lists and the LinkedList collection class in Java. It provides examples of writing generic linked list (LLNode and LList) classes that can store any object type. It also discusses differences between single linked lists, double linked lists, and the LinkedList collection class, including methods for adding, searching, and deleting nodes.

Uploaded by

Felix Espino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views34 pages

Linkedlist: Writing Generic Llist Additional Options To Llist Linkedlist Collection Class Binary Search

The document discusses linked lists and the LinkedList collection class in Java. It provides examples of writing generic linked list (LLNode and LList) classes that can store any object type. It also discusses differences between single linked lists, double linked lists, and the LinkedList collection class, including methods for adding, searching, and deleting nodes.

Uploaded by

Felix Espino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

MODULE 4:

LINKEDLIST

Writing Generic LList


Additional Options to LList
LinkedList Collection Class
Binary Search

1
We wrote a Linked List that holds a String only

WRITING A GENERIC LLIST

2
Previous Linked List Implementation

• We wrote LLNode class, and LList class to


use String data…and then for the
applications in our in-lab Labs, we had to
change the data type in these classes to
int or char to be able to write our solution
• WHAT if we had written the classes
generically to start with? What would this
look like…..

3
General Note

• Use T to represent the Class you will use


generically
• Need to make sure any method you use,
that T class can handle that method
• Need to rewrite LLNode and LLIist classes

4
LLNode - Declaration and
Constructors
• You need to make a generic class
public class GenericLLNode<T> {
private T data;
    private GenericLLNode<T> next;

    public GenericLLNode() {
        this.data = null;
        this.next = null;
    }
    public GenericLLNode (T newData) {
        this.data = newData;
        this.next = null;
    }

5
LLNode - Methods

• Everywhere there was a String, we update to T


    public void updateNode (GenericLLNode<T> nextOne) {
        this.next = nextOne;
    }
    public String toString () {
        return  this.data.toString();
    }
    public GenericLLNode<T> getNext() {
        return this.next;
    }
    public T getData() {
        return this.data;
    }
}

6
LList - Declaration and Constructor

public class GenericLList<T> {


    private GenericLLNode<T> head;
public GenericLList() {
        head = null;
}
public void addAtHead(T data) {
GenericLLNode<T> temp = new GenericLLNode<T>(data);
temp.updateNode(head);
head = temp;
}
public void display() {
display(head);
}
private void display(GenericLLNode<T> node) {
if(node != null) {
System.out.println(node.toString());
display(node.getNext());
}
}
}

7
Example

public static void main (String [] argS) {


    GenericLList<String> list = new GenericLList<String>();
    list.addAtHead("Linda");
    list.addAtHead("George");
    list.addAtHead("Henry");
    System.out.println("\nThe String list is ");
    list.display();
}
OUTPUT:
The String list is 
Henry
George
Linda

8
Example Continued

• If deleteAtHead() was also implemented


String removedOne = list.deleteAtHead();
System.out.println("\nAfter delete, the String list is ");
list.display();
System.out.println("\nThe one deleted is..." + removedOne);

OUTPUT:
After delete, the String list is
George
Linda
The one deleted is...Henry

9
Example 2

• Now we can use the same code for a different object


public static void main (String [] argS) {
GenericLList<Float> flist = new GenericLList();
flist.addAtHead(22.2f); // note autoboxing
flist.addAtHead(33.3f);
flist.addAtHead(44.4f);
System.out.println("\nThe Float list is ");
list.display();
}
OUTPUT:
The Float list is
44.4
33.3
22.2

10
Example 2 Continued

Float fRemovedOne = flist.deleteAtHead();


System.out.println("\nAfter delete, the Float list is ");
flist.display();
System.out.println("\nThe one deleted is..." + fRemovedOne);
OUTPUT:
After delete, the Float list is
33.3
22.2
The one deleted is...44.4

11
Extra Practice

• Add isEqual() method to this example…


• Add searchAndDelete method
• Add deleteAtHead method

12
ADDITIONAL OPTIONS TO
LLIST
13
Single Linked list - Traditional

• Data members - LList class contains a head


(reference to first LLNode in the list); LLNode
contains reference to next node in list
• add method – at head O(1)
• search method – sequential through list O(n)
• sort method – doesn’t really work – need
addInOrder which would have O(n)
• delete method – at head O(1)
• delete method of particular node – O(n)

14
Single Linked List – Options for
Improvements
• Data member – tail – reference to last
node in the list
• Allows add at tail – O(1)
• All other methods same
• Data member - numItems in list
• Can provide useful information without having
to traverse list to count

15
Double Linked List

• LLNode would contain links to both


previous and next nodes in the list
• LList still contains head – reference to first
node in the list, usually also contains a tail
– reference to the last node in the list
• What would add at head or add at tail look
like?

16
DOUBLY LINKED LIST

17
Node Class

public class DLLNode {


private String data;
private DLLNode next;
private DLLNode prev;

public DLLNode (String newData) {


this.data = (newData);
this.next = null;
this.prev = null;
}

public void updateNodeNext (DLLNode nextOne) {


this.next = nextOne;
}

public void updateNodePrev (DLLNode prevOne) {


this.prev = prevOne;
}
public String toString () { return this.data; }
public DLLNode getNext() { return this.next; }
public DLLNode getPrev() { return this.prev; }
}

18
DLList Class

public class DLList {


private DLLNode head;
private DLLNode tail;

public DLList() {
head = null;
tail=null;
}

public void addAtHead (String newData) {


DLLNode newNode = new DLLNode (newData);
if (head != null)
head.updateNodePrev(newNode);
newNode.updateNodeNext(head);
head = newNode;
if (tail == null)
tail = newNode;
}

19
DLList Class (2)

public void displayFromHead() {


DLLNode temp = head;
while (temp != null) {
System.out.println (temp);
temp = temp.getNext();
}
}

public void displayFromTail() {


DLLNode temp = tail;
while (temp != null) {
System.out.println (temp);
temp = temp.getPrev();
}
}
}

20
Use DLList in Main

public static void main(String[] args) {


DLList list = new DLList();
list.addAtHead("Linda");
list.addAtHead("George");
System.out.println("The list from head is");
list.displayFromHead();
System.out.println("The list from tail is");
list.displayFromTail();
}
OUTPUT:
The list from head is
George
Linda
The list from tail is
Linda
George

21
Delete a Node

• What would delete look like?

22
Search and Delete

public boolean searchAndDelete (String oneToDelete) {


if (head == null)
return false;
DLLNode current = head;
// walk through list, compare each node with search value
while (current!= null && !(current.toString().equals(oneToDelete))) {
current = current.getNext();
}
if (current != null) { // must have found it
if (head == current && tail == current){ // deleting only item in list
head = null;
tail = null;
} else if (current == head) { // deleting first item
head = current.getNext();
current.getNext().updateNodePrev(null);
} else if (current == tail) { // deleting last item
tail = current.getPrev();
current.getPrev().updateNodeNext(null);
} else {
current.getPrev().updateNodeNext(current.getNext());
current.getNext().updateNodePrev(current.getPrev());
}
return true;
}
else
return false; // didn’t find it }
}

23
Practice…..write these methods

• public boolean addAtTail(String s)


• public DLLNode deleteAtHead()
• public DLLNode deleteAtTail()
• public boolean addInOrder(String s)

24
LINKEDLIST COLLECTION
CLASS
25
LinkedList Collection Class

• All of the operations perform as could be


expected for a doubly-linked list. Operations
that index into the list will traverse the list
from the beginning or the end, whichever is
closer to the specified index.
• Declaration: LinkedList<E>; where E is the
class type of element to be stored in the list.

26
Collection Methods

Modifier Method Description


and Type
N/A LinkedList() Constructs an empty list.
boolean add​(E e) Appends the specified element to the end
of this list.
void add​(int index, E element) Inserts the specified element at
the specified position in this list.
boolean contains​(Object o) Returns true if this list contains
the specified element.
int size() Returns the number of elements
in this list.
E get​(int index) Returns the element at the
specified position in this list.

27
Collection Methods (2)

Modifier Method Description


and Type
int indexOf​(Object o) Returns the index of the first occurrence of
the specified element in this list, or -1 if this
list does not contain the element.
E remove() Retrieves and removes the head (first
element) of this list.
E remove​(int index) Removes the element at the specified
position in this list.
boolean remove​(Object o) Removes the first occurrence of the
specified element from this list, if it is
present.
void addFirst​(E e) Inserts the specified element at the
beginning of this list.
void addLast​(E e) Appends the specified element to the end of
this list.
28
Collection Methods (3)

Modifier Method Description


and Type
E removeFirst() Removes and returns the first
element from this list.
boolean removeFirstOccurrence​(Object o) Removes the first occurrence of
the specified element in this list
(when traversing the list from
head to tail).
E removeLast() Removes and returns the last
element from this list.
boolean isEmpty() Returns true if this list contains
no elements.

29
Collection Class Example

public static void main (String [] argS) {


LinkedList<String> names = new LinkedList<String>();
names.add(“Linda”);
names.add(“George”);
System.out.println (“The list is currently:”);
System.out.println (names);
}
OUTPUT:
The list is currently:
[Linda, George]

30
Collection Class Example (2)

System.out.println (“The list is now:”);


for (int i=0; i<names.size(); i++)
System.out.println (names.get(i));
OUTPUT:
The list is now:
Linda
George

31
Collection Class Example (3)

• If you have a method to execute on each object


System.out.println (“The list is also:”);
for (String name:names)
System.out.println (name);

OUTPUT:
The list is also:
Linda
George

32
QUESTIONS?

33
Release info

• Release: 10/06/2020
• Author: Melissa Sienkiewicz
• Credits/References:
• Linda Crane
• Rex Woollard
• Deitel, P. | Deitel, H.. (2014). Java How To
Program (Early Objects) (10th Edition)

34

You might also like