MyList<T> It’s a generic type, <T> is a type parameter

Slides:



Advertisements
Similar presentations
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Advertisements

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.
List Representation - Chain Fall 2010 CSE, POSTECH.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Singly Linked Lists Representation Space Analysis Creation and Insertion Traversal Search Deletion.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
List Interface CS java.util.List Interface and its Implementers CS340 2.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
10/7/2015IT 1791 Java’s World in UML Object Shape {abstract} This is done implicitly Shape {abstract} - ShapeName: String # setShapeName(newShapeName:
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Information and Computer Sciences University of Hawaii, Manoa
“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter Objectives  The List interface  Implement lists based on arrays  Learn about List applications CS340 1.
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.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
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 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Single-Linked Lists.
4. Linked Lists.
Tirgul 12 Data Structures (2) 1.
Singly Linked Lists.
Linked Lists Chapter 5 (continued)
Lecture No.03 Data Structures Dr. Sohail Aslam
Doubly Linked List Review - We are writing this code
Revision Based on: Linked Lists Revision Based on:
Programming Abstractions
Java collections library
LinkedList Class.
public class StrangeObject { String name; StrangeObject other; }
Templates II CMSC 202.
Top Ten Words that Almost Rhyme with “Peas”
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Data Type (how to view it)
(Java Collections Framework) AbstractSequentialList
Lecture 26: Advanced List Implementation
Need for Iterators O(n2) this is a common application
Linked Lists [AJ 15].
Lecture 7: Linked List Basics reading: 16.2
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
ADT list.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
COSC 1030 Section 8 List.
ArrayLists 22-Feb-19.
slides created by Marty Stepp
Singly Linked Lists Representation Space Analysis
Recursive Objects Singly Linked Lists.
ArrayList.
Building Java Programs
Programming II (CS300) Chapter 07: Linked Lists
TCSS 143, Autumn 2004 Lecture Notes
slides created by Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
CSE 143 Lecture 21 Advanced List Implementation
Linked Lists Chapter 5 (continued)
Presentation transcript:

MyList<T> It’s a generic type, <T> is a type parameter ArrayList int size(); T get(int i); T set(int i, T item); int indexOf(T item); void add(T item); void add(int i, T item); T remove(int i); Object[] toArray(); <K> K[] toArray(K[] a); A i = 2  [2] [0] [1] B [2] C [3] D [4] E [5] F [6] G [7] H [8] [9] 11/11/2018 IT 179

MyList<T> The internal information and structures are protected. Single Linked List MyList<T> [6] int size(); T get(int i); T set(int i, T item); int indexOf(T item); void add(T item); void add(int i, T item); T remove(int i); Object[] toArray(); <K> K[] toArray(K[] a); G i = 2  [2] [1] B [5] F [3] A D [0] C [4] E [2] H [7] The internal information and structures are protected. 11/11/2018 IT 179

L1 L2 L3 L4 11/11/2018 IT 179 MyArrayList<Integer> data size = 5 void remove(int i) {….} void add(int I, Integer item) {…} Integer max() {…}; … “Sean” 20 “Leon” 45 “Tom” 5 “Mary” 14 MyArrayList<String> data size = 4 void remove(int i) {….} void add(int I, String item) {…} Stringr max() {…}; … L1 L2 L3 L4 24 MySLinkedList<String> head size = 2 void remove(int i) {….} void add(int I, String item) {…} String max() {…}; … data next “ISU” “IT179” data next 11 270 228 MySLinkedList<Integer> head size = 3 void remove(int i) {….} void add(int I, Integer item) {…} Integer max() {…}; … data next data next data next 11/11/2018 IT 179

What do we need for single linked lists? Data structures head tail (optional) A B C D get(0) get(2) get(3) remove E insert Operations 11/11/2018 IT 179

Internal classes (define a class inside a class) public class A { private .... Public .... } public class B { private .... Public .... } private class C { ..... } public class ClassA<T> { T head; } public class ClassB<T,E> { T t; E e; } public class B<T> { private .... Public .... C<T> } private class C<E> { E ... T ... } IT 179 11/11/2018

Using a generic inner class for nodes public class MySLinkedList <T> implements MyList<T>{ /***** This is an inner class for internal nodes ********/ private class Node<E> { private E data; private Node<E> next; private Node(E data, Node<E> next) { // Construct a node pointing to next this.data = data; this.next = next; } /**** This is the end of the inner class Node<E> ******/ private Node<T> head; private int size; public MySLinkedList() { head = null; size = 0; ..... .... x y 11/11/2018 IT 179

Using a non-generic inner class for nodes public class MySLinkedList <T> implements MyList<T>{ /***** This is an inner class for internal nodes ********/ private class Node { private T data; private Node next; private Node(T data, Node next) { // Construct a node pointing to next this.data = data; this.next = next; } /**** This is the end of the inner class Node<E> ******/ private Node head; private int size; public MySLinkedList() { head = null; size = 0; ..... .... x y 11/11/2018 IT 179

There is no index, we have to count. /** * @see myUtil.MyList#get(int) */ public T get(int at) { if (at < 0 || at >= size) throw new IndexOutOfBoundsException("IndexOutOfBoundsException: "); Node<T> theNode = head; int count = 0; while (count < at) { theNode = theNode.next; count++; } return theNode.data; theNode X X X X head 11/11/2018 IT 179

There is no index, we have to count. ..... public int indexOf(T data) { if (head == null) return -1; // nothing there Node<T> theNode = head; int count = 0; while (!theNode.data.equals(data)) { theNode = theNode.next; if (theNode == null) return -1; count++; } return count; That’s why equals is important 11/11/2018 IT 179

* @see myUtil.MyList#set(int, java.lang.Object) */ ..... /** * @see myUtil.MyList#set(int, java.lang.Object) */ public T set(int at, T data) { // return the previous value; if (at < 0 || at >= size) throw new IndexOutOfBoundsException(); Node<T> theNode = head; int count = 0; while (count < at) { theNode = theNode.next; count++; } T oldValue = theNode.data; theNode.data = data; return oldValue; 11/11/2018 IT 179

Append (alternative, not required by MyList<T>) ..... public void append(T item) { // append item to the tail Node<T> newNode = new Node<T>(item, null); size ++; if (head == null) { head = newNode; return; } Node<T> tail = head; while (tail.next != null) tail = tail.next; tail.next = newNode; We have to find the tail. tail X X X null head 11/11/2018 IT 179

Using private method /** * @see myUtil.MyList#add(java.lang.Object) */ public void add(T item) { // append item to the tail add(size, item); } /** * @see myUtil.MyList#add(int, java.lang.Object) */ public void add(int at, T item) { // insert item at at if (at < 0 || at > size) // no can do throw new IndexOutOfBoundsException("Can't add at "+at); if (at == 0) { head = new Node<T>(item, head); size++; } else { addAfter(getNode(at-1),item); } private void addAfter(Node<T> node, T item) { node.next = new Node<T>(item, node.next); size++; } 11/11/2018 IT 179

Private getNode. /** * @see myUtil.MyList#get(int) */ private Node<T> getNode(int at) { if (at < 0 || at >= size) throw new IndexOutOfBoundsException("IndeOutOfBoundsException: "); Node<T> theNode = head; int count = 0; while (count < at) { theNode = theNode.next; count++; } return theNode; theNode X X X X return theNode; head 11/11/2018 IT 179

remove We need to know the previous node /** * @see myUtil.MyList#remove(int) */ public T remove(int at) { if (at < 0 || at >= size) throw new IndexOutOfBoundsException("IndeOutOfBoundsException: "); if (at == 0) { T temp = head.data; head = head.next; size--; return temp; } return removeNext(getNode(at-1)); private T removeNext(Node<T> node) { T temp = node.next.data; node.next = node.next.next; 11/11/2018 IT 179

toArray /** * @see myUtil.MyList#toArray() */ public Object[] toArray() { Object[] a = new Object[size]; Node<T> currentNode = head; for (int i = 0; i<a.length; i++) { a[i] = currentNode.data; currentNode = currentNode.next; } return a; 11/11/2018 IT 179

toArray public <K> K[] toArray(K[] a) { if (a == null) throw new NullPointerException("Argument can't be null"); if (a.length < size) a = (K[]) new Object[size]; Node<T> node = head; for (int i = 0; i<a.length ; i++) { if (i < size) { a[i] = (K) node.data; node = node.next; } else{ a[i] = null; return a; 11/11/2018 IT 179

* @see java.lang.Object#toString() */ public String toString() { /** * @see java.lang.Object#toString() */ public String toString() { Node<T> c=head; String outString="["; while (c != null) { outString += c.data.toString()+","; c = c.next; } return outString.substring(0,outString.length()-1)+"]"; 11/11/2018 IT 179