Java linked list.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
Linear Lists – Array Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
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)
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
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 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Grade 12 Computer Studies HG
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Introduction to Data Structures and Algorithms
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Chapter 5 Linked Lists II
Linked Structures, LinkedStack
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
List Interface and Linked List Mrs. Furman March 25, 2010.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 - Aggregation - UML diagram - Self-Referential Classes - Generisity.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
CSCS-200 Data Structure and Algorithms Lecture
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard.
Linked Data Structures
CHAPTER 4: Linked Structures
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Computing with C# and the .NET Framework
Stack and Queue APURBO DATTA.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Chapter 4 Unordered List.
Data Structures ADT List
Lecture 14 Linked Lists CSE /26/2018.
Introduction to Algorithms and Data Structures
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Recursive Objects Singly Linked Lists.
Tree.
Presentation transcript:

Java linked list

Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example of this is to use arrays, but when you don’t know the amount of information to be stored we need a dynamic data structure. ▪ One option for us is to use a linked list. A linked list works by creating a collection of objects (nodes) which both carry the data we want to store and a reference to the next node in the list. ▪ There is more than one type of a linked list. Some different type of linked list are shown below: 1. Singly linked list. Root node links one way through all the nodes. Last node links to NULL. 2. Doubly linked list. Every nodes stores a reference to its previous node as well as its next. Last node links to NULL.

Java linked list, cont. 3. Circular linked list. Circular linked list have a reference to one node which is the tail node and all the nodes are linked together in one direction forming a circle. ▪ A singly linked list is a linear data structure where each element (node) is a separate object. Tail Field nextNode references a Node< T > object, an object of the same <T> class . Node< T > data nextNode Field data references the object of the <T> class .

Class List - definitions Class List represents a singly linked list. Each element ( we will call it a node ) of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to null. The entry point into a linked list is called the head of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference. The number of nodes in a list is not fixed and can grow and shrink on demand. In Java we are allowed to define a class (say, B) inside of another class (say, A). The class A is called the outer class, and the class B is called the inner class. The purpose of inner classes is purely to be used internally as helper classes. The List class is the outer class and the Node class is the inner class.

Class List - methods Method’s name Method’s description 1 List() Constructor builds an empty linked list . 2 boolean isEmpty() Returns true if this list is empty and false otherwise. 3 Node<T> getFirst() Returns the reference to first element in this list. If list is empty returns null. 4 Node<T> insert(Node<T> pos, T x) Inserts the type T element x after the specified position pos in this list and returns the reference to inserted element x. 5 Node<T> remove(Node<T> pos) Removes the first occurrence of the specified element pos in this list and returns the next element position. We assume that pos != null. 6 String to String() Returns the string representation of linked list.

Outer class List - UML diagram List<T> Node<T> first List() boolean isEmpty() Node<T> getFirst() Node<T> insert(Node<T> pos, T x) Node<T> remove(Node<T> pos) String toString() Class name Class variable Constructor Class methods

Inner class Node - UML diagram Node<T> private T data private Node<T> nextNode Node(T x) Node( T data, Node<T> nextNode ) T getData() Node<T> getNext() Void setData( T data) Void setNext(Node<T> nextNode) String toString() Class name Class variables Constructors Class methods

Class List - basic operations addFirst : The method creates a node ( “C” ) and prepends it at the beginning of the list.

Class List - basic operations addLast: The method appends the node ( “S” ) to the end of the list.

Class List - basic operations Inserting "after“: Find a node containing "key" and insert a new node after it. In the picture below, we insert a new node after “E”:

Class List - basic operations Deletion: Find a node containing "key" and delete it. In the picture below we delete a node containing “ A “

Class List - basic operations Traversing: Start with the head and access each node until you reach null. Do not change the head reference !

Class List - implementation public class List<T> { private Node<T> first; // class List attribute public List() { this.first = null; } // class List constructor public Node<T> getFirst() { return this.first; } // getFirst public boolen isEmpty() { return this.first == null; } // isEmpty public String toString() { String str = “ [ “; Node<T> pos = this.first; while(pos != null) { str = str + pos.getData(); // class Node<T> method if(pos.getNext() != null) str = str + ”, ”; pos = pos.getNext(); } // while str = str + “]”; return str; } // toString

Class List – implementation,cont. public Node<T> insert(Node<T> pos, T x) { Node<T> q = new Node<T>(x); // creating new node if( pos == null ) { q.setNext(this.first); this.first = q; // first element in the list } else { q.setNext(pos.getNext()); pos.setNext(q); return q; } // insert public Node<T> remove(Node<T> pos) { if( this.first == pos ) { this.first = pos.getNext(); // remove first node return this.first; else { Node<T> prev = this.first; while(prev.getNext() != pos) // searching pos reference prev = prev.getNext(); prev.setNext(pos.getNext()); return prev.getNext(); } // remove } // class List Note: Class Node<T> methods

Class List - test This program reads the names of 10 students ,builds linked list and prints the names which begins with an ‘A’ character. public static void main(String args[ ]) { List<String> studNames = new List<String>(); Node<String> last = null; for(int i = 0; i < 10; i++) System.out.println(“ Enter student name “); String name = reader.next(); last = studNames.insert(last,name); } // for Node<String> p = studNames.getFirst(); while(p != null ) { if( p.getData().charAt(0) == ‘A’) System.out.println(p.getData()); p = p.getNext(); } //while } // main public Node<T> insert(Node<T> pos, T x) { Node<T> q = new Node<T>(x); if( pos == null ) q.setNext(this.first); this.first = q; // first element in the list } else q.setNext(pos.getNext()); pos.setNext(q); return q; } // insert

Class List – using external methods public static void nameA(Node<String> p) { while(p != null ) { if( p.getData().charAt(0) == 'A') // getData from Node class, charAt() from String class System.out.println(p.getData()); p = p.getNext(); } // while } // nameA public static void main(String args[ ]) List<String> studNames = new List<String>(); Node<String> last = null; for(int i = 0; i < 10; i++) { System.out.println(“ Enter student name “); String name = reader.next(); last = studNames.insert(last,name); } // for Node<String> pl = studNames.getFirst(); nameA(pl); // calling method nameA from main method } // main

External methods – “what is” questions 1. What is the output for the next program giving the following linked list? 2. What is the purpose of the what method ? public static void what(List<Integer> list) { Node<Integer> a = list.getFirst(); Node<Integer> b = list.getFirst(); while (b != null ) Node<Integer> temp = a; a = a.getNext(); b = b.getNext(); list.remove(temp); if (b != null) } // while } // what public static void main(String[ ] args) { List<Integer> ls = new List<Integer>(); Node<Integer> last = null; System.out.print(" enter an integer -> "); int x = reader.nextInt(); while ( x != 777) { last = ls.insert(last, x); x = reader.nextInt(); } // while System.out.println(ls); what(ls); // calling what method } // main

“what is” questions - trace public static void what(List<Integer> list) { Node<Integer> a = list.getFirst(); Node<Integer> b = list.getFirst(); while (b != null ) { Node<Integer> temp = a; a = a.getNext(); b = b.getNext(); list.remove(temp); if (b != null) b = b.getNext(); } // while } // what a b temp a b != null -> T b b temp a b != null -> T list b b

“what is” questions - solution The what method removes the first half giving in the linked list. enter an integer -> 1 enter an integer -> 2 enter an integer -> 3 enter an integer -> 4 enter an integer -> 777 [ 1, 2, 3, 4 ] [ 3, 4 ] Linked list values sentinel List before calling what output List after calling what

Class List methods - example1 This program reads the coordinates of 10 points, builds linked list of Point type and prints the coordinates of points which their value sums up to 20. public static void main(String args[ ]) { List<Point> ls = new List<Point>(); Node<Point> last = null; for( int i = 0; i < 10; i++) System.out.print(" enter X-> "); int x = reader.nextInt(); System.out.print(" enter Y-> "); int y = reader.nextInt(); last = ls.insert( last, new Point(x,y) ); } // for System.out.println(ls); printP20(ls); // calling external method (next slide) } // main Creating Point type linked list Building Point type linked list

Class List - method printP20 public static void printP20(List<Point> lst) { Node<Point> pos = lst.getFirst(); while( pos != null ) { Point point = pos.getData(); if( point.getX() + point.getY() <= 20) System.out.println(point); pos = pos.getNext(); } // while } // printP20 Class List method Class Point methods Class Node method

method main - what’s different? public static void main(String args[ ]) { List<Point> ls = new List<Point>(); Node<Point> last = null; for( int i = 0; i < N; i++) System.out.print(" enter X-> "); int x = reader.nextInt(); System.out.print(" enter Y-> "); int y = reader.nextInt(); last= ls.insert( last, new Point(x,y) ); } // for System.out.println(ls); printP20(ls); // calling external method } // main public static void main(String args[ ]) { List<Point> ls = new List<Point>(); for( int i = 0; i < N; i++) System.out.print(" enter X-> "); int x = reader.nextInt(); System.out.print(" enter Y-> "); int y = reader.nextInt(); ls.insert( null, new Point(x,y) ); } // for System.out.println(ls); printP20(ls); // calling external method } // main

method main output, N = 3 enter X-> 1 enter Y-> 2 enter X-> 3 [ x= 1.0 y= 2.0, x= 3.0 y= 4.0, x= 5.0 y= 6.0] x= 1.0 y= 2.0 x= 3.0 y= 4.0 x= 5.0 y= 6.0 public Node<T> insert(Node<T> pos, T x) { Node<T> q = new Node<T>(x); if( pos == null ) q.setNext(this.first); this.first = q; // first element in the list } // if else q.setNext(pos.getNext()); pos.setNext(q); } // else return q; } // insert enter X-> 1 enter Y-> 2 enter X-> 3 enter Y-> 4 enter X-> 5 enter Y-> 6 [ x= 5.0 y= 6.0, x= 3.0 y= 4.0, x= 1.0 y= 2.0] x= 5.0 y= 6.0 x= 3.0 y= 4.0 x= 1.0 y= 2.0

Class List methods – example2 This method checks if the linked list of String type is sorted public static boolean isSorted(List<String> lst) { Node<String> pos = lst.getFirst(); while(pos != null) if( pos.getNext() != null ) if( pos.getData().compareTo(pos.getNext().getData() ) > 0 ) return false; pos = pos.getNext(); } // while return true; } // isSorted

Example2 – main and executions public static void main(String args[ ]) { List<String> ls = new List<String>(); Node<String> last = null; for(int i = 1; i < 5; i++) System.out.print(" enter the string "); String x = reader.next(); last = ls.insert(last, x); } // for System.out.println(ls); if(isSorted(ls)) System.out.println("YES"); else System.out.println("NO"); } // main enter the string bee enter the string hello enter the string hi enter the string word [ bee, hello, hi, word ] YES enter the string hello enter the string hi enter the string word enter the string bee [ hello, hi, word, bee ] NO

Class List methods - example 3 This method removes all duplications in Character type linked list public static void remDuplications(List<Character> lst) { Node<Character> pos1= lst.getFirst(), pos2; while( pos1 != null ) char ch = pos1.getData(); pos2 = pos1.getNext(); while( pos2 != null ) if(pos2.getData() == ch) pos2 = lst.remove(pos2); else pos2 = pos2.getNext(); } // inner while pos1 = pos1.getNext(); } // outer while } // remDuplications lst a s a b a z null lst a s b z null

Example3 – main and executions public static void main (String[ ] args) { List<Character> ls = new List<Character>(); Node<Character> last = null; System.out.print( " enter the character -> “ ); char x = reader.next().charAt(0); while ( x != ‘*’ ) { last = ls.insert(last, x); x = reader.next().charAt(0); } // while System.out.println(ls); remDuplications(ls); } // main enter the character -> a enter the character -> s enter the character -> b enter the character -> z enter the character -> * [ a, s, a, b, a, z ] [ a, s, b, z ] sentinel

Merging two linked list Write a Java program that contains a method with the capability to merge two integer type sorted linked lists ( lst1 and lst2 ) . The merged result should be in the third linked list ( lst3 ) that is in sorted order. Do not destroy the original lists. Your program should output the content of three linked lists to show the program performs properly.

Method merge2Lists public static List<Integer> merge2Lists(List<Integer> lst1, List<Integer> lst2) { Node<Integer> pos1 = lst1.getFirst(), pos2 = lst1.getFirst(), pos3 = null; List<Integer> lst3 = new List<Integer>(); while( pos1 != null && pos2 != null ) { if( pos1.getData() > pos2.getData() ) { pos3 = lst3.insert(pos3,pos2.getData()); pos2 = pos2.getNext(); } else { pos3 = lst3.insert(pos3,pos1.getData()); pos1 = pos1.getNext(); } // if } // while while(pos1 != null) { while(pos2 != null) { pos3 = lst3.insert(pos2,pos2.getData()); pos1 = pos2.getNext(); return lst3; } // merge2List

merge2Lists – main and executions public static void main(String[ ] args) { List<Integer> lst1 = new List<Integer>(), lst2 = new List<Integer>(); Node<Integer> last = null; System.out.print(" enter an integer -> "); int x = reader.nextInt(); while ( x != 777) { last = lst1.insert(last, x); x = reader.nextInt(); } // while last = null; last = lst2.insert(last, x); List<Integer> lst3 = merge2Lists(ls1,ls2); System.out.println(ls1); System.out.println(ls2); System.out.println(ls3); } // main enter an integer -> 1 enter an integer -> 2 enter an integer -> 5 enter an integer -> 8 enter an integer -> 9 enter an integer -> 777 enter an integer -> 3 enter an integer -> 4 enter an integer -> 7 [ 1, 2, 5, 8, 9 ] [ 3, 4, 7 ] [ 1, 2, 3, 4, 5, 7, 8, 9 ] lst1 sentinel lst2 sentinel output

Class List methods - example 4 This method calculates the number of different values in the integer type linked list. public static int countDifItems(List<Integer> list) { Node<Integer> temp, pos = list.getFirst(); int count = 0; // number of different values while(pos != null) { temp = pos.getNext(); boolean found = false; // not found while(temp != null) { if(temp.getData() == pos.getData()) { found = true; break; } // if temp = temp.getNext(); } // inner while if( !found ) count++; pos = pos.getNext(); } // outer while return count; } // countDifItems 3

Example4 – main and executions public static void main(String[ ] args) { List<Integer> ls = new List<Integer>(); Node<Integer> last = null; System.out.print(" enter an integer -> "); int x = reader.nextInt(); while ( x != 777) last = ls.insert(last, x); x = reader.nextInt(); } // while System.out.println(ls); System.out.println(“Count = " + countDifItems(ls)); } // main enter an integer -> 1 enter an integer -> 2 enter an integer -> 5 enter an integer -> 777 [ 1, 2, 1, 2, 5 ] Count = 3

Class List methods - example 5 This method checks if the linked list of integer type is circular linked list public static boolean checkCircle(List<Integer> list) { Node<Integer> a = list.getFirst(); Node<Integer> b = list.getFirst(); while ( b != null ) a = a.getNext(); b = b.getNext(); if ( b != null ) else return false; if (a == b) return true; } // while } // checkCircle Tail

Example5 – main and executions public static void main(String[ ] args) { List<Integer> ls = new List<Integer>(); Node<Integer> last = null; System.out.print(" enter an integer -> "); int x = reader.nextInt(); while ( x != 777) { last = ls.insert(last, x); x = reader.nextInt(); } //while System.out.println(ls); if(checkCircle(ls)) System.out.println("YES"); else System.out.println("NO"); } // main enter an integer -> 1 enter an integer -> 2 enter an integer -> 3 enter an integer -> 4 enter an integer -> 5 enter an integer -> 777 [ 1, 2, 3, 4, 5 ] NO /* building circular linked list */ Node<Integer> pos = ls.getFirst(); Node<Integer> first = ls.getFirst(); while(pos.getNext() != null ) pos = pos.getNext(); pos.setNext(first); enter an integer -> 1 enter an integer -> 2 enter an integer -> 3 enter an integer -> 4 enter an integer -> 5 enter an integer -> 777 [ 1, 2, 3, 4, 5 ] YES

Class List recursive method1 This method tests if the number which passed as parameter exists in the linked list of integer type. public static boolean what1(List<Integer> lst, int num) { boolean ans; // returned value int temp; // help variable if( lst.isEmpty() ) ans = false; else temp = lst.getFirst().getData(); lst.remove(lst.getFirst()); ans = (temp == num) || what1(lst,num); lst.insert(null, temp); } // if return ans; } // what

Recursive methods1 debugging enter an integer -> 1 enter an integer -> 2 enter an integer -> 3 enter an integer -> 777 [ 1, 2, 3 ] enter search number -> 5 before insert temp [ ] after insert temp [ 3 ] before insert temp [ 3 ] after insert temp [ 2, 3 ] before insert temp [ 2, 3 ] after insert temp [ 1, 2, 3 ] NO public static boolean what1(List<Integer> lst, int num) { boolean ans; // returned value int temp; // help variable if( lst.isEmpty() ) ans = false; else temp = lst.getFirst().getData(); lst.remove(lst.getFirst()); ans = (temp == num) || what1(lst,num); System.out.println("before insert temp“ + lst); lst.insert(null, temp); System.out.println(“after insert temp“ + lst); } // else return ans; } // what1 enter an integer -> 1 enter an integer -> 2 enter an integer -> 3 enter an integer -> 777 [ 1, 2, 3 ] enter search number -> 2 before insert temp [ 3 ] after insert temp [ 2, 3 ] before insert temp [ 2, 3 ] after insert temp [ 1, 2, 3 ] YES

Linked list recursive method2 This method take the reference to first element in the singly linked list as parameter. What is the output of the method for the following linked list ? public static int what2(Node<Integer> lst) { if ( lst == null ) return 0; Node<Integer> pos = lst.getNext(); int temp = what2(pos); if( !(temp % 2 == 0) ) System.out.println( "temp= “ + temp); System.out.println(pos.getData()); } return temp + 1; } // what2

recursive method2 - solution public static void main(String[ ] args) { List<Integer> ls = new List<Integer>(); Node<Integer> last = null; System.out.print(" enter an integer -> "); int x = reader.nextInt(); while ( x !=777) last = ls.insert(last, x); x = reader.nextInt(); } // while System.out.println(ls); Node<Integer> first = ls.getFirst(); System.out.println("what2 = " + what2(first)); } // main This will produce the next output : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] temp = 1 9 temp = 3 7 temp = 5 5 temp = 7 3 what2 = 9 Method what2 returns the number of elements in the singly linked list.

Class List methods - exam question Write the method: public static int MaxSubList( List<Character> lst, char x, char y ) The method receives the length of the largest sub-list, which can be found between characters x and y. If such sub-list is not found the method receives 0. For example: If we have the following list: lst = 'a', 'd', 'z', 'a', 't', 't', 'a', 'y', 'w‘ The method MaxSubList(lst,'z','a') receives the value 5 ( lst = z, a, t, t, a ). The method MaxSubList(lst,'w','w') receives the value 1. The method MaxSubList(lst,'a','k') receives the value 0.

Exam question - solution public static int MaxSubList ( List<Character> lst, char x, char y ) { Node<Character> pos = lst.getFirst(); boolean start = false; // found x value int maxLen = 0; // largest sub-list length int curLen = 0; // current sub-list length while (pos != null) { if (pos.getData() == x( { start = true; break; } // if pos = pos.getNext(); } // while if (x == y) curLen = 1; while (pos != null && start) { if (pos.getData() == y) maxLen += curLen; curLen = 0; curLen++; return maxLen; } // MaxSubList