Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the...

48
Review of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract data type big O and code analysis lists stacks queues MCS 360 Lecture 39 Introduction to Data Structures Jan Verschelde, 27 April 2020 Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 1 / 48

Transcript of Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the...

Page 1: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

Review of the First 18 Lectures

1 The Final ExamFriday 8 May 2020

2 Examples of Questionsabstract data typebig O and code analysislistsstacksqueues

MCS 360 Lecture 39Introduction to Data StructuresJan Verschelde, 27 April 2020

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 1 / 48

Page 2: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

Review of the First 18 Lectures

1 The Final ExamFriday 8 May 2020

2 Examples of Questionsabstract data typebig O and code analysislistsstacksqueues

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 2 / 48

Page 3: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

The Final Exam

Friday 8 May 2020, 10:30AM online.

The exam consists in two parts:1 the first part is due at 12:30PM, with focus on concepts;2 the second part is due at 9PM, and involves C++ programming.

The first part must be solved individually,for the second part you may work in a pair.

If you work in a pair for the second part, then you must decide andagree before the start of the exam, before 9:30AM on Friday.

If you take the final exam, then email me before 9:30AM on Friday,so you will then receive the questions in an email reply.

To pass the course, you must take the exam.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 3 / 48

Page 4: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

this review

This review has the focus on the first 18 lectures,the material before the first midterm exam:

1 Lectures 1 to 18, the first six chapters in the textbook.

2 ADT, vectors and lists, stacks and queues.

3 Focus on data structure concepts,not so much on C++ programming.

Questions on this review are representative,but the list is by no means exhaustive.Review the quizzes, homework problems, and midterm exams.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 4 / 48

Page 5: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

Review of the First 18 Lectures

1 The Final ExamFriday 8 May 2020

2 Examples of Questionsabstract data typebig O and code analysislistsstacksqueues

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 5 / 48

Page 6: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

write an ADT

1 Write an ADT specification of time, represented byhours, minutes, and seconds.Time is kept in a 24-hour format, e.g.: 20:00:00 is 8PM.

1 Give a definition of the type time.2 Define operations to see whether any two given times are equal

and whether one time is later than another given time.3 Define the addition and subtraction of times.4 Subtraction of two times is only allowed if the first time is later than

or equal to the second one. List all relevant conditions.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 6 / 48

Page 7: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

ADT for time

abstract Time;/* Time is a tuple of 3 nonnegative integer

numbers: (hours, minutes, seconds);The hours are between 0 and 23.The minutes are between 0 and 59.The seconds are between 0 and 59. */

The equality of two times:

abstract bool is_equal ( Time t1, Time t2 );postcondition: is_equal(t1, t2) is true ift1.hours == t2.hours, t1.minutes == t2.minutes,and t1.second == t2.seconds;otherwise, is_equal(t1, t2) is false.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 7 / 48

Page 8: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

ADT for time continuedabstract bool is_later ( Time t1, Time t2 );postcondition: is_later(t1, t2) is true ift1.hours > t2.hours, ort1.hours == t2.hours, and t1.minutes > t2.minutes, ort1.hours == t2.hours, t1.minutes == t2.minutes, andt1.seconds > t2.seconds;otherwise, is_later(t1, t2) is false.

abstract Time operator+ ( Time t1, Time t2 );postcondition: after t3 = t1 + t2 we havet3.seconds = (t1.seconds + t2.seconds ) modulo 60,t3.minutes = (t1.seconds + t2.seconds )/60

+ (t1.minutes + t2.minutes ) modulo 60,t3.hours = (t1.minutes + t2.minutes )/60

+ (t1.hours + t2.hours ) modulo 24.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 8 / 48

Page 9: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

ADT for time continued

To define the subtraction of two times,we convert the times to seconds, subtract,and convert the difference to hours, minutes, seconds.

abstract Time operator- ( Time t1, Time t2 );precondition: is_later(t1, t2).postcondition: after t3 = t1 - t2 we havet3.seconds = (a1 - a2) modulo 60,t3.minutes = ((a1 - a2 - t3.seconds)/60) modulo 60t3.hours = (a1 - a2 - t3.seconds - t3.minutes)/3600wherea1 = (t1.hours*60 + t1.minutes)*60 + t1.seconds,a2 = (t2.hours*60 + t2.minutes)*60 + t2.seconds.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 9 / 48

Page 10: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

Review of the First 18 Lectures

1 The Final ExamFriday 8 May 2020

2 Examples of Questionsabstract data typebig O and code analysislistsstacksqueues

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 10 / 48

Page 11: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

the big O notation

2 Show that O(log10(n)) is O(log2(n)).

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 11 / 48

Page 12: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

O(·)

2 Show that O(log10(n)) is O(log2(n)).I A function f (n) is O(log2(n)) if there are constants c and N,

independent of n, such that, for all n ≥ N: f (n) ≤ c · log2(n).

I Let m be the constant: 10 = 2log10(m),then log2(10) = log10(m), m = 10log2(10), and log10(m) = log2(10).

Consider:

n = 10k = (2log10(m))k = 2k log10(m) ⇒ log2(n) = k log10(m)

⇒ log2(n) = k log2(10).

I Therefore, log2(n) = log10(n) log2(10), or log10(n) =1

log2(10) log2(n).

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 12 / 48

Page 13: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

analysis of code

3 Consider the code below:double x[n+1];double v,y;for(int i=0; i<n+1; i++)

for(y=1.0, int j=0; j<n+1; j++)if(j != i)

y = y*(v - x[j])/(x[i] - x[j]);

1 What are the preconditions on the numbers in the array x?Use sufficient assert statements to enforce the preconditions.

2 Give the loop invariant for the inner loop controlled by j.Describe using the proper mathematical terminology what the valuefor y represents.

3 Count the number of arithmetical operations executed by the codefor any value of n.

4 Use the big O(·) notation to bound the magnitude of the cost of thecode in function of n.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 13 / 48

Page 14: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

enforcing preconditions with assert

Observe the statement

y = y*(v - x[j])/(x[i] - x[j]);

may divide by zero if x[i] - x[y] == 0.0.

double x[n+1];double v,y;for(int i=0; i<n+1; i++)

for(y=1.0, int j=0; j<n+1; j++)if(j != i){

assert(x[i] - x[j] != 0.0)y = y*(v - x[j])/(x[i] - x[j]);

}

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 14 / 48

Page 15: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

looking at the inner loop

for(y=1.0, int j=0; j<n+1; j++)if(j != i)

y = y*(v - x[j])/(x[i] - x[j]);

computes a product, in mathematical notation:

y =n∏

j = 0j 6= i

v − x [j]x [i]− x [j]

.

At step k > 0: y =k−1∏

j = 0j 6= i

v − x [j]x [i]− x [j]

∗ v − x [k ]x [i]− x [k ]

.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 15 / 48

Page 16: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

counting operations

for(int i=0; i<n+1; i++)for(y=1.0, int j=0; j<n+1; j++)

if(j != i) y = y*(v - x[j])/(x[i] - x[j]);

Examining the loop counters:In the outer loop, i runs from 0 to n.In the inner loop, j runs from 0 to n, but excludes i.

The innermost instruction is executed (n + 1)n times.

The instruction y = y*(v - x[j])/(x[i] - x[j]);involves 2 subtractions, 1 division, and 1 multiplication,adding up to 4 arithmetical operations.The arithmetical operation count is thus 4(n + 1)n = O(n2).

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 16 / 48

Page 17: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

Review of the First 18 Lectures

1 The Final ExamFriday 8 May 2020

2 Examples of Questionsabstract data typebig O and code analysislistsstacksqueues

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 17 / 48

Page 18: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

from single to double linked list

4 A node in a single linked list with item type T as template isstruct Node{

T data; // T is template parameterNode *next; // pointer to next node

Node(const T& item, Node* ptr = NULL) :data(item), next(ptr) {}

};

1 Draw an example of at least three elements of a single linked list.Use this example of the input of the algorithm, to illustrate the steps.Draw the evolution of the construction of the output list, stepwise.

2 Describe an algorithm to convert a single linked list to a doublelinked list.

3 Write C++ code to define the conversion.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 18 / 48

Page 19: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

from single to double linked list

4 A node in a single linked list with item type T as template isstruct Node{

T data; // T is template parameterNode *next; // pointer to next node

Node(const T& item, Node* ptr = NULL) :data(item), next(ptr) {}

};

1 Draw an example of at least three elements of a single linked list.Use this example of the input of the algorithm, to illustrate the steps.Draw the evolution of the construction of the output list, stepwise.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 19 / 48

Page 20: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

input and output

We take a single linked list S of three integers as input:

S - Node

next: *data: 3

- Node

next: *data: 5

- Node

next: *data: 4

- NULL

Then the double linked output list D is

D - Node

next: *prev: *data: 3

?NULL

- Node

next: *prev: *data: 5

� - Node

next: *prev: *data: 4

� - NULL

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 20 / 48

Page 21: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

step 1

S - Node

next: *data: 3

-

?

Sptr

Node

next: *data: 5

- Node

next: *data: 4

- NULL

D - Node

next: *prev: *data: 3

?NULL

-

?

Dptr

NULL

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 21 / 48

Page 22: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

step 2

S - Node

next: *data: 3

- Node

next: *data: 5

-

?

Sptr

Node

next: *data: 4

- NULL

D - Node

next: *prev: *data: 3

?NULL

- Node

next: *prev: *data: 5

� -

?

Dptr

NULL

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 22 / 48

Page 23: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

step 3

S - Node

next: *data: 3

- Node

next: *data: 5

- Node

next: *data: 4

- NULL

?

Sptr

D - Node

next: *prev: *data: 3

?NULL

- Node

next: *prev: *data: 5

� - Node

next: *prev: *data: 4

� - NULL

?

Dptr

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 23 / 48

Page 24: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

from single to double linked list

4 A node in a single linked list with item type T as template isstruct Node{

T data; // T is template parameterNode *next; // pointer to next node

Node(const T& item, Node* ptr = NULL) :data(item), next(ptr) {}

};

2 Describe an algorithm to convert a single linked list to a doublelinked list.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 24 / 48

Page 25: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

description of an algorithm

We use the notation S for input, D for output.1 If S == NULL, then D = NULL.2 Sptr = S; D = NULL;3 while Sptr != NULL do

1 Make a new doubly linked node N.2 Copy the data of Sptr to N.3 if D == NULL then

1 D = N;2 Dptr = N;

4 else1 Set the previous pointer of N to Dptr.2 Set the next pointer of Dptr to N.3 Advance Dptr to N4 Advance Sptr to the next of Sptr.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 25 / 48

Page 26: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

from single to double linked list

4 A node in a single linked list with item type T as template isstruct Node{

T data; // T is template parameterNode *next; // pointer to next node

Node(const T& item, Node* ptr = NULL) :data(item), next(ptr) {}

};

3 Write C++ code to define the conversion.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 26 / 48

Page 27: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

type of a node in a double linked list

struct DoubleNode{

T data; // T is template parameterDoubleNode *next; // pointer to next nodeDoubleNode *prev; // pointer to previous node

DoubleNode(const T& item,DoubleNode* p = NULL,DoubleNode* q = NULL) :data(item), next(p), prev(q) {}

};

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 27 / 48

Page 28: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

a C++ functionvoid convert ( Node& S, DoubleNode& D ){

if(S == NULL)D = NULL;

else{

Node *Sptr = S;DoubleNode *Dptr = NULL;

while(Sptr != NULL){

DoubleNode *N = new DoubleNode(Sptr->data);if(D == NULL){

D = N; Dptr = N;}else{

N->prev = Dptr; Dptr->next = N; Dptr = N;}Sptr = Sptr->next;

}}

}

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 28 / 48

Page 29: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

from STL vector to STL list

5 Given an STL vector of doubles, write the code to construct anSTL list with the same content as the given vector.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 29 / 48

Page 30: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

from STL vector to STL list

5 Given an STL vector of doubles, write the code to construct anSTL list with the same content as the given vector.

list<double> convert ( vector<double> v ){

list<double> result;

for(int k=0; k<v.size(); k++)result.push_back(v[i]);

return result;}

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 30 / 48

Page 31: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

looping through an STL list

6 Define a function that takes on input a nonempty STL list ofintegers and that returns the largest element of that list.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 31 / 48

Page 32: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

looping through an STL list

6 Define a function that takes on input a nonempty STL list ofintegers and that returns the largest element of that list.

int max ( list<int> lst ){

list<int>::iterator i = lst.begin();int result;

for(result = *i++; i != lst.end(); i++)if(*i > result) result = *i;

return result;}

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 32 / 48

Page 33: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

Review of the First 18 Lectures

1 The Final ExamFriday 8 May 2020

2 Examples of Questionsabstract data typebig O and code analysislistsstacksqueues

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 33 / 48

Page 34: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

evaluation of a postfix expression

7 Consider the following postfix expression:7 3 4 + * 8 2 5 * - +.

1 Draw the tree which represents this expression.2 Simulate the evaluation of the postfix expression using a stack.

Draw all intermediate states of the stack.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 34 / 48

Page 35: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

constructing the tree of a postfix expression

7 Consider the following postfix expression:7 3 4 + * 8 2 5 * - +.

1 Draw the tree which represents this expression.

+����XXXX

*��

7

HH+

�3

@4

-��

8

HH*

�2

@5

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 35 / 48

Page 36: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

evaluation of a postfix expression

7 Consider the following postfix expression:7 3 4 + * 8 2 5 * - +.

2 Simulate the evaluation of the postfix expression using a stack.Draw all intermediate states of the stack.

- 437 + * 8 2 5 * - +

- 77 * 8 2 5 * - +

- 49 8 2 5 * - +- 8

49 2 5 * - +

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 36 / 48

Page 37: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

evaluation of a postfix expression continued

7 Consider the following postfix expression:7 3 4 + * 8 2 5 * - +.

2 Simulate the evaluation of the postfix expression using a stack.Draw all intermediate states of the stack.

- 2849 5 * - +

- 52849 * - +

- 10849 - +

- -249 + - 47

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 37 / 48

Page 38: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

converting to a postfix expression

8 Use a stack to convert 7*(3 + 4) + 8 - 2*5to a postfix expression.Draw all intermediate states of the data structures used.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 38 / 48

Page 39: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

converting to a postfix expression

8 Convert 7*(3 + 4) + (18 - 2*5)/2 to a postfix expression.

-7*(3+4)+(18-2*5)/2 -* 7 3+4)+(18-2*5)/2

-+* 7 3 4)+(18-2*5)/2 -* 7 3 4 + +(18-2*5)/2

-7 3 4 + * +(18-2*5)/2 -+ 7 3 4 + * 18-2*5)/2

--+ 7 3 4 + * 18 2*5)/2

-*-+ 7 3 4 + * 18 2 5)/2

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 39 / 48

Page 40: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

converting to a postfix expression continued

8 Convert 7*(3 + 4) + (18 - 2*5)/2 to a postfix expression.

--+ 7 3 4 + * 18 2 5 *)/2

-+ 7 3 4 + * 18 2 5 * - /2

-/+ 7 3 4 + * 18 2 5 * - 2

-+ 7 3 4 + * 18 2 5 * - 2 /

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 40 / 48

Page 41: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

converting to a postfix expression, the result

8 Convert 7*(3 + 4) + (18 - 2*5)/2 to a postfix expression.

-7 3 4 + * 18 2 5 * - 2 / +

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 41 / 48

Page 42: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

stack as STL vector

9 Describe an implementation of a stack using an STL vector.Would you pop from the front or from the back?Justify your answer.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 42 / 48

Page 43: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

stack as STL vector9 Describe an implementation of a stack using an STL vector.

Would you pop from the front or from the back?Justify your answer.

Consider for example:V -- ’S’

0

’E’

1

’O’

2

Viewed as a stack, the top of the stack is the index 2,the last element in the vector V. Justification:

1 The push and pop are constant operations:1 push: increase last index, fill up next free spot;2 pop: decrease last index.

2 In general, adding one element to the end of a vector is efficientas long as there enough continuous space allocated.

3 Inserting an element to the first spot requires shifting all elements,which takes O(n) for a vector of size n.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 43 / 48

Page 44: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

Review of the First 18 Lectures

1 The Final ExamFriday 8 May 2020

2 Examples of Questionsabstract data typebig O and code analysislistsstacksqueues

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 44 / 48

Page 45: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

priority queues

10 Assume we want to implement a priority queue with a singlelinked list. For every item in the queue we store a pair of doubles:the arrival time and the size of the job.

1 Write a complete definition of the priority queue.2 Describe an algorithm to insert a job in any given queue. Items in

the queue are sorted in increasing order on arrival time. Do notmake assumptions on the arrival time of the item to insert. Thepriority queue may be empty.Illustrate your algorithm by drawing a general case inserting an iteminto a queue of at least five elements.

3 Write C++ code to implement the algorithm described above.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 45 / 48

Page 46: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

inserting into a list of sorted numbers

Sketch of an answer:

1 We represent the priority queue as a single linked list.

The data of each node is a pair of a double (arrival time),and an int (size of the job).The nodes are sorted on the arrival time.

2 The algorithm to insert a new job into the list uses two chasingpointers, to the previous and the current node.

If a double linked list is used in the representation,then only one pointer to the current node is needed.

3 Review the code for the insert into a sorted list of numbers.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 46 / 48

Page 47: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

use STL for a priority queue of jobs

11 Describe a solution to the previous exercise using the STL.Which data structure of the STL would you choose?Justify your choice.

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 47 / 48

Page 48: Review of the First 18 Lectureshomepages.math.uic.edu/~jan/mcs360/review_three_ans.pdfReview of the First 18 Lectures 1 The Final Exam Friday 8 May 2020 2 Examples of Questions abstract

the STL priority_queue#include<iostream>#include<cstdlib>#include<queue>

using namespace std;

int main(){

priority_queue< pair<int,int> > q;

cout << "a queue of n random jobs ..." << endl;cout << "give n : "; int n; cin >> n;

srand(time(NULL));for(int i=0; i<n; i++){

pair<int,int> job;job.first = rand() % 5;job.second = rand() % 100;cout << "pushing (" << job.first

<< "," << job.second << ")" << endl;q.push(job);

}

Introduction to Data Structures (MCS 360) answers to Review Three L-39 27 April 2020 48 / 48