Programming Abstractions

Slides:



Advertisements
Similar presentations
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Advertisements

Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Pointers OVERVIEW.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
List Interface and Linked List Mrs. Furman March 25, 2010.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
slides adapted from Marty Stepp and Hélène Martin
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Chapter 12 – Data Structures
Programming Abstractions
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Building Java Programs
Doubly Linked List Review - We are writing this code
Programming Abstractions
slides created by Marty Stepp
Chapter 4 Linked Lists
LinkedList Class.
public class StrangeObject { String name; StrangeObject other; }
Top Ten Words that Almost Rhyme with “Peas”
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Object Oriented Programming COP3330 / CGS5409
Chapter 18: Linked Lists.
slides created by Marty Stepp and Ethan Apter
Linked List (Part I) Data structure.
Building Java Programs
Building Java Programs
CSC 143 Queues [Chapter 7].
Lecture 7: Linked List Basics reading: 16.2
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
slides adapted from Marty Stepp and Hélène Martin
slides created by Marty Stepp and Hélène Martin
Building Java Programs
Building Java Programs
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
CSE 12 – Basic Data Structures
Building Java Programs
CSC 143 Java Linked Lists.
slides created by Marty Stepp
slides adapted from Marty Stepp
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
slides adapted from Marty Stepp
TCSS 143, Autumn 2004 Lecture Notes
slides created by Marty Stepp
slides created by Ethan Apter
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
slides created by Marty Stepp
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Topics: Welcome, ProFros! This week: Memory and Pointers Monday: revisit some topics from last week in more detail: Deeper look at new/delete dynamic memory allocation Deeper look at what a pointer is Today: Finish up the music album example Linked nodes Friday: Linked List data structure (if we have time) priority queues and binary trees Welcome, ProFros!

Linked Nodes A great way to exercise your pointer understanding

Linked Node We can chain these together in memory: data next 10 data struct LinkNode { int data; LinkNode *next; } We can chain these together in memory: LinkNode *node1 = new LinkNode; // complete the code to make picture node1->data = 10; node1->next = NULL; LinkNode *node = new LinkNode; node->data = 10; node->next = node1; data next 10 data next 75 NULL node

FIRST RULE OF LINKED NODE/LISTS CLUB: DRAW A PICTURE OF LINKED LISTS Do no attempt to code linked nodes/lists without pictures!

List code example: Draw a picture! struct LinkNode { int data; LinkNode *next; } Before: front->next->next = new LinkNode; front->next->next->data = 40; After: Using “next” that is NULL gives error Other/none/more than one data next 10 data next 20 NULL front data next 10 data next 40 data next 20 NULL front data next 10 data next 20 data next 40 NULL front

FIRST RULE OF LINKED NODE/LISTS CLUB: DRAW A PICTURE OF LINKED LISTS Do no attempt to code linked nodes/lists without pictures!

Linked List Data Structure Putting the ListNode to use

A LinkedList class Let's write a collection class named LinkedList. Has the same public members as ArrayList, Vector, etc. add, clear, get, insert, isEmpty, remove, size, toString The list is internally implemented as a chain of linked nodes The LinkedList keeps a pointer to its front node as a field NULL is the end of the list; a NULL front signifies an empty list LinkedList front add(value) insert(index, value) remove(index) size() toString() ... ListNode ListNode ListNode data next 42 data next -3 data next 17 element 0 element 1 element 2 Notice that LinkedList and ListNode are DIFFERENT. One has one ptr in it (front), the other has data and a next ptr.

Traversing a list? (BUG version) What's wrong with this approach to traverse and print the list? while (front != NULL) { cout << front->data << endl; front = front->next; // move to next node } It loses the linked list as it is printing it! data next 10 data next 20 data next 990 front ...

Traversing a list (12.2) (bug fixed version) The correct way to print every value in the list: ListNode* current = list; while (current != NULL) { cout << current->data << endl; current = current->next; // move to next node } Changing current does not damage the list. data next 10 data next 20 data next 990 front ...

LinkedList.h LinkedList front = class LinkedList { public: void add(int value); void clear(); int get(int index) const; void insert(int index, int value); bool isEmpty() const; void remove(int index); void set(int index, int value); int size() const; private: ListNode* front; }; LinkedList front =

Implementing add // Appends the given value to the end of the list. void LinkedList::add(int value) { ... } What pointer(s) must be changed to add a node to the end of a list? What different cases must we consider? front = data next 42 data next -3 data next 17 element 0 element 1 element 2

Case 1: Add to empty list Before adding 20: After: We must create a new node and attach it to the list. For an empty list to become non-empty, we must change front. data next 20 front = front = element 0

Case 2: Non-empty list Before adding value 20 to end of list: After: front = data next 42 data next -3 element 0 element 1 data next 42 front = data next -3 data next 20 element 0 element 1 element 2

Don't fall off the edge! Must modify the next pointer of the last node. Where should current be pointing, to add 20 at the end? Q: What loop test will stop us at this place in the list? A. while (current != NULL) { ... B. while (front != NULL) { ... C. while (current->next != NULL) { ... D. while (front->next != NULL) { ... front = data next 42 data next -3 element 0 element 1

Code for add // Adds the given value to the end of the list. void LinkedList::add(int value) { if (front == NULL) { // adding to an empty list front = new ListNode(value); } else { // adding to the end of an existing list ListNode* current = front; while (current->next != NULL) { current = current->next; } current->next = new ListNode(value);

Implementing get // Returns value in list at given index. int LinkedList::get(int index) { ... } data next 42 front = data next -3 data next 17 element 0 element 1 element 2

Code for get // Returns value in list at given index. // Precondition: 0 <= index < size() int LinkedList::get(int index) { ListNode* current = front; for (int i = 0; i < index; i++) { current = current->next; } return current->data;

Implementing insert // Inserts the given value at the given index. void LinkedList::insert(int index, int value) { ... } data next 42 front = data next -3 data next 17 element 0 element 1 element 2

Inserting into a list Before inserting element at index 2: After: Q: How many times to advance current to insert at index i ? A. i - 1 B. i C. i + 1 D. none of the above front = data next 48 data next -3 data next 17 element 0 element 1 element 2 data next 48 data next -3 data next 22 data next 17 front = element 0 element 1 element 2 element 3

Code for insert // Inserts the given value at the given index. // Precondition: 0 <= index <= size() void LinkedList::insert(int index, int value) { if (index == 0) { // adding to an empty list front = new ListNode(value, front); } else { // inserting into an existing list ListNode* current = front; for (int i = 0; i < index - 1; i++) { current = current->next; } current->next = new ListNode(value, current->next);

Implementing remove // Removes value at given index from list. void LinkedList::remove(int index) { ... } What pointer(s) must be changed to remove a node from a list? What different cases must we consider? front = data next 48 data next -3 data next 22 data next 17 element 0 element 1 element 2 element 3

Removing from a list Before removing element at index 2: After: Where should current be pointing? How many times should it advance from front? front = data next 48 data next -3 data next 22 data next 17 element 0 element 1 element 2 element 3 data next 48 data next 17 front = data next -3 element 0 element 1 element 2 data next 22 trash

Removing from front Before removing element at index 0: After: To remove the first node, we must change front. front = data next 42 data next -3 data next 20 element 0 element 1 element 2 data next -3 front = data next 20 element 0 element 1 data next 42 trash

Removing the only element Before: After: We must change the front field to store NULL instead of a node. Do we need a special case to handle this? front = data next 20 front = element 0

Code for remove // Removes value at given index from list. // Precondition: 0 <= index < size() void LinkedList::remove(int index) { ListNode* trash; if (index == 0) { // removing first element trash = front; front = front->next; } else { // removing elsewhere in the list ListNode* current = front; for (int i = 0; i < index - 1; i++) { current = current->next; } trash = current->next; current->next = current->next->next; delete trash;

Other list features Add the following public members to the LinkedList: size() isEmpty() set(index, value) clear() toString() Add a size field to the list to return its size more efficiently. Add preconditions and exception tests as appropriate.

Priority Queue Emergency Department waiting room operates as a priority queue: patients are sorted according to priority (urgency), not “first come, first serve” (in computer science, “first in, first out” or FIFO). Image is in the public domain. http://commons.wikimedia.org/wiki/File:Columbus_Fire_Medic_7.JPG

Some priority queue implementation options data next 75 data next 8 data next 20 NULL head Unsorted linked list Insert new element in front Remove by searching list for highest-priority item Sorted linked list Always insert new elements where they go in priority-sorted order Remove from front (will be highest-priority because sorted) data next 8 data next 20 data next 75 NULL head

Unsorted linked list Priority queue implementations Add is FAST data next 75 data next 8 data next 20 NULL head Add is FAST Just throw it in the list at the front O(1) Remove/peek is SLOW Hard to find item the highest priority item—could be anywhere O(N) This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported  license. Keyah Cheatum http://commons.wikimedia.org/wiki/File:Messy_Room.JPG

Sorted linked list Priority queue implementations Add is SLOW data next 8 data next 20 data next 75 NULL head Add is SLOW Need to step through the list to find where item goes in priority-sorted order O(N) Remove/peek is FAST Easy to find item you are looking for (first in list) O(1) http://commons.wikimedia.org/wiki/File:Wall_Closet.jpg Image is in the public domain.

We want the best of both + = Priority queue implementations Fast add AND fast remove/peek We will investigate trees as a way to get the best of both worlds + = Fast add Fast remove/peek