Linked List’s Examples
description
Transcript of Linked List’s Examples
![Page 1: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/1.jpg)
LINKED LIST’S EXAMPLESSalim Malakouti
![Page 2: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/2.jpg)
Linked List?
52 3
Pointer
Node
Value Pointer
![Page 3: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/3.jpg)
Node Class public class Node<T> { /** * Data */ private T value; /** * Pointer to the next Node */ private Node<T> next;
public Node(T value, Node<T> next) { this.setValue(value); this.setNext(next);
} … … }
![Page 4: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/4.jpg)
Comprehension Questions
![Page 5: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/5.jpg)
Question 1 If we just want to iterate over the list
and print all values, which one is faster why? Array Linked List?
![Page 6: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/6.jpg)
Answer Array
Because all elements are beside each other. There is no need for scanning the file.
![Page 7: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/7.jpg)
![Page 8: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/8.jpg)
Algorithmic Questions
![Page 9: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/9.jpg)
How to add a new value to the list?
Question 1
![Page 10: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/10.jpg)
Question 1 /** * This function simply adds a new node to the list by
adding it to the * beginning. * * @param value */ public void add(T value) { Node<T> tmp = getHead(); setHead(new Node<>(value)); getHead().setNext(tmp); }
![Page 11: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/11.jpg)
How to clear the Linked List?
Question 2
![Page 12: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/12.jpg)
Clear /** * This function simply clears the list */ public void clear() { setHead(null); }
![Page 13: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/13.jpg)
How to find the size?
Question 3
![Page 14: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/14.jpg)
Size /** * Computes the size of the list. * * @return */ public int size() { Node<T> current = getHead(); int size = 0; while (current != null) { current = current.getNext(); size++; } return size; }
![Page 15: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/15.jpg)
Size Recursive /** * Computes the size of the list but using a recursive function. * * @return */ public int sizeRecursive() { return sizeR(head); } /** * The inner part of the recursive size * @param head * @return */ private int sizeR(Node<T> head) { if (head == null) return 0; else return sizeR(head.getNext()) + 1; }
![Page 16: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/16.jpg)
How to get the last value?
Question 4
![Page 17: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/17.jpg)
Get Tail /** * This function returns the last element. * * @return */ public Node<T> getTail() { Node<T> current = getHead(); while (current.getNext() != null) { current = current.getNext(); } return current; }
![Page 18: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/18.jpg)
Get Tail Recursive public Node<T> getTailRecursive() { return tailR(head); } /** * Inner function for finding tail recursively * @param node * @return */ private Node<T> tailR(Node<T> node) { if (node.getNext() == null) return node; return tailR(node.getNext()); }
![Page 19: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/19.jpg)
How to append to lists?
Question 5
![Page 20: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/20.jpg)
Append /** * Append the input list to the end of this list. * * @param l */ public void append(LinkedList<T> l) { Node<T> tail = getTail(); if (tail == null) { head = l.getHead(); } else { tail.setNext(l.getHead()); }
![Page 21: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/21.jpg)
How to check if a specific value is in the list?
Question 6
![Page 22: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/22.jpg)
Has Value /** * This function checks to see if there is a node with value equal to the * input. * * @param value */ public boolean hasValue(T value) { Node<T> current = getHead(); while (current != null) { if (current.getValue().equals(value)) { return true; } current = current.getNext(); } return false; }
![Page 23: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/23.jpg)
How to find the ith element?
Question 7
![Page 24: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/24.jpg)
Get ith /** * This function returns the ith item in the list. */ public T get(int index) { Node<T> current = getHead(); int i = 0; while (current != null) { if (i == index) return current.getValue(); current = current.getNext(); i++; } return null; }
![Page 25: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/25.jpg)
How to remove a specific element?
Question 8
![Page 26: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/26.jpg)
Remove /** * This functions removes nodes in the list equal to the input. Only one at * a time. * * @param value */ public void remove(T value) { Node<T> pre = null; Node<T> current = getHead(); while (current != null) { if (current.getValue().equals(value)) { if (pre != null) { pre.setNext(current.getNext()); } else { setHead(current.getNext()); } break; } pre = current; current = current.getNext(); } }
![Page 27: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/27.jpg)
How to reverse the list?
Question 9
![Page 28: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/28.jpg)
Reverse /** * This function simply reverses the list */ public void reverse() { Node<T> next = getHead(); Node<T> pre = null; Node<T> tmp = null; while (next != null) { tmp = next; next = next.getNext(); tmp.setNext(pre); pre = tmp; } setHead(tmp); }
![Page 29: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/29.jpg)
How to insert in ith position?
Question 10
![Page 30: Linked List’s Examples](https://reader036.fdocuments.us/reader036/viewer/2022062305/5681668d550346895dda56a9/html5/thumbnails/30.jpg)
Insert in ith position public void insert(int index, T value) { Node<T> current = getHead(); Node<T> pre = null; int i = 0; while (current != null) { if (i == index) { if (pre != null) { pre.setNext(new Node(value, current)); } else { setHead(new Node(value, current)); } } pre = current; current = current.getNext(); i++; } }