241-423 ADSA: Linked Lists/5 1 241-423 Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, 2013-2014 5. Linked.

Slides:



Advertisements
Similar presentations
Data Structures ADT List
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
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)
John Hurley Cal State LA
CSE Lecture 12 – Linked Lists …
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
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.
Linked Lists Dr. Tim Margush University of Akron © 2009.
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.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
ADSA: Collections/ Advanced Data Structures and Algorithms Objective –give an overview of Collection classes, and create/use a Bag class Semester.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
LinkedList Many slides from Horstmann modified by Dr V.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 10 Linked.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Introduction to Data Structures and Algorithms
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
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.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
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.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
1 Objectives of these slides: to describe linked lists in C 6 – Lists.
Chapter 15 An Introduction to Data Structures. Chapter Goals To learn how to use the linked lists provided in the standard library To be able to use iterators.
List Interface and Linked List Mrs. Furman March 25, 2010.
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.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java linked list.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 12 Iterators.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
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 Lists, Queues, Stacks
Chapter 4 Unordered List.
Big-O notation Linked lists
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
UNIT-3 LINKED LIST.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Data Structures and Algorithms IT12112
Introduction to Data Structures
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 4 Unordered List.
List Data Structure.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Podcast Ch18b Title: STree Class
Collections Framework
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked Lists

ADSA: Linked Lists/5 2 Contents 1. An ArrayList can be Slow 2. What is a Linked List? 3. Java Assignment Differences 4. Implementing a Linked List 5. Using a Linked List 6. Doubly Linked Lists 7. The (Doubly) LinkedList Collection 8. Palindromes

ADSA: Linked Lists/ An ArrayList can be Slow Inserting/removing an element inside an ArrayList requires data shifting Inserting/removing an element inside an ArrayList requires data shifting –O(n) operations

ADSA: Linked Lists/5 4 Each element (node) inside a linked list is linked to the previous node and successor (next) node. Each element (node) inside a linked list is linked to the previous node and successor (next) node. This allows for more efficient insertion and deletion of nodes. Why? This allows for more efficient insertion and deletion of nodes. Why? 2. What is a Linked List? continued

ADSA: Linked Lists/5 5 Inserting a new node only involves breaking one link, and linking the list to both ends of the new node: Inserting a new node only involves breaking one link, and linking the list to both ends of the new node: –all are O(1) operations continued

ADSA: Linked Lists/5 6 Removal of a node only requires the breaking of its two links, removal of the node, and then the relinking of the list: Removal of a node only requires the breaking of its two links, removal of the node, and then the relinking of the list: –all are O(1) operations continued

ADSA: Linked Lists/5 7 The insertion/removal of a node is a local operation The insertion/removal of a node is a local operation –only the links next to the node need to be changed –the other nodes in the list are not affected –fast: O(1) An ArrayList must shift lots of elements when an element is inserted/removed An ArrayList must shift lots of elements when an element is inserted/removed –slow: O(n)

ADSA: Linked Lists/ Java Assignment Differences 32 Foo a = new Foo(); Foo b; b = a; int a = 32; int b; b = a; b 32 a b a Foo object copy the link (the reference) copy the value

ADSA: Linked Lists/ Implementing a Linked List Each node is an object containing a value and a link (reference) to the next node (object) in the list Each node is an object containing a value and a link (reference) to the next node (object) in the list –a singly-linked list The list uses a 'front' variable to point to the first object in the list. The list uses a 'front' variable to point to the first object in the list. The reference in the last object is null. The reference in the last object is null.

ADSA: Linked Lists/5 10 Nodes in a singly-linked list are accessed by moving forward one node at a time from the front Nodes in a singly-linked list are accessed by moving forward one node at a time from the front –called sequential access –a linked list is not a direct access structure like an array –this means that access is slower than in an array linked list access (if index is known): O(n) linked list access (if index is known): O(n) array list access (if index is known): O(1) array list access (if index is known): O(1) Accessing a Node

ADSA: Linked Lists/5 11 Nodes in a Linked List Each Node object contains two variables: Each Node object contains two variables: –nodeValue, of generic type T –next, a reference that links to the next node

ADSA: Linked Lists/5 12 The Node Class public class Node { public T nodeValue; // data held by the node public Node next; // next node in the list public Node() { nodeValue = null; next = null; } public Node(T item) { nodeValue = item; next = null; }

ADSA: Linked Lists/5 13 The variables in the Node class are public to simplify the coding using linked lists The variables in the Node class are public to simplify the coding using linked lists –bad style (from Ford & Topp, not me ☺) The Node class is self ‑ referencing: The Node class is self ‑ referencing: –next refers to (points to) an object of the same type

ADSA: Linked Lists/5 14 Creating a Linked List // create two nodes (figure (a) Node p = new Node ("red"); Node q = new Node ("green") // link p to q p.next = q;// figure (b) // set front to point at the first node Node front = p;// figure (c) continued

ADSA: Linked Lists/5 15 If the linked list is empty, front is assigned null. If the linked list is empty, front is assigned null.

ADSA: Linked Lists/5 16 Scanning a Linked List We scan a singly linked list by starting at the front, and then move along the list one Node at a time We scan a singly linked list by starting at the front, and then move along the list one Node at a time –sequential access (O(n)) –stop when we reach null toString() is an example of a scanning method, which builds a string as it moves along the list. toString() is an example of a scanning method, which builds a string as it moves along the list.

ADSA: Linked Lists/5 17 toString() public static String toString(Node front) // build a string from the list of the form // "[ n1, n2,..., nx ]" { if (front == null) // empty list return "[]"; Node curr = front; // start at the front String s = "[" + curr.nodeValue; while(curr.next != null) { curr = curr.next; // move along list s += ", " + curr.nodeValue; } s += "]"; return s; }

ADSA: Linked Lists/5 18 Moving to a List Position To move to an element at position x, we need to start at the front and move through the list counting up to x To move to an element at position x, we need to start at the front and move through the list counting up to x –sequential access again (O(n)) The first element of the list is at position 0. The first element of the list is at position 0. continued

ADSA: Linked Lists/5 19 Node curr = front; // start at the front of list for (int i = 0; i < xPos; i++) curr = curr.next; // move along list

ADSA: Linked Lists/5 20 Updating the Front of the List Inserting or deleting an element at the front of a list is easy (and fast) because the 'front' variable always points to the first element: Inserting or deleting an element at the front of a list is easy (and fast) because the 'front' variable always points to the first element: –the operations are O(1)

ADSA: Linked Lists/5 21 Node newNode = new Node (item); // insert item at the front of the list newNode.next = front; front = newNode; Insert at the Front

ADSA: Linked Lists/5 22 front = front.next;// move front to next node Delete from the Front

ADSA: Linked Lists/5 23 General Insertion To insert a new node before a node referenced by 'curr', the code must have access to the previous node, 'prev', since its link must be changed. To insert a new node before a node referenced by 'curr', the code must have access to the previous node, 'prev', since its link must be changed. continued

ADSA: Linked Lists/5 24 Node curr =... // set to point to a node Node prev =... // set to point to previous node Node newNode = new Node (item); // new node // update links newNode.next = curr;// step 1 prev.next = newNode;// step 2 continued

ADSA: Linked Lists/5 25 The insertion is O(1) since only two links need to be changed. But the real cost is the sequential search to find the insertion position, which is O(n). The insertion is O(1) since only two links need to be changed. But the real cost is the sequential search to find the insertion position, which is O(n).

ADSA: Linked Lists/5 26 General Deletion Deleting a node at position curr requires access to the predecessor node prev. Deleting a node at position curr requires access to the predecessor node prev. Node curr =... // set to point to a node Node prev =... // set to point to previous node // connect prev to curr.next prev.next = curr.next; curr.next = null; continued

ADSA: Linked Lists/5 27 The deletion is O(1) since only two links need to be changed. But the real cost is the sequential search to find the deletion position, which is O(n) The deletion is O(1) since only two links need to be changed. But the real cost is the sequential search to find the deletion position, which is O(n) –this is shown in the remove() method, which is explained next

ADSA: Linked Lists/5 28 Removing a Target Node To remove the first node having a specified value, scan the list to find the node. To remove the first node having a specified value, scan the list to find the node. The scan must use two references that move together down the list The scan must use two references that move together down the list –one reference (curr) points to the current node in the scan –the other reference (prev) points to the previous node continued

ADSA: Linked Lists/5 29 Once 'curr' finds the node, the code uses 'prev' to unlink 'curr'. Once 'curr' finds the node, the code uses 'prev' to unlink 'curr'. continued

ADSA: Linked Lists/5 30 At the start, point 'curr' at the front of the list and set 'prev' to null, since the first node does not have a predecessor. At the start, point 'curr' at the front of the list and set 'prev' to null, since the first node does not have a predecessor. Move 'curr' and 'prev' down the list until curr.nodeValue matches the target or curr == null. Move 'curr' and 'prev' down the list until curr.nodeValue matches the target or curr == null. continued

ADSA: Linked Lists/5 31 If the target is found then 'curr' points at the node and 'prev' to the predecessor node. If the target is found then 'curr' points at the node and 'prev' to the predecessor node. But there are two possible cases: But there are two possible cases: –the target node is the first node, so 'prev' is null –the target node is not the first node, so 'prev' points to something continued

ADSA: Linked Lists/5 32 Case 1:'prev' is null which means that 'curr' points to the first node. Case 1:'prev' is null which means that 'curr' points to the first node. –so we only have to delete the front of the list front = curr.next; curr.next = null;

ADSA: Linked Lists/5 33 Case 2:The match occurs in the middle of the list. Both 'curr' and 'prev' have non-null values. Unlink the current node. Case 2:The match occurs in the middle of the list. Both 'curr' and 'prev' have non-null values. Unlink the current node. prev.next = curr.next; curr.next = null; continued

ADSA: Linked Lists/5 34 The generic remove() is passed a reference to the front of the list and the target value. The generic remove() is passed a reference to the front of the list and the target value. The method returns the value of 'front', which may have been updated if the first node was deleted. The method returns the value of 'front', which may have been updated if the first node was deleted.

ADSA: Linked Lists/5 35 remove() Method public static Node remove(Node front, T target) /* Delete the first occurrence of the target in the linked list referenced by front; return the value of front */ { // initialize pointers Node curr = front; Node prev = null; boolean foundItem = false; // set to true if we find the target :

ADSA: Linked Lists/5 36 // scan until find item or end of list (O(n)) while (curr != null && !foundItem) { // check for a match if (target.equals(curr.nodeValue)) { if (prev == null) // remove first Node (O(1)) front = front.next; else // erase middle Node (O(1)) prev.next = curr.next; curr.next = null; foundItem = true; } else { // advance curr and prev prev = curr; curr = curr.next; } return front; // may be updated } // end of remove()

ADSA: Linked Lists/ Using a Linked List import java.util.Random; import java.util.Scanner; import ds.util.Node; import ds.util.Nodes; // methods using Node public class ListExample { public static void main(String[] args) { // the initial list is empty Node front = null; Random rnd = new Random(); Scanner keyIn = new Scanner(System.in); :

ADSA: Linked Lists/5 38 System.out.print("Enter the size of the list: "); int listCount = keyIn.nextInt(); // create a list Node newNode; for (int i = 0; i < listCount; i++) { newNode = new Node (rnd.nextInt(100)); newNode.next = front; // insert at list front front = newNode; } System.out.print("Original list: "); System.out.println( Nodes.toString(front) ); :

ADSA: Linked Lists/5 39 System.out.print("Ordered list: "); Node p; while (front != null) { // list not empty p = getMaxNode(front); // get largest System.out.print(p.nodeValue + " "); front = Nodes.remove(front, p.nodeValue); } System.out.println(); } // end of main()

ADSA: Linked Lists/5 40 public static > Node getMaxNode(Node front) { Node maxNode = front; // initial values Node curr = front.next; T maxValue = front.nodeValue; while (curr != null) { // try to update maxNode and maxValue if (maxValue.compareTo(curr.nodeValue)< 0) { maxValue = curr.nodeValue; maxNode = curr; } curr = curr.next; } return maxNode; } // end of getMaxNode() } // end of ListExample class

ADSA: Linked Lists/5 41 Execution

ADSA: Linked Lists/ Doubly Linked Lists A node in a doubly-linked list contain two references that point to the next node and the previous node. A node in a doubly-linked list contain two references that point to the next node and the previous node. front points to the first node in the list front points to the first node in the list back points at the last node in the list back points at the last node in the list continued

ADSA: Linked Lists/5 43 A doubly-linked list can be scanned in both directions: A doubly-linked list can be scanned in both directions: –a forward scan starts at 'front' and ends when the link is to the same object as 'back' –a backward scan starts at 'back' and ends when the link is to the same object as 'front' continued

ADSA: Linked Lists/5 44 Like a singly-linked list, a doubly linked list is a sequential structure. Like a singly-linked list, a doubly linked list is a sequential structure. To move forward or backward, use the node links 'next' and 'prev'. To move forward or backward, use the node links 'next' and 'prev'. Unlike a singly linked list, the insert and delete operations only need a single reference to the node. Unlike a singly linked list, the insert and delete operations only need a single reference to the node. continued

ADSA: Linked Lists/5 45 Insertion into a doubly linked list requires four reference assignments. Insertion into a doubly linked list requires four reference assignments. prevNode = curr.prev; newNode.prev = prevNode;// 1 prevNode.next = newNode;// 2 curr.prev = newNode;// 3 newNode.next = curr;// 4 continued

ADSA: Linked Lists/5 46 To delete a node curr, link the predecessor (curr.prev) of 'curr' to the successor of 'curr' (curr.next). To delete a node curr, link the predecessor (curr.prev) of 'curr' to the successor of 'curr' (curr.next). prevNode = curr.prev; succNode = curr.next; succNode.prev = prevNode;// 1 prevNode.next = succNode;// 2 curr.prev = null; curr.next = null; continued

ADSA: Linked Lists/5 47 In a singly-linked list, adding and removing a node at the front of the list are O(1) operations. In a singly-linked list, adding and removing a node at the front of the list are O(1) operations. With a doubly linked list, you can add and remove a node at the back of the list with the same O(1) efficiency. With a doubly linked list, you can add and remove a node at the back of the list with the same O(1) efficiency.

ADSA: Linked Lists/ The (Doubly) LinkedList Collection In Ford & Topp's DSA package

ADSA: Linked Lists/5 49 UML for LinkedList

ADSA: Linked Lists/5 50 The LinkedList() constructor creates an empty list. The LinkedList() constructor creates an empty list. The toString() method returns a string representing the list as a comma-separated sequence of elements enclosed in brackets. The toString() method returns a string representing the list as a comma-separated sequence of elements enclosed in brackets. LinkedList Methods continued

ADSA: Linked Lists/5 51 Reuse the Collection methods: Reuse the Collection methods: –isEmpty(), size(), contains(), toArray() add() inserts a new element at the back of the list and returns true. add() inserts a new element at the back of the list and returns true. remove() with an Object reference deletes the first occurrence of the object in the list remove() with an Object reference deletes the first occurrence of the object in the list –the method returns true or false depending on whether a match was found

ADSA: Linked Lists/5 52 LinkedList Examples LinkedList aList = new LinkedList (); alist.add("Red"); alist.add("Green"); alist.add("Blue); System.out.println("Size = " + aList.size()); System.out.println("List contains the string 'White' is " + aList.contains("White"); Size = 3 List contains the string 'White' is false

ADSA: Linked Lists/5 53 aList.add("Black");// add Black at the end aList.add("Blue");// add Blue at the end aList.remove("Blue");// delete first "Blue" System.out.println(aList);// uses toString() [Red, Green, Black, Blue]

ADSA: Linked Lists/5 54 LinkedList Index Methods The LinkedList can access and update an element with get() and set(), and modify the list with the add() and remove(). The LinkedList can access and update an element with get() and set(), and modify the list with the add() and remove(). The index methods have O(n) worst case running time. Use these methods only for small data sets. The index methods have O(n) worst case running time. Use these methods only for small data sets.

ADSA: Linked Lists/5 55 Example // create list containing [5,7,9,4,3] Integer i= list.get(1); // i has value 7 list.remove(1); // remove value at position 1 continued

ADSA: Linked Lists/5 56 list.set(2, 8); // store node 8 at position 2 list.add(2, 6); // store value 6 at position 2

ADSA: Linked Lists/5 57 Accessing the Ends of a LinkedList Methods for the front of the list: Methods for the front of the list: –getFirst(), addFirst(), removeFirst() For the back of the list: For the back of the list: –getLast(), addLast(), removeLast() They all are O(1) operations They all are O(1) operations

ADSA: Linked Lists/5 58 End-of-List Examples LinkedList list = new LinkedList (); list.addFirst("Tom"); list.addFirst("Debbie"); list.addLast("David"); ist.addLast("Maria"); continued

ADSA: Linked Lists/5 59 // identify the elements at the ends of the list System.out.println("First element is " + list.getFirst()); System.out.println("Last element is " + list.getLast()); First element is Debbie Last element is Maria continued

ADSA: Linked Lists/5 60 // Exchange the first and last elements in the list. // remove elements at the ends of the list String firstElem = aList.removeFirst(); String lastElem = aList.removeLast(); // add elements back in switched positions aList.addLast(firstElem); aList.addFirst(lastElem); continued

ADSA: Linked Lists/5 61 // Output elements in the list by position. // Repeatedly delete first element and display its // value until list is empty while (!aList.isEmpty()) System.out.print(aList.removeFirst() + " "); Maria Tom David Debbie

ADSA: Linked Lists/5 62 A linked list is a natural way to implement a queue. A linked list is a natural way to implement a queue. –the element at the front of the queue can be removed with getFirst() –a new element can be added to the back of the queue by using addLast() Linked List as a Queue

ADSA: Linked Lists/ Palindromes A palindrome is a string that reads the same forward and backward: A palindrome is a string that reads the same forward and backward: –e.g. "level", "noon.", "Stack Cats" –ignore non-letters and let capitals == lowercase isPalindrome() takes a LinkedList object as an argument and returns true if the sequence is a palindrome; false otherwise. isPalindrome() takes a LinkedList object as an argument and returns true if the sequence is a palindrome; false otherwise. continued

ADSA: Linked Lists/5 64 Panic in a Titanic, I nap. Race car Yawn a more Roman way. A Toyota's a Toyota

ADSA: Linked Lists/5 65 public static boolean isPalindrome(LinkedList aList) { // list must have 2 or more elements while (aList.size() > 1) { // compare elements on opposite ends of list if ( !aList.getFirst().equals(aList.getLast()) ) return false; // delete the matching elements aList.removeFirst(); aList.removeLast(); } // if we get here then list is a palindrome return true; }

ADSA: Linked Lists/5 66 The "?" Wildcard isPalindrome() does not refer to the generic type of the list. In this case, we may use: isPalindrome() does not refer to the generic type of the list. In this case, we may use: LinkedList aList "?" means that we don't care about the type of the elements of the list. "?" means that we don't care about the type of the elements of the list.

ADSA: Linked Lists/5 67 Checking for a Palindrome import java.util.Scanner; import ds.util.LinkedList; public class CheckPali { public static void main(String[] args) { LinkedList charList = new LinkedList (); // get input line from user System.out.print("Enter a string: "); Scanner keyIn = new Scanner(System.in); String str = keyIn.nextLine(); :

ADSA: Linked Lists/5 68 // put all letters into list as lowercase chars for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (Character.isLetter(ch)) charList.addLast( Character.toLowerCase(ch) ); } if (isPalindrome(charList)) System.out.println("'" + str + "' is a palindrome"); else System.out.println("'" + str + "' is not a palindrome"); } // end of main() // isPalindrome() method goes here } // end of CheckPali class

ADSA: Linked Lists/5 69 Execution