Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.

Similar presentations


Presentation on theme: "LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer."— Presentation transcript:

1 LINKED LIST’S EXAMPLES Salim Malakouti

2 Linked List? 523 Pointer Node ValuePointer

3 Node Class  public class Node {  /**  * Data  */  private T value;  /**  * Pointer to the next Node  */  private Node next;  public Node(T value, Node next) {  this.setValue(value);  this.setNext(next);  }  …  }

4 Comprehension Questions

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

8 Algorithmic Questions

9 How to add a new value to the list? Question 1

10  /**  * This function simply adds a new node to the list by adding it to the  * beginning.  *  * @param value  */  public void add(T value) {  Node tmp = getHead();  setHead(new Node<>(value));  getHead().setNext(tmp);  }

11 How to clear the Linked List? Question 2

12 Clear  /**  * This function simply clears the list  */  public void clear() {  setHead(null);  }

13 How to find the size? Question 3

14 Size  /**  * Computes the size of the list.  *  * @return  */  public int size() {  Node current = getHead();  int size = 0;  while (current != null) {  current = current.getNext();  size++;  }  return size;  }

15 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 head) {  if (head == null)  return 0;  else  return sizeR(head.getNext()) + 1;  }

16 How to get the last value? Question 4

17 Get Tail  /**  * This function returns the last element.  *  * @return  */  public Node getTail() {  Node current = getHead();  while (current.getNext() != null) {  current = current.getNext();  }  return current;  }

18 Get Tail Recursive  public Node getTailRecursive() {  return tailR(head);  }  /**  * Inner function for finding tail recursively  * @param node  * @return  */  private Node tailR(Node node) {  if (node.getNext() == null)  return node;  return tailR(node.getNext());  }

19 How to append to lists? Question 5

20 Append  /**  * Append the input list to the end of this list.  *  * @param l  */  public void append(LinkedList l) {  Node tail = getTail();  if (tail == null) {  head = l.getHead();  } else {  tail.setNext(l.getHead());  } 

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

22 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 current = getHead();  while (current != null) {  if (current.getValue().equals(value)) {  return true;  }  current = current.getNext();  }  return false;  }

23 How to find the ith element? Question 7

24 Get ith  /**  * This function returns the ith item in the list.  */  public T get(int index) {  Node current = getHead();  int i = 0;  while (current != null) {  if (i == index)  return current.getValue();  current = current.getNext();  i++;  }  return null;  }

25 How to remove a specific element? Question 8

26 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 pre = null;  Node 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();  }

27 How to reverse the list? Question 9

28 Reverse  /**  * This function simply reverses the list  */  public void reverse() {  Node next = getHead();  Node pre = null;  Node tmp = null;  while (next != null) {  tmp = next;  next = next.getNext();  tmp.setNext(pre);  pre = tmp;  }  setHead(tmp);  }

29 How to insert in ith position? Question 10

30 Insert in ith position  public void insert(int index, T value) {  Node current = getHead();  Node 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++;  }


Download ppt "LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer."

Similar presentations


Ads by Google