master
Assignment 1 - Computer Science 272: Data Structures and Algorithms
Answer question 1, question 2, and any other 2 questions from questions 3 to 6 – maximum 100 marks. You must score at least 50 to pass the assignment.
- You have learned some fundamental data structure concepts such as array, queue and priority queue, stack, list and linked list, sequence, and unordered set, and you understand the concept of interface or abstract data type that defines the set of operations supported by a data structure and the semantics, or meaning, of those operations. You can use the interface of one particular data structure to define or implement the operations of a different data structure.
- a. Describe the meaning of the essential methods
add(x),deleteMin(), andsize()that are supported by the priority queue interface. Implement those methods using a singly-linked list. Analyze the running time of theadd(x)anddeletMin()operations based on this implementation.- The
add(x)method is used to add an element to the queue with a priority. - The
deleteMin()method is used to remove the element with the lowest priority from the queue and return it. - The
size()method is used to determine how many items are in the queue. - The
add(x)function is linear time O(n). - The
deleteMin(x)function is constant time O(1).
- The
- b. Implement the stack methods
push(x)andpop()using two queues. Analyze the running time of thepush(x)andpop()operations based on this implementation.
- Swap two adjacent elements in a list by adjusting only the links (and not the data) using:
- a. singly-linked list.
- b. doubly-linked list.
- Using a
USet, implement aBag. ABagis like aUSet- it supports theadd(x),remove(x), andfind(x)methods - but it allows duplicate elements to be stored. Thefind(x)operation in a Bag returns some element (if any) that is equal to x. In addition, aBagsupports thefindAll(x)operation that returns a list of all elements in the Bag that are equal to x. - Design and implement a
RandomQueue. This is an implementation of the Queue interface in which theremove()operation removes an element that is chosen uniformly at random among all the elements currently in the queue. (Think of a RandomQueue as a bag in which we can add elements or reach in and blindly remove some random element.) Theadd(x)andremove()operations in aRandomQueueshould run in constant time per operation. - Write a method,
reverse(), that reverses the order of elements in aDLList - Design and implement a
MinStackdata structure that can store comparable elements and supports the stack operationspush(x),pop(), andsize(), as well as themin()operation, which returns the minimum value currently stored in the data structure. All operations should run in constant time.