Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.

Slides:



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

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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
© 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 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.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
© 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.
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 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.
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.
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.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
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.
Chapter 5 Linked Lists II
© 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.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSC 205 Programming II Lecture 15 Linked List – Other Variations.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
An Array-Based Implementation of the ADT List
Cinda Heeren / Geoffrey Tien
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
CS2006- Data Structures I Chapter 5 Linked Lists I.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Chapter 4 Linked Lists
public class StrangeObject { String name; StrangeObject other; }
Chapter 4 Linked Lists.
Data structures in C++.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Ch. 5 Linked List When a list is implemented by an array Linked list
Indirection.
Linked Lists Chapter 4.
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)
Data Structures & Algorithms
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

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 list Is able to grow in size as needed Does not require the shifting of items during insertions and deletions Accessing an element does not take a constant time

Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference

Linked Lists Each node contains two things The data A pointer to the next node 7

Linked Lists class Node { public int data; public Node next; } 7

Linked Lists class Node { public int data; public Node next; } 7 A node points to another node, so the pointer must be of type Node

Building a Linked List Node a = new Node(7, null); a Assume we’ve added a constructor to the Node class that lets us initialize data and next like this. 7

Building a Linked List Node a = new Node(7, null); a.next = new Node(3, null); a 7 3 a.nexta.data a.next.data

Traversing a Linked List Node a = new Node(7, null); a.next = new Node(3, null); Node p = a; a 7 3 p

Building a Linked List Node a = new Node(7, null); a.next = new Node(3, null); Node p = a; p = p.next; // go to the next node a 7 3 We can walk through a linked list by traversing nodes one by one. p

Traversing a Linked List Node a = new Node(7, null); a.next = new Node(3, null); Node p = a; p = p.next; // go to the next node p = p.next; a 7 3 Eventually, p hits the end of the list and becomes null. p

Preliminaries Figure 5-1 a) A linked list of integers; b) insertion; c) deletion

Reference-based implementation Reference-based implementations of ADT use Java references We are going to look at a reference-based implementation of ATD list (using linked lists)

Object References (review) A reference variable Contains the location of an object Example Node node; node = new Node(5,null); As a data field of a class has the default value null As a local reference variable (in a method) does not have a default value

Object References When one reference variable is assigned to another reference variable, both references then refer to the same object Integer p, q; p = new Integer(6); q = p; A reference variable that no longer references any object is marked for garbage collection

Object References Figure 5-3a-d a) Declaring reference variables; b) allocating an object; c) allocating another object, with the dereferenced object marked for garbage collection

Object References Figure 5-3e-g e) allocating an object; f) assigning null to a reference variable; g) assigning a reference with a null value

Object References (arrays) An array of objects Is actually an array of references to the objects Example Integer[] scores = new Integer[30]; Instantiating Integer objects for each array reference scores[0] = new Integer(7); scores[1] = new Integer(9); // and so on …

Object References (equality) Equality operators ( == and != ) Compare the values of the reference variables, not the objects that they reference equals method Compares objects by contents

Object References When an object is passed to a method as an argument, the reference to the object is copied to the method’s formal parameter public void changeInteger(Integer n) { n=new Integer(5); } Integer p=new Integer(7); changeInteger(p);

Reference-Based Linked Lists Linked list Contains nodes that are linked to one another A node Contains both data and a “link” to the next item Can be implemented as an object public class Node { private Object item; private Node next; // constructors, accessors, // and mutators … } // end class Node Figure 5-5 A node

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) { item = newItem; next = nextNode; } // end constructor 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

Reference-Based Linked Lists Using the Node class Node n = new Node (new Integer(6)); Node first = new Node (new Integer(9), n); Figure 5-7 Using the Node constructor to initialize a data field and a link value

Reference-Based Linked Lists Data field next in the last node is set to null head reference variable References the list’s first node Always exists even when the list is empty Figure 5-8 A head reference to a linked list

Reference-Based Linked Lists public class ListReferenceBased implements ListInterface { // reference to linked list of items private Node head; private int numItems; // number of items in list public ListReferenceBased() { numItems = 0; head = null; } // end default constructor

Programming with Linked Lists: Displaying the Contents of a Linked List curr reference variable References the current node Initially references the first node To display the data portion of the current node System.out.println(curr.getItem()); To advance the current position to the next node curr = curr.getNext();

Displaying the Contents of a Linked List Figure 5-10 The effect of the assignment curr = curr.getNext( )

Displaying the Contents of a Linked List To display all the data items in a linked list for (Node curr = head; curr != null; curr = curr.getNext()) { System.out.println(curr.getItem()); } // end for

Deleting a Specified Node from a Linked List To delete node N which curr references Set next in the node that precedes N to reference the node that follows N prev.setNext(curr.getNext()); Figure 5-11 Deleting a node from a linked list

Deleting a Specified Node from a Linked List Deleting the first node is a special case head = head.getNext(); Figure 5-12 Deleting the first node

Deleting a Specified Node from a Linked List To return a node that is no longer needed to the system curr.setNext(null); curr = null; Three steps to delete a node from a linked list Locate the node that you want to delete Disconnect this node from the linked list by changing references (Return the node to the system)