CSCA48 Term Test 1 Seminar

Similar documents
What is an algorithm?

8. Fundamental Data Structures

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

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

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

First Name: Last Name: Q1: /10

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Data structure and algorithm in Python

1 P age DS & OOPS / UNIT II

Data Structures I: Linked Lists

PA3 Design Specification

Dynamic Data Structures

Data structure and algorithm in Python


Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

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

CSC148 Week 2. Larry Zhang

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

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

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

Queues. Queue ADT Queue Implementation Priority Queues

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Question 0. Do not turn this page until you have received the signal to start.

Data Structure. Recitation VII

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

DS L9: Queues

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

Abstract Data Types Chapter 1

CS350 - Exam 1 (100 Points)

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

Sequence Abstract Data Type

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

Programming. Lists, Stacks, Queues

CMPT 125: Practice Midterm Answer Key

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

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

Do not turn this page until you have received the signal to start.

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

811312A Data Structures and Algorithms, , Exercise 1 Solutions

Solution for Data Structure

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

Information Science 2

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

03/31/03 Lab 7. Linked Lists

Name CPTR246 Spring '17 (100 total points) Exam 3

THE UNIVERSITY OF WESTERN AUSTRALIA

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

Computer Science Fundamentals 107

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Apply to be a Meiklejohn! tinyurl.com/meikapply

Lecture 12 ADTs and Stacks

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

STACKS AND QUEUES. Problem Solving with Computers-II

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

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

March 13/2003 Jayakanth Srinivasan,

CSC148-Section:L0301

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer

Queues Fall 2018 Margaret Reid-Miller

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

ECE Spring 2018 Problem Set #0 Due: 1/30/18

Student Number: Lab day:

Queue Implementations

"apple" "grape" "grape" "grape" "apple"

Agenda & Reading. COMPSCI 105 SS 2015 Principles of Computer Science

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

CS 331 Midterm Exam 2

Fundamentals of Data Structure

Linked Lists, Stacks, Queues, and Priority Queues

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

Ashish Gupta, Data JUET, Guna

csci 210: Data Structures Stacks and Queues

Stacks and Queues

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Abstract Data Types 1

Randomized Queues and Deques

Lecture Data Structure Stack

Introduction to Data Structures

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

Computer Science First Exams Pseudocode Standard Data Structures Examples of Pseudocode

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs

Largest Online Community of VU Students

Ashish Gupta, Data JUET, Guna

Data Structures And Algorithms

Basic Data Structures

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1)

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

Largest Online Community of VU Students

Unit 4 Basic Collections

Queues The Queue ADT. The Queue ADT is defined by the following operations: init Initialize a new empty queue.

Chapter 18: Stacks And Queues

Abstract Data Types 1

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

16. Dynamic Data Structures

Transcription:

CSCA48 Term Test 1 Seminar Brian Chen and Joshua Concon January 30, 2017 Implementation class LLNode ( object ): A Node in a singly - linked list def init (self, data, link = None ): ( LLNode, object ) -> NoneType Create a new node to hold data self. data = data self. link = link class DLLNode ( object ): A Node in a doubly - linked list def init (self, data, nxt = None, prev = None ): ( DLLNode, object ) -> NoneType Create a new node to hold data self. data = data self. next = nxt self. prev = prev class EmptyStackError ( Exception ): pass class Stack (): A class to represent a Stack. Raises EmptyStackError when popping from an empty stack. def init ( self : Stack ) -> None : self. _contents = [] def str ( self : Stack ) -> str : s = "" 1

for item in self. _ contents : s = s + str ( item ) + ", " return s [: -2] def push ( self : Stack, item : object ) -> None : self. _contents. append ( item ) def pop ( self : Stack ) -> object : if self. isempty (): raise EmptyStackError return self. _contents. pop () def isempty ( self : Stack ) -> bool : return ( len ( self. _contents ) == 0) 2

Question 1 Implement a Queue using only the Stack ADT. Assume that module stack.py defines a class Stack that provides the usual methods: push(item), pop(). from stack import * class QueueUsingStack (): A Queue implemented with Stack Objects def init ( self ): def push (self, obj ): def pop ( self ): 3

Question 2 Write the body of the function below so that it satisfies its docstring. def reverse ( head ): ( LLNode ) -> LLNode Given the head pointer to a linked list, reverse the linked list and return a pointer to the head of the reversed list.) 4

Question 3 We are given a task by a customer. He wants to have a data structure that can be added to and removed from on both sides. If the data structure is empty, he would like to be notified by an error. Implement this ADT and write an Representative Invariant for it. class Deque (): A double ended queue def init ( self : Deque ) -> None : Representation invariant : def enqueue_left (self, item ): def enqueue_left (self, item ): def dequeue_left ( self ): 5

def dequeue_right ( self ): def is_empty ( self : Deque ): def get_deque ( self : Deque ): 6

Question 4 Finish the following function def remove (head, item ): ( DLLNode, Object ) -> DLLNode Removes the first DLLNode with the data item from the doubly - linked list and returns the list. If item is not in the list at head, just return the list. 7

Answers Question 1 from stack import * class QueueUsingStack (): A Queue implemented with Stack Objects def init ( self ): self. _inbox = Stack () self. _outbox = Stack () def push (self, obj ): self. _inbox. push ( obj ) def pop ( self ): if( self. _outbox. isempty ()): while ( not ( self. _inbox. isempty ())): self. _outbox. push ( self. _inbox. pop ()) return self. _outbox. pop () 8

Question 2 def reverse ( head ): ( LLNode ) -> LLNode Given the head pointer to a linked list, reverse the linked list and return a pointer to the head of the reversed list.) current = head previous = None while current : next = current. next current. next = previous previous = current current = next head = previous return head 9

Question 3 class Deque (): A double ended queue def init ( self : Deque ) -> None : # Representation invariant : # _ contents is a list of object. # _ contents [:] are the objects in the dequeue. # if i < j, i >=0, j < len ( _contents ), then # _ contents [ i] is to the left of _ contents [ j] in the deque # _ contents [ j] is to the right of _ contents [ i] in the deque self. _contents = [] def enqueue_left (self, item ): self. _contents = [ item ] + self. _contents def enqueue_left (self, item ): self. _contents = self. _contents + [ item ] def dequeue_left ( self ): if( self. is_empty ()): raise EmptyQueueError (" Can t dequeue from an empty queue ") else : item = self. _ contents [0] self. _contents = self. _contents [1:] return item def dequeue_right ( self ): if( self. is_empty ()): raise EmptyQueueError (" Can t dequeue from an empty queue ") else : item = self. _contents [ -1] self. _contents = self. _contents [: -1] return item def is_empty ( self : Deque ): return len ( self. _contents ) == 0 def get_deque ( self : Deque ): return self. _ contents 10

Question 4 def remove (head, item ): ( DLLNode, Object ) -> DLLNode Removes the first DLLNode with the data item from the doubly - linked list and returns the list. If item is not in the list at head, just return the list. current = head removed = False while (( current is not None ) and ( not removed )): if( current. cargo = item ): removed = True # if current is not the head if( current. prev is not None ): before = current. prev after = current. next before. next = after after. prev = before # if current is the head else : head = current. next head. prev = None current = current. next 11