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.

Slides:



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

Singly linked lists Doubly linked lists
Stacks, Queues, and Linked Lists
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Data Structures ADT List
Chapter 17 Linked List Saurav Karmakar Spring 2007.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
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.
Data Structures & Algorithms
1 Recitation 9. Practice with linked lists Introduction. This recitation will go over the linked list implementation given on the back of this sheet and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
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.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-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 3: Arrays, Linked Lists, and Recursion
Chapter 17 Linked List.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
1 Writing a Good Program 8. Elementary Data Structure.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Chapter 5 Linked Lists II
Subject Name : Data Structure Using C Title : Linked Lists
© 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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
LINKED LISTS.
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Chapter 4 Linked Structures.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Linked Lists Chapter 5 (continued)
Doubly Linked List Review - We are writing this code
Data Structures and Algorithms
Programming Abstractions
Chapter 4 Linked Lists
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Object Oriented Programming COP3330 / CGS5409
Programming Abstractions
Data Structures and Algorithms
Header and Trailer Sentinels
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Chapter 9 Linked Lists.
Presentation transcript:

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

Reference in Java A must read: Study all the examples there!

Storing a sequence of records Oftentimes, we need to store a sequence of data records. E.g.: »The list of students: ID, names, grades, etc. »All the s, sorted by arrival time »Finishing times of marathon runners »Bank transactions

Storing a sequence of records Need to support these operations »get the i-th record »delete the i-th record, »check if a record exists (get the index, if any) »insert a record to the i-th place Using an array to store such data records. »Pros and cons? »Fast random access (get the i-th record) »Slow insert/delete »Need to know the maximum size beforehand Overflow risk vs. waste of storage

A new data structure: Linked Lists List of integers: [ ] Key differences to array? »Array: random access (directly goes to i-th record) »List: sequential access (follows the references)

Inserting and Deleting Inserting a record Removing a record

Linked Lists List of integers: [ ] public class ListItem{ public int number; public ListItem next; public ListItem(int value, ListItem p){ number = value; next = p; } } Building lists with the constructor »ListItem p = new ListItem(3,null); »ListItem q = new ListItem(7,p); »ListItem r = new ListItem(2,new ListItem(5,null)); »What happens when q=p;? when ListItem d = r.next; ? p 3 7 q r 2 5

toString for the ListItem class public class ListItem{ public int number; public ListItem next; public ListItem(int value, ListItem p){ number = value; next = p; } public String toString() { if (next == null) return number + “ “; else return number + “ “ + next.toString(); } }

Traversing a List Pass over list by advancing object reference Task: get the sum of all ListItem values reachable from a ListItem p p // recursive traversal int sum(ListItem p) { if (p == null) return 0; else return p.number + sum(p.next); } // iterative traversal int sum(ListItem p) { int result = 0; while (p != null) { result += p.number; p = p.next; } return result; }

Implementing a LinkedIntList Class List of integer values with the following operations »addFirst(int x) – add the value x at the front of the list insert new ListItem before current head »addLast(int x) – add the value x at the end of the list add new ListItem after last item »indexOf(int x) – return the index of first item with value x scan the list for first match »remove(int x) – delete the first item with value x scan to the first item and re-direct previous item’s next pointer »size() – return the number of elements in the list uses a stored value Two instance variables »ListItem head – reference to first ListItem (or null) »int size – number of items in the list

Adding Elements // Create new list item and insert before current first element void addFirst(int x) { head = new ListItem(x, head); size++; } // Create new list item and insert it at the end of the list void addLast(int x) { if (head == null) { addFirst(x); } else { // scan to end of list, then insert item ListItem p; for (p = head; p.next != null; p = p.next) {} p.next = new ListItem(x,null); size++; }

Finding and Removing Items in a List // Return index of first item with value x (or -1 if none) int indexOf(int x) { int i = 0; for (ListItem p = head; p != null; p = p.next) { if (p.number == x) return i; i++; } return -1; } // Remove first item with given value void remove(int x) { if (head == null) return; if (head.number == x) { head = head.next; size--; return; } for (ListItem p = head; p.next != null; p = p.next) { if (p.next.number == x) { p.next = p.next.next; size--; return; }}

Doubly Linked Lists Lists with forward and reverse links »public class ListItem{ public int number; public ListItem next; public ListItem prev; } »makes it easy to traverse a list in reverse (by following p.prev) »makes it easier to delete arbitrary elements (e.g. delete the previous item) head size=6 tail

Doubly Linked Lists and Sentinels Lists are sometimes implemented with sentinel nodes at the beginning and end »makes implementation more symmetric »reduces number of special cases in methods »list object includes references to both head and tail nodes »explore it in your studio tomorrow head size=6 tail