Implementing Singly Linked

Slides:



Advertisements
Similar presentations
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Advertisements

Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
An Introduction to Sorting Chapter 11 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Software Engineering Design & UML.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
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.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Chapter 4 Linked Structures.
Lecture 5 of Computer Science II
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Lectures linked lists Chapter 6 of textbook
Review Deleting an Element from a Linked List Deletion involves:
CS2006- Data Structures I Chapter 5 Linked Lists I.
Data Structure and Algorithms
Methods Chapter 6.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Pointers, Polymorphism, and Memory Allocation
Chapter 4 Linked Lists.
Reference: COS240 Syllabus
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
An Introduction to Sorting
Chapter 19 Java Never Ends
CMSC 341 Lecture 5 Stacks, Queues
Array Lists Chapter 6 Section 6.1 to 6.3
CS 2308 Final Exam Review.
Chapter 4 Link Based Implementations
Sorts.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Exam 1 Review CS 3358.
A Kind of Binary Tree Usually Stored in an Array
Lesson 16: Functions with Return Values
Data Structures and Algorithms
Further Data Structures
Exam 1 Review CS 3358.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Linked Lists.
Doubly Linked List Implementation
List Implementations that Use Arrays
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 8 CSE 331 Sep 16, 2016.
Lecture 8 CSE 331 Sep 15, 2017.
Ch. 2: Getting Started.
Lecture 10 CSE 331 Sep 21, 2012.
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
CS2013 Lecture 7 John Hurley Cal State LA.
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Stacks, Queues, and Deques
TCSS 143, Autumn 2004 Lecture Notes
Linked Lists and Loops based on slides by Ethan Apter
slides created by Ethan Apter
CMSC 202 Exceptions 2nd Lecture.
Skip Lists The non-list List Copyright © 2016 Curt Hill.
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
List Implementations that Use Arrays
Lecture 4 – Data collection List ADT
Presentation transcript:

Implementing Singly Linked Pre-Pointers Insertion Sort Headers Sorted Lists Implementing Singly Linked Lists Riley Ch. 5 (& 3) Handouts SMD181 Lecture 15 Multiple Iterators MasterMind – Code – Iterator Integrity

Lecture 15 - Håkan Jonsson News Book a time slot for the design review on Wednesday, Feb 28 ASAP. You may work in pairs on the design. You still have to write your own program and have that graded. Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Headers The implementation of a singly linked listed we saw last lecture can be improved in two ways. 1. Always having a “dummy” node at the beginning of the internal node list. This node is never used to store data. It makes operations on the list simpler. Called a header. Pictures… (Ch. 5.8 in Riley) Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Figures Fig. 5.29, no header node p. 227, same as 5.29 but with a header node. Fig. 5.30, two operations Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Pre-Pointers 2. Another improvement: Let the internal iterator reference variable refer to the node immediately before the iterators current position(!) Is called a pre-pointer. Possible since we have a header (and therefore always one node before another). Makes the implementation of the iterator simpler. Pictures… Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Figures Fig. 5.32, without and with a pre-pointer p. 232-233, operations removeAT and insertBefore Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Sorted Lists (Riley 6.1) A class for lists in which the elements are ordered (automatically, it seems). ADT in Fig. 6.2. Different from an ordinary list. Stores objects that are Comparable. Comparable is an interface that specifies a method public int compareTo(Object o) The method insert keeps the list ordered. Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson (Sorted Lists) Implementation: Has a (private) object of type SimpleSingleListPlus. Fig. 6.1 The method insert works like insert in Insertion Sort. Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Figures Fig. 6.1 Fig. 6.2 Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Insertion Sort The Sorting Problem: Input: n comparable elements in an array. Output: An array with the n input elements in sorted order. Insertion sort: Inserts elements from the input array, one at a time, into a sorted array while it keeps the array sorted. Like when you play cards, pick up one card at a time, and find its proper place. Lecture 15 - Håkan Jonsson

Copyright © The McGraw-Hill Companies, Inc. Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson MasterMind Last lecture I handed out an example of a design of MasterMind, a board game. I managed to find also a (simple) implementation! Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson MasterMind To note: The class Command has a static method build that produces commands. This is a Command-factory. Each command class inherits Command. So, they all must have a method exec. In the Game class there is a loop in which user input is read and translated into a command, which is then executed. (Small difference: There is no interface Pegs.) Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Multiple Iterators We have seen examples where containers (simple singly linked lists) have one iterator each. This limits what can be done with the container. Fig. 6.6, using two iterators. Better: Separate the list (the container) from the iterator(s). Fig. 6.5 Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson A Dilemma A common problem is when two or more classes need access to variables in a third class. Making the variables public means they are accessible also to other classes, which is usually bad (dangerous). A solution is to make one class an inner class of the other! The inner class has full access to the declarations of outer class, also private things. This is how multiple iterators are implemented in the course book. Fig. 6.8. Lecture 15 - Håkan Jonsson

Lecture 15 - Håkan Jonsson Figures Fig. 6.6 (Removing duplicates using two iterators) Fig. 6.5 (UML-diagram of the list and its iterator) Fig. 6.7 + 6.8 (Final solution) Lecture 15 - Håkan Jonsson