1 Lecture 25 Abstract Data Types –III Overview  Basic Node Design  Example: The Dynamic Phone List  Manipulating the Phone List  Inserting Nodes into.

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.
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linear collections.
Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.
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)
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Data Structures Using C++ 2E
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.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
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.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Introduction to Data Structures and Algorithms
Chapter 5 Linked Lists II
Linked Structures, LinkedStack
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
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.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 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.
List Interface and Linked List Mrs. Furman March 25, 2010.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli | Ralph Walde Trinity College Hartford, CT presentation slides for published by Prentice.
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.
CSCS-200 Data Structure and Algorithms Lecture
Linked Lists CS 367 – Introduction to Data Structures.
Chapter 16: Linked Lists.
Data Structures: Linked Lists
Section 2.6 Linked List StringLog ADT Implementation
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists I.
CS212D: Data Structures Week 5-6 Linked List.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
8-1.
CS212D: Data Structures Week 5-6 Linked List.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
8-1.
Chapter 16-2 Linked Structures
Chapter 13 Collections.
Linked Lists [AJ 15].
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Introduction to Algorithms and Data Structures
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Programming II (CS300) Chapter 07: Linked Lists
Presentation transcript:

1 Lecture 25 Abstract Data Types –III Overview  Basic Node Design  Example: The Dynamic Phone List  Manipulating the Phone List  Inserting Nodes into a List  The insert() Method  Traversing a List  Printing the Nodes of a List  Looking up a Node in a List  The remove() Method  Testing the List  The Generic Node Class

2 Lecture 25 Basic Node Design l A node in a linked list contains data elements and link elements. public class Node { private Object data; private Node next; public Node(Object obj); // Constructor public void setData(Object obj); // Data access public Object getData(); public void setNext(Node link); // Link access public Node getNext(); } // Node Data elements. Link elements. Stores any kind of object.

3 Lecture 25 Example: The Dynamic Phone List public class PhoneListNode { private String name; private String phone; private PhoneListNode next; public PhoneListNode(String s1, String s2) { name = s1; phone = s2; next = null; } // PhoneListNode() public void setNext(PhoneListNode nextPtr) { next = nextPtr; } // setNext() public PhoneListNode getNext() { return next; } // getNext() } // PhoneListNode public void setData(String s1, String s2) { name = s1; phone = s2; } // setData() public String getName() { return name; } // getName() public String getData() { return name + " " + phone; } // getData() public String toString() { return name + " " + phone; } // toString() Data elements.

4 Lecture 25 Manipulating the Phone List l A class to manipulate the list. public class PhoneList { private PhoneListNode head; public PhoneList() { head = null; // Start with empty list } public boolean isEmpty() { // Defines an empty list return head == null; } public void insert(PhoneListNode node) { } public String getPhone(String name) { } public String remove(String name) { } public void print() { } } // PhoneList Design: These methods require little knowledge of the list. Null reference defines empty list.

5 Lecture 25 Inserting Nodes into a List l Insert at the end of the list. Case 1: Empty list Case 2: Nonempty list. Head Newnode Before Insertion After Insertion Head Ahmed Omer Head Ahmed Omer Newnode (a) Insertion into Empty List ( b) Insertion into Existing List

6 Lecture 25 The insert() Method If the list is not empty, insert() traverses the list and inserts at the end. public void insert(PhoneListNode newNode) { if (isEmpty()) head = newNode; // Insert at head of list else { PhoneListNode current = head; // Start traversal at head while (current.getNext() != null) // While not at the last node current = current.getNext(); // go to the next node current.setNext( newNode ); // Do the insertion } } // insert() Case 1: Empty list Case 2: Nonempty list. Note loop bound.

7 Lecture 25 Traversing a List Traverse: current always points to current node. New node inserted after the last node.

8 Lecture 25 Printing the Nodes of a List l Traverse the list, printing each node. public void print() { if (isEmpty()) System.out.println("Phone list is empty"); PhoneListNode current = head; // Start traversal at head while (current != null) { // While not at end of list System.out.println( current.toString() ); // print node's data current = current.getNext(); // go to the next node } } // print() Traverse: current always points to current node. Note loop bound. l Terminating Traversal: The loop bound depends on whether you need a reference to the last node or not after the loop terminates.

9 Lecture 25 Looking up a Node in a List l Searching: traverse the list until the name is found or until the end is reached (not found). public String getPhone(String name) { if (isEmpty()) // Case 1: empty list return "Phone list is empty"; else { PhoneListNode current = head; while ((current.getNext() != null) && (!current.getName().equals(name))) current = current.getNext(); if (current.getName().equals(name)) // Case 2: found the name return current.getData(); else // Case 3: no such person return ("Sorry. No entry for " + name); } } // getPhone() Compound condition. current points to correct node.

10 Lecture 25 Removing a Node From a List l To remove, the list must be re-linked. Head Previous Head (a) Removing First Node Head (b) Removing Other Node Removed nodes will be garbage collected Current Previous Re-link the head. Two pointers needed to re- link an internal node.

11 Lecture 25 The remove() Method public String remove (String name) { // Remove an entry by name if (isEmpty()) // Case 1: empty list return "Phone list is empty"; PhoneListNode current = head; PhoneListNode previous = null; if (current.getName().equals(name)) { // Case 2: remove first node head = current.getNext(); return "Removed " + current.toString() ; } while ((current.getNext() != null) && (!current.getName().equals(name))) { previous = current; current = current.getNext(); } if (current.getName().equals(name)) { // Case 3: remove named node previous.setNext(current.getNext()); return "Removed " + current.toString(); } else return ("Sorry. No entry for " + name); // Case 4: node not found } // remove() Pointer to previous node. Cut out the node. Re-link the head. a Head Previous Head Current Previous

12 Lecture 25 Testing the List public static void main(String argv[]) { // Create list and insert some nodes PhoneList list = new PhoneList(); list.insert( new PhoneListNode(“Ahmed M", " ")); list.insert( new PhoneListNode(“Omer W", " ")); list.insert( new PhoneListNode(“Salem P", " ")); list.insert( new PhoneListNode(“Nuri M", " ")); list.insert( new PhoneListNode(“Rami K", " ")); // Test whether insertions worked System.out.println( "Phone Directory" ); list.print(); } // main() l Strategy »Test insertions. »Test lookups, both successes and failures. »Test removals at different locations.

13 Lecture 25 Testing the List // Test whether lookups work System.out.println("Looking up numbers by name"); System.out.println(list.getPhone(“Ahmed M")); System.out.println(list.getPhone(“Omer R")); System.out.println(list.getPhone(“Salem K")); System.out.println(list.getPhone(“Zaki M")); System.out.println(list.remove(“Majdi P")); l Searches: l Removals: System.out.println("Phone Directory"); list.print(); // Test removals, printing list after each removal System.out.println(list.remove(“Ahmed M")); System.out.println("Phone Directory"); list.print(); System.out.println(list.remove(“Rami K")); System.out.println("Phone Directory"); list.print();

14 Lecture 25 OOD: The List Abstract Data Type (ADT ) l Wanted: a generic list structure. l An Abstract Data Type (ADT) has two parts: »the data that are being stored and manipulated, »the methods and operations on those data. l Example: Integer data »Data: all the integral values »Operations: +, -, /, *, % l Example: List »Data: the objects to be stored. »Operations: insert, delete, search. l Information Hiding: Hide implementation!

15 Lecture 25 The Generic Node Class public class Node { private Object data; // Stores any kind of data private Node next; public Node(Object obj) { // Constructor data = obj; next = null; } // Link access methods public void setNext( Node nextPtr ) { next = nextPtr; } public Node getNext() { return next; } } // Node // Data access methods public void setData(Object obj) { data = obj; } public Object getData() { return data; } public String toString() { return data.toString(); }

16 Lecture 25 The Generic List Class public class List { private Node head; public List() { head = null; } public boolean isEmpty() { return head == null; } public void print() { } public void insertAtFront( Object newObj ) { } public void insertAtRear( Object newObj ) { } public Object removeFirst() { } public Object removeLast() { } } // List Two kinds of insertions... … and kinds of removals.

17 Lecture 25 List Insertion Methods public void insertAtFront (Object newObj) { Node current = new Node( newObj ); current.setNext(head); head = current; } public void insertAtRear(Object newObj) { if (isEmpty()) head = new Node(newObj); else { Node current = head; // Start at head of list while (current.getNext() != null) // Find the end of the list current = current.getNext(); current.setNext(new Node(newObj)); // Insert the newObj } } // insertAtRear() The inserted object goes into a new Node which is inserted in the list.

18 Lecture 25 List Removal Methods public Object removeLast() { if (isEmpty()) // Empty list return null; Node current = head; if (current.getNext() == null) { // Singleton list head = null; return current; } Node previous = null; // All other cases while (current.getNext() != null) { previous = current; current = current.getNext(); } previous.setNext(null); return current; } // removeLast() public Object removeFirst() { Node first = head; head = head.getNext(); return first; } // removeFirst() Re-link the list. Head (a) Removing First Node Removed nodes will be garbage collected Head Previous Head Current Previous

19 Lecture 25 Testing the List ADT public static void main( String argv[] ) { // Create list and insert heterogeneous nodes List list = new List(); list.insertAtFront(new PhoneRecord(“Ahmed M", " ")); list.insertAtFront(new Integer(8647)); list.insertAtFront(new String("Hello World")); list.insertAtRear(new PhoneRecord(“Sami M", " ")); list.insertAtRear(new PhoneRecord(“Rami K", " ")); System.out.println("Generic List"); // Print the list list.print(); // Remove objects and print resulting list Object o; o = list.removeLast(); System.out.println(" Removed " + o.toString()); System.out.println("Generic List:"); list.print(); o = list.removeFirst(); System.out.println(" Removed " +o.toString()); System.out.println("Generic List:"); list.print(); } // main() Insert different types of objects. Note use of toString().

20 Lecture 25 Sample output of phoneList that defines a node of a linked list of phone records. Sample output of List(a generic list data structures. In linked list