Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Similar documents
1 P age DS & OOPS / UNIT II

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Dynamic Data Structures

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Abstract Data Types 1

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Abstract Data Types 1

Information Science 2

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Sequential Containers Cont'd

Programming Abstractions

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

STACKS AND QUEUES. Problem Solving with Computers-II

csci 210: Data Structures Stacks and Queues

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Largest Online Community of VU Students

Priority Queue ADT. Revised based on textbook author s notes.

Queues. Queue ADT Queue Implementation Priority Queues

Linked Lists

CS350 - Exam 1 (100 Points)

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Sequential Containers Cont'd

Midterm Practice TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

8. Fundamental Data Structures

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Midterm Exam (REGULAR SECTION)

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

UNIVERSITY REGULATIONS

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

[2:3] Linked Lists, Stacks, Queues

Programming. Lists, Stacks, Queues

CS 455 MT 2 Sample Questions

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

Sequence Abstract Data Type

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CS 106B Lecture 5: Stacks and Queues

CS24 Week 4 Lecture 2

16. Dynamic Data Structures

811312A Data Structures and Algorithms, , Exercise 1 Solutions

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Largest Online Community of VU Students

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

I want an Oompa-Loompa! screamed Veruca. Roald Dahl, Charlie and the Chocolate Factory

== isn t always equal?

CS302 - Data Structures using C++

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CMSC 341. Deques, Stacks and Queues 9/22/04 1

Linked Structures. See Section 3.2 of the text.

15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00

CS 216 Exam 1 Fall SOLUTION

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

.:: UNIT 4 ::. STACK AND QUEUE

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

ADTs: Stacks and Queues

III Data Structures. Dynamic sets

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Object Oriented Programming COP3330 / CGS5409

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Largest Online Community of VU Students

Lecture 10 Notes Linked Lists

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Data Structure. Recitation VII

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

infix expressions (review)

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Linked List Implementation of Queues

CSCE 2014 Final Exam Spring Version A

PA3 Design Specification

Lecture 10 Notes Linked Lists

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

DataStruct 5. Stacks, Queues, and Deques

Stacks and queues (chapters 6.6, 15.1, 15.5)

SYSC 2006 Winter 2012 Linear Collections: Queues

CS 222: Linked Lists, Queues, Stacks

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

Transcription:

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let us look at it from the user s point of view. All the user needs to know is that he/she can insert items into the Bag, remove them, count them, and iterate through the items in the Bag, without having to know how exactly the items are stored within the Bag. There is more than one way of implementing the Bag class with the same interface and behavior, your solution to Homework 1 being one of them. Project 2 will let you implement the Bag class in a different way, without altering the interface; that is, a program that uses the previous version of the Bag class should work with the new version without any modification. Such a division of interface and implementation is called abstraction. The user is said to be abstracted from the implementation. We ll do this for many of the data structures we ll see in this class. Doubly Linked Lists We studied linked lists last week. Each node in a linked list keeps a pointer to the next node. This time, let us create a linked list whose nodes have two links each -- one to the next node (next) and another to the previous one (prev). It s called a doubly linked list, and it looks like this: head tail doubly-linked list with 4 nodes Insertion Suppose I want to add a new node. Where shall it be? You are free to add it anywhere you want in the list, but since we have the head pointer that points to the first node, it might be easier to put it in front. Last week, for a (singly) linked list, we simply created a new node, made the new node point to the first one, and then updated the head pointer. However, a node in the doubly linked list has an extra pointer prev, which complicates our problem. Before: head tail After: head tail Let us first figure out how to set next and prev for the new node. Because it is going to be the first one in the list, there will be no previous node. Thus we set prev to NULL. next should point to the node that is in front before the insertion, which is currently pointed by head (and head may point to NULL, no problem.) So for the new node, 1) Set new node s prev to NULL. 2) Set new node s next to head. Copyright 2011 Brian Choi Week 3, Page 1/7

There are two more things we need to update at this stage. head is pointing to a wrong one, and the original head node s prev should be updated to point to the newly added node. However, there may be no node in the list, in which case there is no such prev to set. Therefore, we set prev only if the list is not empty before the insertion. 3) If the list was empty before the insertion, just make head point to the new node. 4) If not, set head s prev to the new node, and then make head point to the new node. If the tail pointer is present and is pointing to NULL, the list must have been empty. We should set tail to point to the new node in this case. Otherwise, since we added a new node to the front, tail shouldn t change. This completes the insertion process. Question: Think about how the algorithm should change if the new node should go to the end of the list. Search You can apply the same searching algorithm you used for (singly) linked lists. Removal Removing a node involves searching the node to remove, fixing the links such that the resulting list is a valid doubly linked list, and actually deleting the node from the memory. There can be four cases, though, and for each case we have to do different things. The node s location determines our action. Suppose we performed a search and have a pointer p that points to the searched node. (Is this enough? We needed two pointers for a singular linked list.) Let us do the following: - We can check if the node is the head by checking p == head. Let this boolean be A. - Also, check if the node is the tail by checking p == tail. Let this boolean be B. Case 1 (A, but not B): The node is the head of the list, and there is more than one node in the list. Case 2 (B, but not A): The node is the tail of the list, and there is more than one node in the list. Case 3 (A and B): The node is both the head and the tail of the list (i.e. it is the only node). Case 4 (not A and not B): The node is in the middle of the list. I will leave what to do for each case as an exercise. I suggest you draw a before-and-after diagram like the one on the previous page, and figure out which links should point to which. Again, remember to delete the node as the last step, since you might delete a pointer that you must know in order to fix the broken link. Some of the above cases can be combined when you actually implement it. NOTE: The textbook presents a circular doubly linked list, which uses a dummy node to get rid of special cases. The doubly linked list here is different from the textbook s version. The linear version is presented in this handout for the sake of diversity (so you get a chance to see and deal with a different version). For Project 2, you are allowed to use either one. In fact, most people will choose to implement a circular linked list, because it is slightly easier, with less special cases to worry about. Copyright 2011 Brian Choi Week 3, Page 2/7

Stacks A stack is just another data structure we ll see. It doesn t mean anything more than what you think it means. It is a bunch of items stacked on top of another. Suppose I have a stack of 5 items. What should happen if I add an item to this stack? Naturally, it should go onto the top of the stack. Now the stack has 6 items. Now maybe I want to pick one item up from the stack (imagine picking up a newspaper from a pile of them.) Which one should I pick up? It is probably easiest to take the one on the top. As a result, the state of the stack will return to what we had before -- one with 5 items. In summary, a stack is a Last-In-First-Out data structure. Interface A stack interface should support the two operations described above: 1) push() : add a new item onto the stack 2) pop() : remove the top item from the stack Remember how we talked about every data structure needs to support three operations, namely insert, remove, and search? push() and pop() correspond to insert and remove operations, respectively, and they define what a stack is. A stack features a limited search operation -- you can only look at the top item. See the next page for an example Stack interface. Implementation Like other ADTs, there are many ways of implementing a stack. Let us use an array as an example. Just to make the programming task easier, let us keep a counter for the number of items currently in the stack. private:! ItemType m_items[max_items];! // MAX_ITEMS is our capacity! int m_numitems;!! // assume it s initialized to 0 For now, suppose that ItemType is typedef-ed to be int. Now suppose this is what I have in main: Stack s; s.push(18); Copyright 2011 Brian Choi Week 3, Page 3/7

class Stack { public: bool push(const ItemType& item); // true if successful ItemType pop(); // pop bool empty() const; // true if empty int count() const; // number of items! private:!! // Some data structure that keeps the items. }; Stack Interface The natural place to insert the new value is the front (or the first available slot) of the array. (Notice this position is conveniently marked by m_numitems.) So we will add 18 to the position 0, and then increment m_numitems. After this, m_items will look like this: 18 Question: Write the C++ implementation of push() function. Now let us add more items to the array. Suppose my array is in the following state at some point: 18 30 22 43 7 5 Question: Just looking at the array, what can you say about this stack? Which one was added most recently? Which one was added least recently? Consider the above line of code. It calls pop() on s and store the value into x, if s is not empty. ItemType x; if (!s.empty()) x = s.pop(); Copyright 2011 Brian Choi Week 3, Page 4/7

Question: What will the array look like after the execution of the above code? How about the values of x and m_numitems? x = m_numitems = Question: Provide the C++ implementation of pop() function. Page 298 of the textbook has an array-based implementation of a stack similar to one we have looked here. It is also possible to implement it using a linked list. Notice that whichever form of implementation we use, we need two information: 1) how to check for emptiness, and 2) where the top is. In the array version, m_numitems does the job for us. For a linked list, we can use the head pointer to indicate the top position and NULL to check for emptiness. Copyright 2011 Brian Choi Week 3, Page 5/7

Queues A queue is very similar to a stack, but differs in that it is a FIFO (First-In-First-Out) structure. class Queue {! public: bool enqueue(const ItemType& item);!// push ItemType dequeue();!!!! // pop bool empty() const;!!!! // true if empty int count() const;!!!! // number of items! private:!! // some data structure that keeps the items }; Interface A stack interface should support the two operations described above: 1) enqueue() : add a new item to the back of the queue 2) dequeue() : remove the item in front of the queue Here is an example run, assuming Item is typedef-ed to be int. Queue q; q.enqueue(1); q.enqueue(2); cout << q.dequeue() << endl;! // prints 1 q.enqueue(3); cout << q.dequeue() << endl;! // prints 2 Queue Interface Implementation Again, let us use an array to implement it. Assume the array has a fixed capacity of MAX_ITEMS. We will insert items in the same order as we did for a stack, and suppose we have the following array at some point. 18 30 22 43 7 18 is the first item that was added, or enqueued, and thus when we call dequeue(), 18 must be returned and removed. Then our resulting queue must begin with 30 and end at 7. The starting position has changed. When we add an item, say 5, then it should be added to the end of the array, after 7, thus shifts the ending position. After dequeue() and enqueue(5), we ll have: 30 22 43 7 5 Copyright 2011 Brian Choi Week 3, Page 6/7

This is different from the stack s case, where the starting position was always 0. For a stack, it was enough to keep the number of items, yet for a queue, we need an extra variable that keeps track of where the queue begins. private:! ItemType m_items[max_items];! // MAX_ITEMS is our capacity! int m_numitems;!! // assume it s initialized to 0! int! m_startpos;!! // starting position m_startpos marks the starting position, which we will shift by 1 (i.e. m_startpos++) at each dequeue() call. Then when we enqueue a new item, we can put the new item to the position (m_startpos + m_numitems). Wait, is this correct? Consider the following situation: suppose we kept enqueueing and dequeueing, and at some point we have the following array: 4 5 12 7 There are only 4 items in the queue, thus we should be able to add more items to the queue. Yet (m_startpos + m_numitems) points to a location that s out of bounds. Looking at the figure above, it seems we can start adding more items to the front of the array. So we must wrap back when we hit the end of the array. A CS32 student should be able to figure out how to do it with a simple trick. Question: Give a C++ implementation of the function enqueue(). Question: Give a C++ implementation of the function dequeue(). Challenge(?) Question: Can you implement a queue if you re given two stacks of the same size, instead of an array? Question: Do you think you can implement Stack and Queue with a linked list/doubly-linked list? Copyright 2011 Brian Choi Week 3, Page 7/7