16. Linked Structures and Miscellaneous

Size: px
Start display at page:

Download "16. Linked Structures and Miscellaneous"

Transcription

1 16. Linked Structures and Miscellaneous The main purpose of this section is to look at the cool topic of linked data structures. This is where we use one object to point to the next object in a structure of many objects. In this section, we will look at how object reference variables, which contain the address of other objects, can be used to link together interesting, useful, and high performance data structures. Linked data structures will be covered extensively in Cmpt 201, 307, and 354, but you will get a peek here in case you don t take Cmpt 201. Also, the concepts of addresses and pointers are used for other things like: Required Readings: None. Optional Readings: Ch. 10 of [Savitch2001] 16. Linked Structures and Miscellaneous Linked Data Structures Simple Static Linked List Simple List Exercises Vectors Java Collections Classes Summary/Software Engineering Process Final Exam and Office Hours: Copyright 1998 by R. Tront 16-1 Copyright 1998 by R. Tront 16-2

2 16.1 Linked Data Structures One of the lovely things you can do with reference/ pointer/address variables is to create linked data structures in RAM. You can also use linked data structures on disk, though you use the position measured down the file, rather than RAM addresses, as your link specifier. In either case, the data is not necessarily arranged in any kind of order, however the links usually have some order (perhaps increasing value of some attribute) ptrtolowest Each of the top 3 boxes represent an object record. The are called list nodes. The fact is, the data may be scattered all over the heap portion of RAM, or all over a particular file on disk. It is the links that provide the order. Since RAM and binary files allow quickly moving from one place in the heap or in a random-access file to Copyright 1998 by R. Tront 16-3 another, we usually do not draw a linked list as shown above, but in the more conceptual order shown below. ptrtofirst Note that I have changed the name of the pointer to the list head from ptrtolowest to ptrtofirst. Since a list could alternately be in decreasing integer order, this a more general name. The HUGE advantage of these links is that they are easy to change. This makes inserting a new element at the start, or into the middle of a list much faster than making room in the middle of an array for a new element (where you would painstakingly have to move all the elements in the latter portion of the array to the right). Copyright 1998 by R. Tront 16-4

3 16.2 Simple Static Linked List Now, let s look at some code for a simple list that holds integers. Note that this is not a list class, as that would mean we could create multiple lists. Instead this is the simplest list your instructor can use as a first exposure to linked list: one where the shepherd knows where a bunch of other objects (or at least where the first of many sheep that are connected together with rope/ leash). / / F I L E : S t a t i c I n t e g e r L i s t. j a v a / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = c l a s s L i s t N o d e { / / T h i s c l a s s p r o v i d e s t h e n o d e r e c o r d s f o r / / a n i n t e g e r l i s t. int datavalue; L i s t N o d e n e x t ; / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = p u b l i c c l a s s S t a t i c I n t e g e r L i s t { / / P r o v i d e s a s i n g l e l i s t p o i n t e d t o b y a / / s t a t i c v a r i a b l e. T h i s i s l i k e s h e p h e r d / / k n o w i n g w h e r e t h e l i s t o f s h e e p a r e. s t a t i c L i s t N o d e p t r T o F i r s t ; / / p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { i n s e r t A t F r o n t ( 1 3 ) ; i n s e r t A t F r o n t ( 5 ) ; i n s e r t A t F r o n t ( 2 ) ; printlist(); Copyright 1998 by R. Tront 16-5 Copyright 1998 by R. Tront 16-6

4 There are two classes in this file (the file also continues over the following few pages). The first class, which is not public, is just for the list node records. (It would probably more appropriate for it to be a nested class, but we have not studied any of the 4 kinds of nested classes yet, so I have just defined it nonpublic. This unfortunately means that any class in the package can access it, though no other class needs to access it). The next thing is the start of the definition of my static list class. This class s only attribute is a static variable that will be used to point to the head of the list. The class also has a main program that demonstrates the use of other static functions like telling the shepherd to add a node to the front of the list, and to print an inventory of the list. Now let s look at how we would implement these functions: p u b l i c s t a t i c v o i d p r i n t L i s t ( ) { / / p r i n t s l i s t i n f r o n t - t o - b a c k o r d e r. L i s t N o d e t e m p P t r = p t r T o F i r s t ; w h i l e ( t e m p P t r! = n u l l ) { / / D o n ' t e n t e r p r i n t i n g l o o p i f l i s t / / i s e m p t y, s o u s e t o p e x i t l o o p. S y s t e m. o u t. p r i n t l n ( t e m p P t r. d a t a V a l u e ) ; / / c h a n g e t e m p P t r t o r e f e r t o n e x t //element, in preparation for next //iteration of print loop. t e m p P t r = t e m p P t r. n e x t ; Copyright 1998 by R. Tront 16-7 Copyright 1998 by R. Tront 16-8

5 The print function uses a temporary variable to point at intermediate nodes as it is iterating down the list. When you first start, you set this so called list scanner reference variable (sometimes called an iterator) to point at the first element. After a few cycles of the loop, when you are half way down the list, the situation might look like this: ptrtofirst 2 tempptr 5 13 Later, as you get to the end of the list, the algorithm finds that the next attribute of the last node is null (this is what the unconnected dot means in the above diagram). We use the special object reference value called null as a marker for the end of the list. Now let s look at the interesting part: how we build the list: / / p u b l i c s t a t i c v o i d i n s e r t A t F r o n t ( i n t n e w V a l u e ) { i f ( p t r T o F i r s t = = n u l l ) { / / i f n o e l e m e n t s i n t h e l i s t : p t r T o F i r s t = n e w L i s t N o d e ( ) ; p t r T o F i r s t. d a t a V a l u e = n e w V a l u e ; / / N o w m a r k t h i s a s l a s t n o d e i n l i s t. p t r T o F i r s t. n e x t = n u l l ; e l s e { / / p u t i n f r o n t o f f i r s t e l e m e n t. //Need temporary pointer. L i s t N o d e t e m p P t r ; / / N o w c r e a t e a n d f i l l n e w n o d e. t e m p P t r = n e w L i s t N o d e ( ) ; t e m p P t r. d a t a V a l u e = n e w V a l u e ; / / n e w n o d e s h o u l d p o i n t t o / / o l d f i r s t n o d e. tempptr.next = ptrtofirst; //head pointer can point to new node p t r T o F i r s t = t e m p P t r ; Copyright 1998 by R. Tront 16-9 Copyright 1998 by R. Tront 16-10

6 When you are inserting a new element at the front of a list (or middle, or back), you generally have to handle the special case of whether the list might be empty. In the printlist( ) function, we handled it with a top-exit loop. In the insertatfront( ) function, we handle it with an if statement. If the list is empty, then we need to create a new node that is pointed to by ptrtofirst. We also need to insert the new value in the that new node s datavalue attribute. And we need to mark this as the last node. If the list already has some values in it, we need to insert the new node in front of the currently frontmost node. And, we need to make ptrtofirst point at the new first node. BUT, we cannot overwrite the address in ptrtofirst yet, or we will lose the address of the old frontmost node. So we need a temporary object reference variable to hold either the address of the new node, or temporarily hold the value of the old first node. It doesn t really matter which of these two tiny strategies you choose, but you must program things appropriately once you have chose the strategy. I chose to have a temporary hold the address of the new first record. ptrtofirst 1 tempptr Once we construct the new first node, we can fill its datavalue and also point it at the old first node (whose address we can copy from the old value of ptrtofirst). Once I have the new node pointing to the old first node, I no longer need to remember that address in ptrtofirst. So at the end I finally update ptrtofirst so that it will contain the address of the new first node Copyright 1998 by R. Tront Copyright 1998 by R. Tront 16-12

7 16.3 Simple List Exercises Good Exercise: Add a ListNode constructor with two parameters: the value, and the address to put in the next attribute. Then rewrite the StaticIntegerList class to take as much advantage of this ListNode constructor as possible. Good exercise: Add another static attribute that always refers to the last element on the list (sometimes called the tail of the list). You must set this properly when the very first node of the list is added. Good exercise: Write a function that adds an element to the end of the list, using the above tail pointer. Good exercise: Write a function that adds an element to the end of the list without using a tail pointer. It would have to first scan the list to the end (similar to the print loop above), before adding the new element after the old last node. Good exercise: Write a function that removes the first element of the list. Note: If you have functions that both add to the end of the list and also another that removes from the front, you have just made a queue (like a line-up of people at your bank machine or movie theater ticket window). There are many situations where a computer program must use a first in, first out queue like this. Copyright 1998 by R. Tront Using an array for a queue is very slow, because when you remove an element from an array, you have to shift all the elements toward the front. This can be very slow if the array is 10,000 elements long. However, in comparison to an array, a list may take up twice as much space (100% more space) as an array, because each node requires the storage of an integer and also an object reference (i.e. address). But, if each datavalue is, say, a 1000 byte student record rather than a single integer, then the wasted space is only 0.1% rather than being 100% extra space. Good Exercise: Write a function to search the list for a certain datavalue. Good Exercise: Write a list class that, rather that using nodes that contain an integer and a next reference, instead contain a reference to an Object and a next reference. This way we would put any kind of objects in the list, making it more general purpose. Good Exercise: Write a function that searches a list for an student record with a particular student ID. Good Exercise: Write a function that adds a new record after a particular middle record. The middle record might be found with a search for a certain student number or may be just a record count (e.g. insert after the 15th record). Copyright 1998 by R. Tront 16-14

8 Hard Exercise: Insert a record before a particular searched for element. Hard Exercise: Remove the last element from a list. Moderate exercise: Rewrite the above class so the list is an object rather than a static entity. Then in an application program, you can construct and use multiple lists (e.g. the day list, the night list, the green list, etc.) Vectors Early versions of Java provided a somewhat adaptable collection class called a Vector. It was was like an expandable array, and also supported list like features. It is still used somewhat, but has generally been superceded by other new library collection classes. However, Vectors have one advantage in that they are self-protected against multiple threads of a program (or programs) trying to do things to them at the same time, and the links getting all mixed up. This is because Vectors use by default critical section protection. In the newer collection classes, critical section protection is not present by default, but can be added to any class by special constructor means. This means that they newer ones are faster that Vectors if you don t need the critical section protection. Copyright 1998 by R. Tront Copyright 1998 by R. Tront 16-16

9 16.5 Java Collections Classes Newer versions of Java come with a large library of sophisticated collection classes for lists, trees, queues, sets, hash tables, lookup tables, etc. For instance, lookup and compare ArrayList and LinkedList, both of which are new. Most collection classes used in most modern computer languages use the concept of an iterator class. An iterator is an instance of yet another class that is used to point to a particular place in a list. You can ask the iterator object to give you the data from the node it is pointing to, move ahead one node in the list, back up one node in the list, tell you whether it is currently at the head or the tail of the list, insert an element just after the node it is pointing to, rewind to point at the first element of the list. (Rewind is sort of like rewinding a tape). It is common to construct an iterator, rewind it to the beginning, and scan the list by asking the iterator to move ahead one node at a time. You will learn more about iterators in Cmpt Summary/Software Engineering Process You have had a very good introduction to programming. But software engineering is more than just programming. I chose to give you assignments that built on the previous one so that in the end you would have something pretty substantial. It is small in comparison to industrial programs (which are often 100,000 to 1,000,000 lines of code), but in the end was pretty large for first year students. You gained several benefits from this size: The program grew larger than one module, and thus you began to appreciate that architectural planning/layout of a program is a significant design issue (there are different ways to lay it out, and design is chosing between alternatives. You saw that a multi-module program needed carefully planned and specified class declarations/interfaces (not function definitions/bodies) known to programmers of other modules, in order for them to understand exactly how to access your module s variables and functions. These well-defined interfaces provides strong type checking across modules, so you don t catastrophically call a function with the wrong number or type of parameters. Copyright 1998 by R. Tront Copyright 1998 by R. Tront 16-18

10 Since you are unlikely to write 100,000 lines yourself, using other programmer s modules is an essential aspect of any large project. Finally, you gained an appreciation for maintenance. Specifically, you saw that adding features even for a reasonably well thought out increase in program functionality required rather messy changes and adjustments to the various modules. But, if the module interfaces were well thought out, and the module implementations abstract (irrelevant to the calling programmer), then often an implementation change within one module (say array to file storage) could be done without changing any calling modules. In a large project, the typical phases (called the Software Life Cycle) needed are: Requirements Elicitation - getting the customer to state exactly what she needs, or to provide you with enough documentation that you can figure out this yourself. This is NOT easy as customers often either do not know what they want, or don t describe it completely, or don t have time to tell you. Analysis - analyze the information gleaned during requirements elicitation and organize it. Generally this requires making a data model of objects and relationships. Some students on Coop work terms have worked on projects which had 1500 different object classes and their relationships. Also during analysis you have to figure out storage needs, performance needs, security needs, user interface style, training needs, etc. Design - often broken into 4 sub-phases: - External Design - what the user interface will look like during any given stage of any given operation. This is easiest documented with a draft user manual. Student s hate the idea of writing the manual first, but if you don t know what it is supposed to look like, how can you know what to program? - Architectural Design - deals with the macroscopic issues like how many processors, what data and processes on each, what subsystems for each process/program. - Module Design - designing the interfaces for the various modules, including class declarations. This includes what data and (member) functions will be exported from each class and package. And what are the function names, parameter lists, and return types. - Detailed Design - the design of the actual code with flowcharts and selecting certain algorithms (e.g. selection sort vs. quicksort). Implementation and Unit Testing - the writing of the code, and the testing by the author of individual modules. Unit testing is done by writing little main programs called drivers designed to call and test your functions. This is done before your new module is linked into the other 100,000 lines of code in the project, so you won t embarrass yourself when you code messes up the operation of the other 100,000 lines of code. Copyright 1998 by R. Tront Copyright 1998 by R. Tront 16-20

11 System Integration and Integration Testing - where you start linking things together and debugging the link errors and other inter-module incompatibilities. System and Acceptance Testing - where the system as a whole is tested. System testing is done by your company, and acceptance testing by the customer. Note that Integration and testing and system debugging can take up to 40% of the project effort. Failing to budget for this will embarrassingly cause you to be 100/(100-40) = 66% over your initial (imaginary) budget and effort estimates. Installation and Cut-Over and Training - where the system is installed for the first time at the customer site(s). The data from the customer s previous system must be transferred and (hopefully) read by your new software. And all the customer s employees must be pre-trained. Doing this for a huge system like Air Canada which must be running 24 hours a day is a nightmare for programmers. Maintenance - three kinds: debugging errors reported by your software customers, enhancing your software product, and porting your software product to new operating systems, database, network, and hardware as needed. Retirement of the software. I wish you the best of luck on the final exam, and on the rest of your education and a perhaps career deeply involved in this software life cycle! 16.7 Final Exam and Office Hours: Final Exam- Day: Time: Room: Regular Office Hours - are now ended. Special Office Hours: Marks will be posted: Where: In the on-line Gradebook. When: sometime on (Instructor s pet peeve: Don t ask me when they will be ready before this. I will you if they are ready early.) After marks are posted, office hours for appeals will be announced via . Copyright 1998 by R. Tront Copyright 1998 by R. Tront 16-22

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE373 Fall 2013, Second Midterm Examination November 15, 2013 CSE373 Fall 2013, Second Midterm Examination November 15, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to

More information

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley PAIRS AND LISTS 6 GEORGE WANG gswang.cs61a@gmail.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley June 29, 2010 1 Pairs 1.1 Overview To represent data types

More information

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit. Com S 227 Fall 2018 Miniassignment 1 40 points Due Date: Friday, October 12, 11:59 pm (midnight) Late deadline (25% penalty): Monday, October 15, 11:59 pm General information This assignment is to be done

More information

About this exam review

About this exam review Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review

More information

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to

More information

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1.2 ALGORITHMS ALGORITHM An Algorithm is a procedure or formula for solving a problem. It is a step-by-step set of operations to be performed. It is almost

More information

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings. CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please stop promptly

More information

3. Simple Types, Variables, and Constants

3. Simple Types, Variables, and Constants 3. Simple Types, Variables, and Constants This section of the lectures will look at simple containers in which you can storing single values in the programming language C++. You might find it interesting

More information

CSE351 Winter 2016, Final Examination March 16, 2016

CSE351 Winter 2016, Final Examination March 16, 2016 CSE351 Winter 2016, Final Examination March 16, 2016 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. Please stop promptly at 4:20. There are 125 (not 100) points,

More information

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer) COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back

More information

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings. CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please stop promptly

More information

Total Score /15 /20 /30 /10 /5 /20 Grader

Total Score /15 /20 /30 /10 /5 /20 Grader NAME: NETID: CS2110 Fall 2009 Prelim 2 November 17, 2009 Write your name and Cornell netid. There are 6 questions on 8 numbered pages. Check now that you have all the pages. Write your answers in the boxes

More information

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time) CSE 11 Winter 2017 Program Assignment #2 (100 points) START EARLY! Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time) PROGRAM #2: DoubleArray11 READ THE ENTIRE ASSIGNMENT BEFORE STARTING In lecture,

More information

Lecture Notes on Queues

Lecture Notes on Queues Lecture Notes on Queues 15-122: Principles of Imperative Computation Frank Pfenning Lecture 9 September 25, 2012 1 Introduction In this lecture we introduce queues as a data structure and linked lists

More information

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin CS 61B Summer 2005 (Porter) Midterm 2 July 21, 2005 - SOLUTIONS Do not open until told to begin This exam is CLOSED BOOK, but you may use 1 letter-sized page of notes that you have created. Problem 0:

More information

Total Score /15 /20 /30 /10 /5 /20 Grader

Total Score /15 /20 /30 /10 /5 /20 Grader NAME: NETID: CS2110 Fall 2009 Prelim 2 November 17, 2009 Write your name and Cornell netid. There are 6 questions on 8 numbered pages. Check now that you have all the pages. Write your answers in the boxes

More information

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Robert Recorde room Swansea, December 13, 2013.

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Robert Recorde room Swansea, December 13, 2013. Computer Science Department Swansea University Robert Recorde room Swansea, December 13, 2013 How to use the revision lecture The purpose of this lecture (and the slides) is to emphasise the main topics

More information

CS 261 Spring 2013 Midterm

CS 261 Spring 2013 Midterm CS 261 Spring 2013 Midterm Name: Student ID: 1: 2: 3: 4: 5: 6: Total: 1. (20 points) Suppose that we implement a simplified version of the dynamic array data structure in which the amount of memory allocated

More information

CS112 Lecture: Repetition Statements

CS112 Lecture: Repetition Statements CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general

More information

Discussion 2C Notes (Week 9, March 4) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 9, March 4) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 9, March 4) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Heaps A heap is a tree with special properties. In this class we will only consider

More information

Intro to Algorithms. Professor Kevin Gold

Intro to Algorithms. Professor Kevin Gold Intro to Algorithms Professor Kevin Gold What is an Algorithm? An algorithm is a procedure for producing outputs from inputs. A chocolate chip cookie recipe technically qualifies. An algorithm taught in

More information

15. Recursion and Search Algorithms

15. Recursion and Search Algorithms 15. Recursion and Search Algorithms Recursion is a powerful mathematical and programming technique for certain kinds of problems. In programming, it manifests itself as a function that calls itself! Recursion

More information

CS 104 (Spring 2014) Final Exam 05/09/2014

CS 104 (Spring 2014) Final Exam 05/09/2014 CS 104 (Spring 2014) Final Exam 05/09/2014 G o o d L u c k Your Name, USC username, and Student ID: This exam has 8 pages and 8 questions. If yours does not, please contact us immediately. Please read

More information

1 Dynamic Memory continued: Memory Leaks

1 Dynamic Memory continued: Memory Leaks CS104: Data Structures and Object-Oriented Design (Fall 2013) September 3, 2013: Dynamic Memory, continued; A Refresher on Recursion Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we continue

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

CmpSci 187: Programming with Data Structures Spring 2015

CmpSci 187: Programming with Data Structures Spring 2015 CmpSci 187: Programming with Data Structures Spring 2015 Lecture #13, Concurrency, Interference, and Synchronization John Ridgway March 12, 2015 Concurrency and Threads Computers are capable of doing more

More information

CS61BL Summer 2013 Midterm 2

CS61BL Summer 2013 Midterm 2 CS61BL Summer 2013 Midterm 2 Sample Solutions + Common Mistakes Question 0: Each of the following cost you.5 on this problem: you earned some credit on a problem and did not put your five digit on the

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Computer Science E-22 Practice Final Exam

Computer Science E-22 Practice Final Exam name Computer Science E-22 This exam consists of three parts. Part I has 10 multiple-choice questions that you must complete. Part II consists of 4 multi-part problems, of which you must complete 3, and

More information

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Linux Lab Swansea, December 13, 2011.

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Linux Lab Swansea, December 13, 2011. Computer Science Department Swansea University Linux Lab Swansea, December 13, 2011 How to use the revision lecture The purpose of this lecture (and the slides) is to emphasise the main topics of this

More information

Programming in C/C Lecture 2

Programming in C/C Lecture 2 Programming in C/C++ 2005-2006 Lecture 2 http://few.vu.nl/~nsilvis/c++/2006 Natalia Silvis-Cividjian e-mail: nsilvis@few.vu.nl vrije Universiteit amsterdam News Check announcements on the C/C++ website

More information

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors Arrays Returning arrays Pointers Dynamic arrays Smart pointers Vectors To declare an array specify the type, its name, and its size in []s int arr1[10]; //or int arr2[] = {1,2,3,4,5,6,7,8}; arr2 has 8

More information

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting RMIT School of Computer Science and Information Technology Programming 2 Topic 8: Linked Lists, Basic Searching and Sorting Lecture Slides COPYRIGHT 2008 RMIT University. Original content by: Peter Tilmanis,

More information

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit. Com S 227 Spring 2018 Miniassignment 1 40 points Due Date: Thursday, March 8, 11:59 pm (midnight) Late deadline (25% penalty): Friday, March 9, 11:59 pm General information This assignment is to be done

More information

Linked Lists. References and objects

Linked Lists. References and objects Linked Lists slides created by Marty Stepp http://www.cs.washington.edu/143/ Modified by Sarah Heckman Reading: RS Chapter 16 References and objects In Java, objects and arrays use reference semantics.

More information

Lecture Notes on Interfaces

Lecture Notes on Interfaces Lecture Notes on Interfaces 15-122: Principles of Imperative Computation Frank Pfenning Lecture 14 October 16, 2012 1 Introduction The notion of an interface to an implementation of an abstract data type

More information

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 Prof. John Park Based on slides from previous iterations of this course Today s Topics Overview Uses and motivations of hash tables Major concerns with hash

More information

CMPT 280 Intermediate Data Structures and Algorithms

CMPT 280 Intermediate Data Structures and Algorithms The University of Saskatchewan Saskatoon, Canada Department of Computer Science CMPT 280 Intermediate Data Structures and Algorithms Assignment 6 - SOLUTIONS 1 Solutions Written Questions 1. (10 points)

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the

More information

Part III Appendices 165

Part III Appendices 165 Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.

More information

CS61BL: Data Structures & Programming Methodology Summer 2014

CS61BL: Data Structures & Programming Methodology Summer 2014 CS61BL: Data Structures & Programming Methodology Summer 2014 Instructor: Edwin Liao Final Exam August 13, 2014 Name: Student ID Number: Section Time: TA: Course Login: cs61bl-?? Person on Left: Possibly

More information

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer Prelim 2 CS 2110, November 20, 2014, 7:30 PM 1 2 3 4 5 Extra Total Question True/False Short Answer Complexity Induction Trees Graphs Extra Credit Max 20 10 15 25 30 5 100 Score Grader The exam is closed

More information

Week - 01 Lecture - 04 Downloading and installing Python

Week - 01 Lecture - 04 Downloading and installing Python Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 04 Downloading and

More information

CSC 261/461 Database Systems Lecture 17. Fall 2017

CSC 261/461 Database Systems Lecture 17. Fall 2017 CSC 261/461 Database Systems Lecture 17 Fall 2017 Announcement Quiz 6 Due: Tonight at 11:59 pm Project 1 Milepost 3 Due: Nov 10 Project 2 Part 2 (Optional) Due: Nov 15 The IO Model & External Sorting Today

More information

COP 3502 Spring 2018 Study Union Review

COP 3502 Spring 2018 Study Union Review Page 1 COP 3502 Spring 2018 Study Union Review Alec May Table of Contents: Tries 2 Heaps 3 Hash Tables 5 Base Conversion 6 Bitwise Operators 7 Backtracking 8 Sorts 9 Linked Lists 10 Tips and Resources

More information

Student Performance Q&A:

Student Performance Q&A: Student Performance Q&A: 2004 AP Computer Science A Free-Response Questions The following comments on the 2004 free-response questions for AP Computer Science A were written by the Chief Reader, Chris

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis Computer Science 210 Data Structures Siena College Fall 2017 Topic Notes: Complexity and Asymptotic Analysis Consider the abstract data type, the Vector or ArrayList. This structure affords us the opportunity

More information

Symbol Tables Symbol Table: In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is

More information

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016 COMP 250 Winter 2016 5 generic types, doubly linked lists Jan. 28, 2016 Java generics In our discussion of linked lists, we concentrated on how to add or remove a node from the front or back of a list.

More information

CSE 331 Midterm Exam 2/13/12

CSE 331 Midterm Exam 2/13/12 Name There are 10 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed

More information

CPS 110 Final Exam. Spring 2011

CPS 110 Final Exam. Spring 2011 CPS 110 Final Exam Spring 2011 Please answer all questions for a total of 300 points. Keep it clear and concise: answers are graded on content, not style. I expect that you can answer each question within

More information

SELECTION. (Chapter 2)

SELECTION. (Chapter 2) SELECTION (Chapter 2) Selection Very often you will want your programs to make choices among different groups of instructions For example, a program processing requests for airline tickets could have the

More information

CS5412 CLOUD COMPUTING: PRELIM EXAM Open book, open notes. 90 minutes plus 45 minutes grace period, hence 2h 15m maximum working time.

CS5412 CLOUD COMPUTING: PRELIM EXAM Open book, open notes. 90 minutes plus 45 minutes grace period, hence 2h 15m maximum working time. CS5412 CLOUD COMPUTING: PRELIM EXAM Open book, open notes. 90 minutes plus 45 minutes grace period, hence 2h 15m maximum working time. SOLUTION SET In class we often used smart highway (SH) systems as

More information

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: This exam is CLOSED book and notes. Instructions The exam consists of six questions on 11 pages. Please answer all questions on

More information

Lecture 13 Notes Sets

Lecture 13 Notes Sets Lecture 13 Notes Sets 15-122: Principles of Imperative Computation (Fall 2015) Frank Pfenning, Rob Simmons 1 Introduction In this lecture, we will discuss the data structure of hash tables further and

More information

Lecture Notes on Memory Layout

Lecture Notes on Memory Layout Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,

More information

Ch. 11: References & the Copy-Constructor. - continued -

Ch. 11: References & the Copy-Constructor. - continued - Ch. 11: References & the Copy-Constructor - continued - const references When a reference is made const, it means that the object it refers cannot be changed through that reference - it may be changed

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE Chapter 1 : What is an application of linear linked list data structures? - Quora A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements

More information

During the first 2 weeks of class, all students in the course will take an in-lab programming exam. This is the Exam in Programming Proficiency.

During the first 2 weeks of class, all students in the course will take an in-lab programming exam. This is the Exam in Programming Proficiency. Description of CPSC 301: This is a 2-unit credit/no credit course. It is a course taught entirely in lab, and has two required 2-hour 50-minute lab sessions per week. It will review, reinforce, and expand

More information

Section 05: Solutions

Section 05: Solutions Section 05: Solutions 1. Memory and B-Tree (a) Based on your understanding of how computers access and store memory, why might it be faster to access all the elements of an array-based queue than to access

More information

Linked lists Tutorial 5b

Linked lists Tutorial 5b Linked lists Tutorial 5b Katja Mankinen 6 October 2017 Aim Pointers are one of the most powerful tools in C++: with pointers, you can directly manipulate computer memory. However, at first glance they

More information

Java Collections Framework

Java Collections Framework Java Collections Framework Introduction In this article from my free Java 8 course, you will be given a high-level introduction of the Java Collections Framework (JCF). The term Collection has several

More information

FILE ORGANIZATION. GETTING STARTED PAGE 02 Prerequisites What You Will Learn

FILE ORGANIZATION. GETTING STARTED PAGE 02 Prerequisites What You Will Learn FILE ORGANIZATION GETTING STARTED PAGE 02 Prerequisites What You Will Learn PRINCIPLES OF FILE ORGANIZATION PAGE 03 Organization Trees Creating Categories FILES AND FOLDERS PAGE 05 Creating Folders Saving

More information

Lecture 11 Unbounded Arrays

Lecture 11 Unbounded Arrays Lecture 11 Unbounded Arrays 15-122: Principles of Imperative Computation (Spring 2018) Rob Simmons, Frank Pfenning Arrays have efficient O(1) access to elements given an index, but their size is set at

More information

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

Question Bank Subject: Advanced Data Structures Class: SE Computer

Question Bank Subject: Advanced Data Structures Class: SE Computer Question Bank Subject: Advanced Data Structures Class: SE Computer Question1: Write a non recursive pseudo code for post order traversal of binary tree Answer: Pseudo Code: 1. Push root into Stack_One.

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

More information

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1 Review from Lecture 21 CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1 the single most important data structure known to mankind Hash Tables, Hash Functions,

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

CS222P Fall 2017, Final Exam

CS222P Fall 2017, Final Exam STUDENT NAME: STUDENT ID: CS222P Fall 2017, Final Exam Principles of Data Management Department of Computer Science, UC Irvine Prof. Chen Li (Max. Points: 100 + 15) Instructions: This exam has seven (7)

More information

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Assignment 1: grid. Due November 20, 11:59 PM Introduction CS106L Fall 2008 Handout #19 November 5, 2008 Assignment 1: grid Due November 20, 11:59 PM Introduction The STL container classes encompass a wide selection of associative and sequence containers. However,

More information

HASH TABLES. Hash Tables Page 1

HASH TABLES. Hash Tables Page 1 HASH TABLES TABLE OF CONTENTS 1. Introduction to Hashing 2. Java Implementation of Linear Probing 3. Maurer s Quadratic Probing 4. Double Hashing 5. Separate Chaining 6. Hash Functions 7. Alphanumeric

More information

CS 167 Final Exam Solutions

CS 167 Final Exam Solutions CS 167 Final Exam Solutions Spring 2018 Do all questions. 1. [20%] This question concerns a system employing a single (single-core) processor running a Unix-like operating system, in which interrupts are

More information

Lecture Notes on Queues

Lecture Notes on Queues Lecture Notes on Queues 15-122: Principles of Imperative Computation Frank Pfenning and Jamie Morgenstern Lecture 9 February 14, 2012 1 Introduction In this lecture we introduce queues as a data structure

More information

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Up to here Not included in program Java collections framework prebuilt data structures interfaces and methods for manipulating

More information

Computer Science 210 Data Structures Siena College Fall 2018

Computer Science 210 Data Structures Siena College Fall 2018 Computer Science 210 Data Structures Siena College Fall 2018 Topic Notes: The ArrayList Arrays are a very common method to store a collection of similar items. Arrays work very well for a lot of situations,

More information

Recitation 10: Malloc Lab

Recitation 10: Malloc Lab Recitation 10: Malloc Lab Instructors Nov. 5, 2018 1 Administrivia Malloc checkpoint due Thursday, Nov. 8! wooooooooooo Malloc final due the week after, Nov. 15! wooooooooooo Malloc Bootcamp Sunday, Nov.

More information

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion Announcements: Test 1 Information Test 1 will be held Monday, Sept 25th, 2017 from 6-7:50pm Students will be randomly assigned

More information

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019 CS 31: Introduction to Computer Systems 22-23: Threads & Synchronization April 16-18, 2019 Making Programs Run Faster We all like how fast computers are In the old days (1980 s - 2005): Algorithm too slow?

More information

Soda Machine Laboratory

Soda Machine Laboratory Soda Machine Laboratory Introduction This laboratory is intended to give you experience working with multiple queue structures in a familiar real-world setting. The given application models a soda machine

More information

HOUR 18 Collaborating on Documents

HOUR 18 Collaborating on Documents HOUR 18 Collaborating on Documents In today s office environments, people are increasingly abandoning red ink pens, highlighters, and post-it slips in favor of software tools that enable them to collaborate

More information

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am Name Student ID # Section TA Name The exam is closed book, closed notes, closed devices, except that you may have a 5x8 card with handwritten notes

More information

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5 Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5 [talking head] This lecture we study theory design and implementation. Programmers have two roles to play here. In one role, they

More information

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount

More information

Using Images in FF&EZ within a Citrix Environment

Using Images in FF&EZ within a Citrix Environment 1 Using Images in FF&EZ within a Citrix Environment This document explains how to add images to specifications, and covers the situation where the FF&E database is on a remote server instead of your local

More information

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02) Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 04 Lecture - 01 Merge Sort (Refer

More information

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

More information

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

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print) Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 SOLUTIONS Your name (Please print) 1. Suppose you are given a singly-linked

More information

CSE 331 Midterm Exam Sample Solution 2/13/12

CSE 331 Midterm Exam Sample Solution 2/13/12 Question 1. (14 points) (assertions) Using backwards reasoning, find the weakest precondition for each sequence of statements and postcondition below. Insert appropriate assertions in each blank line.

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference

More information

CS 151 Name Final Exam December 17, 2014

CS 151 Name Final Exam December 17, 2014 Note that there are 10 equally-weighted qeustions. CS 151 Name Final Exam December 17, 2014 1. [20 points] Here is a list of data: 4 2 18 1 3 7 9 0 5. For each of the following structures I will walk through

More information

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. What are 3 differences between C and C++ const variables? QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

More information

11. Abstract Classes and Interfaces

11. Abstract Classes and Interfaces 11. Abstract Classes and Interfaces THIS SECTION IS OPTIONAL FOR CMPT 101, and you will find it was written for a different course. It refers to previous exercises that are not part of Cmpt 101. Sometimes

More information

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion Review from Lectures 5 & 6 Arrays and pointers, Pointer arithmetic and dereferencing, Types of memory ( automatic, static,

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms Computer Science 4U Unit 1 Programming Concepts and Skills Algorithms Algorithm In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation,

More information