© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Linked Lists Chapter 4.
Chapter 24 Lists, Stacks, and Queues
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
CHAPTER 7 Queues.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
CMPT 225 Stacks.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Final Review.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
April 27, 2017 COSC Data Structures I Review & Final Exam
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Advanced Data Structures Lecture 1
Linked Data Structures
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Queues Chapter 8 (continued)
Chapter 12 – Data Structures
Pointers and Linked Lists
CC 215 Data Structures Queue ADT
Linked Lists Chapter 5 (continued)
CS210- Lecture 3 Jun 6, 2005 Announcements
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
CMPT 225 Lecture 8 – Queue.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review

© 2006 Pearson Addison-Wesley. All rights reserved8 A-2 Topics Covered Java review Software Engineering Recursion –(and a little algorithm efficiency) Abstract Data Types Linked Lists Stacks Queues

© 2006 Pearson Addison-Wesley. All rights reserved8 A-3 Topics Covered Roughly: chapters 1-8 Minus chapter 6... though we did some of it in class We didn’t read the second half of ch. 7, but we covered it in class The algorithm efficiency was not in any of these chapters

© 2006 Pearson Addison-Wesley. All rights reserved8 A-4 Java Review You should be able to read Java code –You don’t need to memorize a bunch of java.util classes You should be able to write Java code –Defining your own classes, interfaces, methods, etc.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-5 Software Engineering What is a good solution to a problem? Object oriented design –Read a UML diagram –Write a UML diagram Modularity, modifiability, readability, ease of use, fail-safe programming…

© 2006 Pearson Addison-Wesley. All rights reserved8 A-6 Recursion Write recurrence relations –e.g. rabbits(n) = rabbits(n-1) + rabbits(n-2) Write recursive (Java) methods Solve problems with simple recursive solutions Backtracking Relation with induction

© 2006 Pearson Addison-Wesley. All rights reserved8 A-7 Algorithm Efficiency Big-O notation: –What does O(n) mean? –What about O(n 2 )? Count the steps of an algorithm, translate to big-O

© 2006 Pearson Addison-Wesley. All rights reserved8 A-8 Abstract Data Types Difference: –ADT vs. data structure How do you specify ADTs? Designing the right ADT for a problem ADT List and SortedList

© 2006 Pearson Addison-Wesley. All rights reserved8 A-9 Linked Lists Object references Using nodes and references Programming with linked lists –inserting, deleting, traversing Variations –tail references, circular lists, doubly linked

© 2006 Pearson Addison-Wesley. All rights reserved8 A-10 Stacks ADT Stack –LIFO Implementations Problem solving with Stacks

© 2006 Pearson Addison-Wesley. All rights reserved8 A-11 Queues ADT Queue –FIFO Implementations Problem solving with Queues

© 2006 Pearson Addison-Wesley. All rights reserved8 A-12 Example 1 Write a pre-condition, post-condition, and loop invariant for the following: public int Factorial(int n) { int val = 1; for(int i=1; i<=n; i++) val = val*i; return val; }

© 2006 Pearson Addison-Wesley. All rights reserved8 A-13 Example 1 - Solution Pre-condition: –n is an integer greater than or equal to 1 Post-condition: –returned value is n! Loop invariant: –after iteration i of the for loop, val is equal to i!

© 2006 Pearson Addison-Wesley. All rights reserved8 A-14 Example 2 What is wrong with this: void printN(int n) { System.out.println(n); printN(n-1); }

© 2006 Pearson Addison-Wesley. All rights reserved8 A-15 Example 2 - Solution No base case! It will run forever

© 2006 Pearson Addison-Wesley. All rights reserved8 A-16 Example 3 Write a recursive method that takes input n, and prints the numbers from n to 0.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-17 Example 3 - Solution void printUpToN(int n) { if(n<0) System.out.print(“”); else { System.out.print(n); printUpToN(n-1); }

© 2006 Pearson Addison-Wesley. All rights reserved8 A-18 Example 4 Using only the List ADT operations, write a pseudocode method swap(List list, int i, int j) that exchanges positions i and j in List

© 2006 Pearson Addison-Wesley. All rights reserved8 A-19 Example 4 - Solution swap(List list, int i, int j): int temp = list.retrieve(i); list.add(j+1,temp); temp = list.retrieve(j); list.remove(j); list.add(i+1,temp); list.remove(i);

© 2006 Pearson Addison-Wesley. All rights reserved8 A-20 Example 5 Specify an abstract data type for a Bag... items can be inserted, contents dumped out, removed one by one

© 2006 Pearson Addison-Wesley. All rights reserved8 A-21 Example 5 - Solution ADT Bag +createBag() //Creates an empty bag +isEmpty():boolean{query} //Determines if the Bag is empty +add(in newItem:BagItemType) //Adds newItem to the Bag +removeAll() //Removes all items from the Bag +pickRandom() //Removes a random item from the Bag

© 2006 Pearson Addison-Wesley. All rights reserved8 A-22 Example 6 Write pseudocode to add the contents of one list to the end of another list

© 2006 Pearson Addison-Wesley. All rights reserved8 A-23 Example 6 - Solution append(List list1, List list2) newList.createList(); while(!list1.isEmpty()) newList.add(newList.size()+1,list1.get(1)); list1.remove(1); while(!list2.isEmpty()) newList.add(newList.size()+1,list2.get(1)); list2.remove(1); return newList

© 2006 Pearson Addison-Wesley. All rights reserved8 A-24 Example 7 Same thing... using Queue ADT

© 2006 Pearson Addison-Wesley. All rights reserved8 A-25 Example 7 - Solution append(Queue queue1, Queue queue2) newQueue.createQueue(); while(!queue1.isEmpty()) newQueue.enQueue(queue1.deQueue()); while(!queue2.isEmpty()) newQueue.enQueue(queue2.deQueue()); return newQueue

© 2006 Pearson Addison-Wesley. All rights reserved8 A-26 Example 8 Write a pseudocode method to count the number of items in a linked list.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-27 Example 8 – Solution (Iterative) numElements(LinkedList ll) Node curr = head; int count = 0; while(curr.getNext()!=null) count++; curr = curr.getNext(); return count;

© 2006 Pearson Addison-Wesley. All rights reserved8 A-28 Example 9 Write a pseudocode method to find the largest element in a linked list of integers

© 2006 Pearson Addison-Wesley. All rights reserved8 A-29 Example 9 - Solution largestElement(LinkedList ll) if(head==null) throw exception Node curr = head; int largest = head.getItem(); while(curr.getItem()!=null) if(largest<curr.getItem()) largest = curr.getItem() curr=curr.getNext(); return largest;

© 2006 Pearson Addison-Wesley. All rights reserved8 A-30 Example 10 Given one possible explanation for a StackException that is thrown from a push operation

© 2006 Pearson Addison-Wesley. All rights reserved8 A-31 Example 10- Solution Explanation: –The Stack ADT was implemented with a fixed size array, and it has reached maximum capacity.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-32 Example 11 What are contents of aStack following this: aStack.createStack(); if(aStack.isEmpty()) aStack.push(4); push(5); pop() popall(); push(6); push(7);

© 2006 Pearson Addison-Wesley. All rights reserved8 A-33 Example 11 - Solution What are contents of aStack following this: aStack.createStack(); if(aStack.isEmpty()) aStack.push(4); push(5); pop() popall(); push(6); push(7); 7676

© 2006 Pearson Addison-Wesley. All rights reserved8 A-34 Example 12 Briefly explain/define these terms: –circular linked list –data structure –encapsulation

© 2006 Pearson Addison-Wesley. All rights reserved8 A-35 Example 12 Briefly explain/define these terms: –circular linked list a linked list where tail node references the head –data structure a construct in a programming language that stores data –encapsulation objects keep internal data enclosed internally, hiding it from view and preventing it from being accessed

© 2006 Pearson Addison-Wesley. All rights reserved8 A-36 Example 13 Given the following running times for input n, what is the big-O measure of efficiency –n + 6n steps –n2 + n(n 2 + 5) steps –log n + n steps

© 2006 Pearson Addison-Wesley. All rights reserved8 A-37 Example 13 - Solution Given the following running times for input n, what is the big-O measure of efficiency –n + 6n steps O(n) –n2 + n(n 2 + 5) steps O(n 3 ) –log n + n steps O(n)

© 2006 Pearson Addison-Wesley. All rights reserved8 A-38 Example 14 Which ADT is appropriate for each type of data: –An alphabetic list of names –Airplanes that stack above a busy airport waiting to land –An employer that is laying off employees, starting with the most recent hires

© 2006 Pearson Addison-Wesley. All rights reserved8 A-39 Example 14 - Solution Which ADT is appropriate for each type of data: –An alphabetic list of names LIST –Airplanes that stack above a busy airport waiting to land QUEUE –An employer that is laying off employees, starting with the most recent hires STACK

© 2006 Pearson Addison-Wesley. All rights reserved8 A-40 Example 15 Give UML diagram from requirements: –A drawing program is to allow kids to draw all sorts of shapes: lines, rectangles, and circles. While using this program, kids select the shape they wish to draw, the colour of the shape and they indicate the shape's position on the canvas (a canvas is a drawing area on a window created by the drawing program). –The position of a line is indicated by the x and y coordinates of its end points. –The position of a rectangle is indicated by the x and y coordinates of its top left corner and the x and y coordinates of its bottom right corner. –The position of a circle is indicated by the x and y coordinates of its center and the length of its radius.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-41 Example 15 - Solution colour shape display( ) or draw( ) one end x, one end y other end x other end y line topleft x topleft y bottomright x bottomright y rectangle center x center y radius circle display( ) or draw( )

© 2006 Pearson Addison-Wesley. All rights reserved8 A-42 More types of examples Predict the output of Java code.... Recursive methods for traversal... for recognizing classes of strings (e.g. all ‘a’) Defining ADT’s from description... bank account, rectangle Merging sorted lists

© 2006 Pearson Addison-Wesley. All rights reserved8 A-43 More types of examples Displaying contents of a Queue or Stack Writing data structure for an ADT... say the Bag from earlier... need to decide data members Inserting nodes in doubly linked/circular linked lists