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

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Data Structures ADT List
Linked Lists Linear collections.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
LIST PROCESSING.
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
Stacks, Queues & Deques CSC212.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Iterators CS 367 – Introduction to Data Structures.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Information and Computer Sciences University of Hawaii, Manoa
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Graphs – Part II CS 367 – Introduction to Data Structures.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSCS-200 Data Structure and Algorithms Lecture
Linked Lists and Generics Written by J.J. Shepherd.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
Class 2 – Recursion on Lists
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Stacks and Queues Chapter 4.
Algorithm for deleting a node from a singly linked list
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Stack and Queue APURBO DATTA.
Java collections library
Priority Queue.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Chapter 4 Linked Lists
Programmazione I a.a. 2017/2018.
Top Ten Words that Almost Rhyme with “Peas”
MyList<T> It’s a generic type, <T> is a type parameter
Recursion.
Summary on linked lists
Data Structures ADT List
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Linked Lists [AJ 15].
Programming Abstractions
Recursive Linked List Operations
Data Structures ADT List
Linked Lists.
Recursive Objects Singly Linked Lists.
Queues Definition of a Queue Examples of Queues
Queues: Implemented using Linked Lists
Presentation transcript:

LINKED LIST’S EXAMPLES Salim Malakouti

Linked List? 523 Pointer Node ValuePointer

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);  }  …  }

Comprehension Questions

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

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

Algorithmic Questions

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

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

How to clear the Linked List? Question 2

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

How to find the size? Question 3

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

Size Recursive  /**  * Computes the size of the list but using a recursive function.  *   */  public int sizeRecursive() {  return sizeR(head);  }  /**  * The inner part of the recursive size  head   */  private int sizeR(Node head) {  if (head == null)  return 0;  else  return sizeR(head.getNext()) + 1;  }

How to get the last value? Question 4

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

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

How to append to lists? Question 5

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

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

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

How to find the ith element? Question 7

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;  }

How to remove a specific element? Question 8

Remove  /**  * This functions removes nodes in the list equal to the input. Only one at  * a time.  *  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();  }

How to reverse the list? Question 9

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);  }

How to insert in ith position? Question 10

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++;  }