DNHI Homework 3 Solutions List, Stacs and Queues

Similar documents
PA3 Design Specification

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS171 Midterm Exam. October 29, Name:

Queues Fall 2018 Margaret Reid-Miller

Lecture No.04. Data Structures

Insertion Sort: an algorithm for sorting an array

ITI Introduction to Computing II

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

8. Fundamental Data Structures

infix expressions (review)

Csci 102: Sample Exam

Largest Online Community of VU Students

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

Summer Final Exam Review Session August 5, 2009

CIS Fall Data Structures Midterm exam 10/16/2012

Linked Structures. See Section 3.2 of the text.

[2:3] Linked Lists, Stacks, Queues

COP 3502 Section 2 Exam #2 Version A Spring /23/2017

CS24 Week 4 Lecture 2

Top of the Stack. Stack ADT

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CS 206 Introduction to Computer Science II

1 P age DS & OOPS / UNIT II

CS2012 Programming Techniques II

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS350: Data Structures Linked Lists

Data Structures and Algorithms Notes

Laboratorio di Algoritmi e Strutture Dati

CE204 Data Structures and Algorithms Part 2

Introduction to Computing II (ITI 1121) Final Examination

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

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

CS 216 Exam 1 Fall SOLUTION

Largest Online Community of VU Students

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

182 review 1. Course Goals

CMPT 225. Lecture 6 linked list

Basic Data Structures

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

THE UNIVERSITY OF WESTERN AUSTRALIA

Top of the Stack. Stack ADT

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Final Exam Data Structure course. No. of Branches (5)

Linked List Implementation of Queues

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

University of KwaZulu-Natal Pietermaritzburg Campus Examinations

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

CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017

Introduction to Computer Science II (ITI 1121) Final Examination

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Basic Data Structures 1 / 24

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

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

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

csci 210: Data Structures Stacks and Queues

Abstract Data Types 1

Fun facts about recursion

CMSC Introduction to Algorithms Spring 2012 Lecture 7

16. Dynamic Data Structures

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Programming. Lists, Stacks, Queues

Data Structure (CS301)

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Introduction to Computer Science II (CSI 1101) Final Examination

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

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

In addition to the correct answer, you MUST show all your work in order to receive full credit.

for dequeue will be O(N). (Or words to that effect. Needed to mention both or -1.)

CS61B Spring 2016 Guerrilla Section 2 Worksheet

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

CSE 230 Intermediate Programming in C and C++

DATA STRUCTURE UNIT I

Abstract Data Types 1

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

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

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

CMSC 132: Object-Oriented Programming II. Stack and Queue

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

CS350 - Exam 1 (100 Points)

DS L9: Queues

Stacks Fall 2018 Margaret Reid-Miller

Solutions to a few of the review problems:

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

12 Abstract Data Types

DEEPIKA KAMBOJ UNIT 2. What is Stack?

NATIONAL UNIVERSITY OF SINGAPORE

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

INSTITUTE OF AERONAUTICAL ENGINEERING

CSCI-140 Midterm Review October 10, 2015 Presented by the RIT Computer Science Community

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Programming II (CS300)

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Examination Questions Midterm 1

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

COMP 250 Midterm #2 March 11 th 2013

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Transcription:

Solutions List, Stacs and Queues Problem 1 Given the IntegerQueue ADT below state the return value and show the content of the, initially empty, queue of Integer objects after each of the following operations. If any of the operations below is invalid or would cause program to crash, state it and explain what is wrong with it (then proceed with the next step ignoring the invalid line). Assume that queue is a reference of type IntegerQueue. Informal Specification operations: The IntegerQueue contains a (possibly empty) collection of objects of type Integer. The queue supports the following insert/enqueue This operation adds a Integer object, given as a parameter, to the end of the queue of integers. remove/dequeue This operation removes and returns a Integer object from the front of the queue of integers. If the queue is empty, null should be returned. tostring This operation produces a String that contains all Integer objects stored in the queue from font to back separated by a single comma and a single space. If the queue is empty, an empty string should be returned. queue.insert( 15 ); queue.insert( -15 ); queue.insert( 35 ); queue.insert( 13 ); A Assume that the implementation is array based and follows the efficiency ideas that we discussed in class. Assume that the initial capacity of the array to store the queue is equal to 4 and that its size is doubled whenever the array runs out of room. queue.insert( 15 ); [ 15,,, ] return value: none [ 15, 3,, ] return value: none queue.insert( -15 ); [ 15, 3,-15, ] return value: none queue.insert( 35 ); [ 15, 3,-15, 35] return value: none [, 3,-15, 35] return value: 15 [,,-15, 35] return value: 3 [,,, 35] return value: -15 queue.insert( 13 ); [ 13,,, 35] return value: none 1

[ 13,,, ] return value: 35 [,,, ] return value: 13 [,,, ] return value: null (or an exception thrown) [, 3,, ] return value: none or [ 3,,, ] return value: none B Assume that the implementation is reference based and the head reference points of the front of the queue. queue.insert( 15 ); head -> [15]-> null return value: none head -> [15]-> [3]-> null return value: none queue.insert( -15 ); head -> [15]-> [3]-> [-15]-> null return value: none queue.insert( 35 ); head -> [15]-> [3]-> [-15]-> [35]-> null return value: none head -> [3]-> [-15]-> [35]-> null return value: 15 head -> [-15]-> [35]-> null return value: 3 head -> [35]-> null return value: -15 queue.insert( 13 ); head -> [35]-> [13]-> null return value: none head -> [13]-> null return value: 35 head -> null return value: 13 head -> null return value: null (or an exception thrown) head -> [ 3]-> null return value: none C Discuss efficiency/performance of each the insert and remove operations for each implementation (for the reference based implementation, consider the difference in performance if you have an additional reference to the end of the queue). Array based implementation: assumption: we know the indexes of the first and last elements in the queue insertion is O(1) as long as there is room in the array, when array needs to be resized, it is O(N) removal is always O(1) Reference based implementation: assumption: we only have a reference to the head and it points to the first element in the queue insertion is O(N) since we need to traverse the queue to get to the end remove is O(1) since we have a reference to the first element Reference based implementation: assumption: we have references to the front and back of the queue, front points to the first elemeent and back points to the last element insertion is O(1) since have a reference to the last element remove is O(1) since we have a reference to the first element 2

Problem 2 Given the CharStack ADT that we discussed in class show the content of the, initially empty, stack of Character objects after each of the following operations. If any of the operations below is invalid or would cause program to crash, state it and explain what is wrong with it. Assume that stack is a reference of type CharStack. Informal Specification operations: The CharStack contains a (possibly empty) collection of objects of type Character. The stack supports the following insert/push This operation adds a Character object, given as a parameter, to the top of the stack of characters. remove/pop This operation removes and returns a Character object from the top of the stack of characters. peek This operation returns a Character object from the top of the stack of characters. tostring This operation produces a meaningful String representation of the stack. stack.push( c ); stack.push( new Character( s ) ); stack pop(); char p = s ; stack.push( p ); stack.push( p ); stack.push( new Character( 1 ) ); stack.push( % ); stack.push( A ); stack.push( X ); A Assume that the implementation is array based and follows the efficiency ideas that we discussed in class. Assume that the initial capacity of the array to store the stack is equal to 4 and that its size is doubled whenever the array runs out of room. stack.push( c ); [ c,,, ] return value: none stack.push( new Character( s ) ); [ c, s,, ] return value: none stack pop(); [ c,,, ] return value: s char p = s ; stack.push( p ); [ c, s,, ] return value: none stack.push( p ); [ c, s, s, ] return value: none stack.push( new Character( 1 ) ); [ c, s, s, 1] return value: none [ c, s, s, 1] return value: 1 [ c, s, s, ] return value: 1 stack.push( % ); [ c, s, s, %] return value: none [ c, s, s, %] return value: % stack.push( A ); [ c, s, s, %, A,,, ] return value: none stack.push( X ); 3

[ c, s, s, %, A, X,, ] return value: none [ c, s, s, %, A,,, ] return value: X [ c, s, s, %,,,, ] return value: A B Assume that the implementation is reference based. Where should the head reference point to (bottom of the stack or top of the stack). The head reference needs to point to the top of the stack in order to provide efficient implementation. stack.push( c ); head -> [c]-> null return value: none stack.push( new Character( s ) ); head -> [s]-> [c]-> null return value: none stack pop(); head -> [c]-> null return value: s char p = s ; stack.push( p ); head -> [s]-> [c]-> null return value: none stack.push( p ); head -> [s]-> [s]-> [c]-> null return value: none stack.push( new Character( 1 ) ); head -> [1]-> [s]-> [s]-> [c]-> null return value: none head -> [1]-> [s]-> [s]-> [c]-> null return value: 1 head -> [s]-> [s]-> [c]-> null return value: 1 stack.push( % ); head -> [%]-> [s]-> [s]-> [c]-> null return value: none head -> [%]-> [s]-> [s]-> [c]-> null return value: % stack.push( A ); head -> [A]-> [%]-> [s]-> [s]-> [c]-> null return value: none stack.push( X ); head -> [X]-> [A]-> [%]-> [s]-> [s]-> [c]-> null return value: none head -> [A]-> [%]-> [s]-> [s]-> [c]-> null return value: X head -> [%]-> [s]-> [s]-> [c]-> null return value: A C Discuss efficiency/performance of each the insert/push and remove/pop operations for each implementation. Array based implementation: assumption: we know the indexes of the top element in the stack, bottom of the stack is anchored to the zeroth index insertion is O(1) as long as there is room in the array, when array needs to be resized, it is O(N) removal is always O(1) Reference based implementation: assumption: we have a reference to the head and it points to the top element in the stack insertion is O(1) since we have a reference to the top element remove is O(1) since we have a reference to the top element Problem 3 The GenericList interface and its GenericLinkedList implementation used in class (see the source code for lecture 5) provide an insertion method that always adds a new node to the back of the list. Assuming that the data item that is stored in the GenericNode object implements the Comparable interface (i.e. has standard definitions of compareto(...)), write the method 4

void orderedinsert( T item ) that adds a new node to the list in a sorted order (from smallest to largest). 1 /* * 2 * 3 * I n s e r t s a T o b j e c t k e e p i n g t h e o r d e r of t h e l i s t 4 * 5 * @ p a r a m i t e m t h e i t e m to i n s e r t 6 */ 7 public void insert(t item) { 8 // a d d n o d e o n l y if i t e m is n o t n u l l 9 // ( we do n o t w a n t to h a v e n o d e s s t o r i n g n u l l r e f e r e n c e as t h e d a t a v a l u e ) 10 if (item == null ) 11 return; 12 13 // c r e a t e n e w n o d e a n d s e t i t s n e x t r e f e r e n c e to n u l l 14 GenericNode<T> newnode = new GenericNode <T> ( item, null ); 15 16 // s p e c i a l c a s e f o r an e m p t y l i s t 17 if (head == null ) 18 head = newnode; 19 // s p e c i a l c a s e w h e n n e w n o d e b e c o m e s f i r s t n o d e 20 else if(newnode.compareto(head)<0){ 21 newnode.setnext(head); 22 head = newnode; 23 }else{ 24 // c r e a t e t h e c u r r e n t r e f e r e n c e a n d a d v a n c e it to t h e l a s t n o d e 25 GenericNode<T> current = head; 26 while (current.getnext()!= null && newnode.compareto(current.getnext())>0) 27 current = current.getnext(); 28 29 if(current.getnext()==null) 30 // m a k e t h e l a s t n o d e p o i n t to t h e n e w l a s t n o d e 31 current.setnext(newnode); 32 else{ 33 // i n s e r t t h e n e w n o d e a f t e r t h e c u r r e n t 34 newnode.setnext(current.getnext()); 35 current.setnext(newnode); 36 } 37 } 38 numofelements++; 39 } Problem 4 Answer the following true/false questions. Explain your answers. F A stack is a first in, first out structure. F A queue is a last in, first out structure. F A Java interface must have at least one defined constructor. T A class that implements an interface has to implement ALL methods listed in the interface. T In a non-empty stack, the item that has been in the stack the longest is at the bottom of the stack. F A recursive solution to a problem is always better than an iterative solution. T A general case of a valid recursive algorithm must eventually reduce to a base case. F Recursive methods should be used whenever execution speed is critical. T If you enqueue 5 elements into an empty queue, and then perform the dequeue operation 5 times, the queue will be empty again. F If you push 5 elements into an empty stack, and then perform the peek operation 5 times, the stack will be empty again. T In a singly linked list based implementation of a queue, at least one of the operations that add or remove an item has to traverse all the elements in the queue. 5

F In a singly linked list based implementation of a stack,, at least one of the operations that add or remove an item has to traverse all the elements in the stack. Problem 5 In a circular singly linked list, the next reference of the last node points back to the first node (instead of null). Write a method that counts the number of nodes in a circular singly lined list. Assume that there is a head reference that points to the first node in the list. 1 int size () 2 if (head == null ) return 0; 3 return 1 + size ( head.next ) 4 5 6 int size ( Node n ) 7 // t h i s s h o u l d n o t h a p p e n in a c i r c u l a r l i s t 8 if (n == null ) throw new IllegalStateException () ; 9 if (n == head ) return 0; 10 return 1 + size (n.next ) 11 Note: you could also use an iterative solution. Problem 5a Write a method that detects if a given linked list is a circular list or if it ends with the traditional null reference. 1 boolean iscircular () 2 if (head == null ) return false 3 return iscircular ( head.next ) 4 5 6 boolean iscircular ( Node n ) 7 if (n == null ) return false 8 if (n == head ) return true 9 return iscircular (n.next ) 10 Note: you could also use an iterative solution. Problem 6 A doubly linked list mantains a reference to the head and to the tail of the list (first and last nodes, respectively). Each node has a next reference and a previous reference that point to the nodes after it and before it. Write the following methods that operate on a doubly linked list. Make sure that the list is still valid after the operation is performed. Make sure that your operations can handle special cases of an empty list and one element list. A Write a method that adds a node at the beginning of the doubly linked list 1 void addfront ( data ) 2 if (data == null ) throw new IllegalArgumentException("null parameter detected"); 3 Node n = new Node ( data ) 4 if ( head == null ) 5 head = n 6 tail = n 7 else 8 n.next = head 9 head.previous = n 10 head = n 6

B Write a method that adds a node at the end of the doubly linked list 1 void addback ( data ) 2 if (data == null ) throw new IllegalArgumentException("null parameter detected"); 3 Node n = new Node ( data ) 4 if ( head == null ) 5 head = n 6 tail = n 7 else 8 n.previous = tail 9 tail.next = n 10 tail = n C Write a method that removes a node at the beginning of the doubly linked list 1 typeofdata removefront ( ) 2 if ( head == null ) // z e r o n o d e s 3 return null 4 if ( head == tail ) // o n l y o n e n o d e 5 tmp = head.data 6 head = null 7 tail = null 8 return tmp 9 else 10 tmp = head.data 11 head = head.next 12 head.previous = null 13 return tmp D Write a method that removes a node at the end of the doubly linked list 1 typeofdata removefront ( ) 2 if ( head == null ) // z e r o n o d e s 3 return null 4 if ( head == tail ) // o n l y o n e n o d e 5 tmp = head.data 6 head = null 7 tail = null 8 return tmp 9 else 10 tmp = tail.data 11 tail = tail.previous 12 tail.next = null 13 return tmp Problem 7 Write a method of a LinedList class that computes and returns the number of nodes stored in the linked list. Assume that there is a data field called head that contains the reference to the first node. Provide both iterative and recursive implementations. 1 int size ( ) 2 Node current = head 3 int counter = 0 4 while current!= null 5 counter ++ 6 return counter 1 int size ( ) 2 return size ( head ) 7

3 4 5 int size ( Node n ) 6 if (n == null ) return 0 7 else return 1 + size (n.next ) 8 Problem 8 Write a method of a LinkedList class that computes and returns the sum of the values stored in the nodes. Assume that the node definition is as follows: class Node { int data; Node next; } 1 int sum ( ) 2 Node current = head 3 int sum = 0 4 while current!= null 5 sum += current.data 6 return sum Problem 9 Convert the following infix expressions to postfix and to prefix. a b + c a b c + + a b c a (b/c d) a b c / d a / b c d (a b/c) d a b c / d a / b c d a b/(c d) a b c d / a / b c d a (b + c d)/e a b c d + e / a / + b c d e a (b + c) d/e a b c + d e / a / + b c d e a (b + c) (d/e) a b c + d e / a + b c / d e (a b) + c d/e a b c d e / + + a b / c d e Problem 10 Evaluate the following postfix expressions for the following values of the variables: a = 7, b = 3, c = 12, d = 5 and e = 1. a b c + -8 a b c d + 52 a b + c d e + -7 8

Extra Challenge 1 1 Write a method of a linked list that performs a partition of the values stored in the list around a value x. All nodes less than x should come before all nodes greater than or equal to x. Extra Challenge 2 Implement a method to check if a linked list is a palindrome (in the broad sense: first element is the same as last, etc). You should pass through the list only once. You may use other data structures. You may assume that the length of the list is known. 1 This is a challenging problem that maybe useful for a job interview. It is not the type of problem that you may see on the exam. 9