1 / 30

Linked List’s Examples

Linked List’s Examples. Salim Malakouti. Linked List?. Value. Pointer. Pointer. 5. 2. 3. Node. 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) {

ouida
Download Presentation

Linked List’s Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked List’s Examples Salim Malakouti

  2. Linked List? Value Pointer Pointer 5 2 3 Node

  3. 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); • } • … • … • }

  4. ComprehensionQuestions

  5. Question 1 • If we just want to iterate over the list and print all values, which one is faster why? • Array • Linked List?

  6. Answer • Array • Because all elements are beside each other. There is no need for scanning the file.

  7. Algorithmic Questions

  8. Question 1 How to add a new value to the list?

  9. 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); • }

  10. Question 2 How to clear the Linked List?

  11. Clear • /** • * This function simply clears the list • */ • public void clear() { • setHead(null); • }

  12. Question 3 How to find the size?

  13. 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++; • } • returnsize; • }

  14. Size Recursive • /** • * Computes the size of the list but using a recursive function. • * • * @return • */ • public intsizeRecursive() { • return sizeR(head); • } • /** • * The inner part of the recursive size • * @param head • * @return • */ • private intsizeR(Node<T> head) { • if (head == null) • return 0; • else • return sizeR(head.getNext()) + 1; • }

  15. Question 4 How to get the last value?

  16. 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; • }

  17. 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()); • }

  18. Question 5 How to append to lists?

  19. Append • /** • * Append the input list to the end of this list. • * • * @param l • */ • publicvoidappend(LinkedList<T> l) { • Node<T> tail = getTail(); • if (tail == null) { • head = l.getHead(); • } else { • tail.setNext(l.getHead()); • }

  20. Question 6 How to check if a specific value is in the list?

  21. Has Value • /** • * This function checks to see if there is a node with value equal to the • * input. • * • * @param value • */ • public booleanhasValue(T value) { • Node<T> current = getHead(); • while (current != null) { • if (current.getValue().equals(value)) { • return true; • } • current = current.getNext(); • } • return false; • }

  22. Question 7 How to find the ith element?

  23. 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) • returncurrent.getValue(); • current = current.getNext(); • i++; • } • returnnull; • }

  24. Question 8 How to remove a specific element?

  25. 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(); • } • }

  26. Question 9 How to reverse the list?

  27. 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); • }

  28. Question 10 How to insert in ith position?

  29. 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++; • } • }

More Related