Chapter 5 Linked Lists II

Slides:



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

Linked Lists Geletaw S..
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CS Data Structures II Review COSC 2006 April 14, 2017
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
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.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 7 Stacks II CS Data Structures I COSC 2006
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
April 24, 2017 Chapter 4 Data Abstraction The Walls
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Introduction to Data Structures and Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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.
List Interface and Linked List Mrs. Furman March 25, 2010.
April 27, 2017 COSC Data Structures I Review & Final Exam
CSC 205 Programming II Lecture 15 Linked List – Other Variations.
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
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Linked List, Stacks Queues
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
CS2006- Data Structures I Chapter 5 Linked Lists I.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Lists CS 3358.
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
public class StrangeObject { String name; StrangeObject other; }
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 4 Linked Lists.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Ch. 5 Linked List When a list is implemented by an array Linked list
Chapter 4 Linked Lists.
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists Chapter 5 (continued)
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
General List.
Programming II (CS300) Chapter 07: Linked Lists
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

Chapter 5 Linked Lists II CS2006- Data Structures I Chapter 5 Linked Lists II

ADT Linked List Specification: Operations: Elements: isEmpty: Elements are nodes One reference Operations: isEmpty: Check if the list is empty getLength Get the length of the list insert Insert a new element at a specific position remove Delete an element from a specific position retrieve Rerieve contents of an element From a specific position traverse Perform a specific Operation on all the elements of the list

Inserting a Node Steps: 1. Determine the point of insertion 2. Create a new node & store the new data 3. Connect the new node to the list by changing “pointers” Special case?

Inserting a Node Finding Point of Insertion in a Sorted List Java Code: Using for-loop  for (prev = NULL, cur = Head; / / Start (cur !=NULL) && (newValue > cur.getItem()); / / Termination prev = cur, cur = cur.getNext()); / / Step Determining the values of cur & prev is simpler when you insert or delete a node by position instead of by value

Deleting a Node Node Deletion Steps: Special cases? 1. Locate the node that you want to delete (find cur) 2.  Disconnect the node from the list by changing “pointers” 3.  Return the node to the system Special cases?

Deleting a node not at the head Deleting a node not at the head is similar to adding a node to the middle of the list. We would have to set up previous (prev) and current (cur) example

Deleting from the middle public void deleteNode() { if(cur != null && prev != null){ prev.setNext(cur.getNext()); cur = cur.getNext(); } The deleteNode() method works even if the node is a tail node. In this case, the next of the previous node will be assigned the null value.

Deleting a Node Node Deletion: Observations Links of the list can't be followed backwards Two external “pointers” are needed: cur: Node to be deleted prev: Node pointing to cur List should be traversed to find the proper positions of cur & prev

Traversing a Linked List Visit each node in the list , do some operation (e.g. display) on its items, until the end of the list is reached Example Displaying the Contents of a Linked List

Traversing a Linked List for ( Node cur = head ; cur != null ; cur = cur.getNext() ) { System.out.println ( cur.getItem() ); } cur head 1 3 7 9 null Recursive?

Insert and Delete operations on an empty list  Problem with insertion and deletion methods: They require special cases and different actions for first nodes. The addition of a dummy head node to the linked list eliminates the special cases “dummy" head node does not contain any data and its reference points to the first data containing node. An empty list now consists of a head reference and a header node with a null reference

Interface for ADT List // **************************************************** // Interface for the ADT list //for easy understanding, I change Comparable to Object public interface ListInterface { // list operations: public boolean isEmpty(); public int size(); public void addFirst(Object item); public void addLast(Object item); public void remove(Object item); public Node find(Object item); public void removeAll(); } // end ListInterface

Comparable Node Class public class Node { private Object item; private Node next; public Node(Object newItem) { item = newItem; next = null; } // end constructor public Node(Object newItem, Node nextNode) { next = nextNode;

Comparable Node Class (2) public void setItem(Object newItem) { item = newItem; } // end setItem public Object getItem() { return item; } // end getitem public void setNext(Node nextNode) { next = nextNode; } // end setNext public Node getNext() { return next; } // end getNext } // end class Node

Implementation of ADT List // **************************************************** // Reference-based implementation of ADT list. public class List implements ListInterface { // reference to linked list of items private Node head; private int numItems; // number of items in list public List() { numItems = 0; head = null; } // end default constructor public boolean isEmpty( ) { return numItems == 0; } // end isEmpty public int size( ) { return numItems; } // end size

Implementation of ADT List // **************************************************** // Reference-based implementation of ADT list. public class List implements ListInterface { // reference to linked list of items private Node head; private int numItems; // number of items in list public List() { numItems = 0; head = null; } // end default constructor public boolean isEmpty( ) { return numItems == 0; } // end isEmpty public int size( ) { return numItems; } // end size

Implementation of ADT List (2) public Node find(Object findItem) { // Locates a specified node in a linked list. // Returns a reference to the desired node. Node curr = head; while((curr != null) && (!findItem.equals (curr.getItem)) { curr = curr.getNext(); } // end while return curr; } // end find public void addFirst(Object item) { // insert a new first node into the list Node newNode = new Node(item, head); head = newNode; numItems++; } // end addFirst

Implementation of ADT List (3) public void addLast(Object item) { // insert a new last node into the list Node curr = head; if (curr == null) { // insert a new first (and only) node Node newNode = new Node(item, head); head = newNode; } else { while(curr.getNext() != null) curr = curr.getNext(); // curr now contains a ref to the last node on the list Node newNode = new Node(item); curr.setNext(newNode); numItems++; } // end addLast

Implementation of ADT List (4) public void remove(Object removeItem) { // if(isEmpty()) return; Node curr = head, prev = null; if(curr == null) return; while((curr != null) && (!removeItem.equals (curr.getItem)) { prev = curr; curr = curr.getNext(); } // end while - if curr == null removeItem was not found if(curr != null) { // if node is not found do nothing if(curr == head) // remove first node head = head.getNext(); else prev.setNext(curr.getNext()); // remove node after prev numItems--; } } // end remove

Implementation of ADT List (5) public void removeAll() { // setting head to null causes list to be // unreachable and thus marked for garbage collection head = null; numItems = 0; } // end removeAll

Implementation of ADT List (5) public void removeAll() { // setting head to null causes list to be // unreachable and thus marked for garbage collection head = null; numItems = 0; } // end removeAll

Implementation of ADT List (6) public void printList() { // calls the recursive method to print the linked list printNode(head); System.out.println(); } private void printNode(Node curr) { // the recursive printing method if(curr != null){ System.out.print(curr.getItem()+" "); printNode(curr.getNext()); } // end List

Implementation of ADT List (6) public void printList() { // calls the recursive method to print the linked list printNode(head); System.out.println(); } private void printNode(Node curr) { // the recursive printing method if(curr != null){ System.out.print(curr.getItem()+" "); printNode(curr.getNext()); } // end List

Arrays vs. Lists Arrays Lists Fixed size Can have arbitrary length Items placed in sequence Items linked to one another using reference Number of items can't exceed array size Number of items can increase indefinitely All elements following the insertion/deletion position have to be shifted reference change used to insert/delete

Review The last node of a linear linked list ______. has the value null has a next reference whose value is null has a next reference which references the first node of the list cannot store any data

Review A reference variable whose sole purpose is to locate the first node in a linked list is called ______. top front Head first

Review Which of the following will be true when the reference variable curr references the last node in a linear linked list? curr == null head == null curr.getNext() == null head.getNext() == null

Review If a linked list is empty, the statement head.getNext() will throw a(n) ______. IllegalAccessException ArithmeticException IndexOutOfBoundsException NullPointerException

Review To delete a node N from a linear linked list, you will need to ______. set the reference next in the node that precedes N to reference the node that follows N set the reference next in the node that precedes N to reference N set the reference next in the node that follows N to reference the node that precedes N set the reference next in N to reference the node that follows N

Review Which of the following statements deletes the node that curr references? prev.setNext(curr); curr.setNext(prev); curr.setNext(curr.getNext()); prev.setNext(curr.getNext());

Review Which of the following statements deletes the first node of a linear linked list that has 10 nodes? head.setNext(curr.getNext()); prev.setNext(curr.getNext()); head = head.getNext(); head = null;