Comp 182 Midterm

Ace your homework & exams now with Quizwiz!

If myList is initially empty, what sequence of operations creates the list (75, 76, 68)?

ListAppend(myList, node 75) ListAppend(myList, node 76) ListAppend(myList, node 68)

Given the list (27, 40, 15, 25, 10, 19, 30), what are the new partitioned lists if the pivot is 25?

(10, 15, 19, 25) and (27, 30, 40)

Given the list (11, 22, 25), which is the correct way to add 32 to the list in ascending order?

InsertAfter(list, 25, 32)

The Big O notation of the algorithm 7 + 12N + 3N^2 is _____.

N^2

ListSearch visits _____ when searching for 83 in the singly-linked list (80, 81, 82, 83).

all the nodes

The following algorithm is an example of _____. def MyMath(num) if num <= 1 return num return MyMath(num - 2) + MyMath(num - 1)

an exponential runtime complexity

Which XXX completes the insertAfter() method in the Java LinkedList class for a singly-linked list? public void insertAfter(Node currentNode, Node newNode) { if (m_head == null) { m_head = newNode; m_tail = newNode; } else if (currentNode == m_tail) { m_tail.next = newNode; m_tail = newNode; } else { newNode.next = XXX; currentNode.next = newNode; } }

currentNode.next

Which XXX completes the removeAfter() method in the Java LinkedList class for a singly-linked list? public void removeAfter(Node currentNode) { if (currentNode == null && m_head != null) { Node succeedingNode = m_head.next; m_head = succeedingNode; if (succeedingNode == null) { m_tail = null; } } else if (currentNode.next != null) { Node succeedingNode = XXX; currentNode.next = succeedingNode; if (succeedingNode == null) { m_tail = currentNode; } } }

currentNode.next.next

Which representation is correct for a doubly-linked list with 2 nodes?

data:Tom data:Sam next:-> next:->null null<-:prev <-:prev

Which XXX would replace the missing statement in the following algorithm? ListInsertAfter(students, curNode, newNode) { if (students⇢head == null) { students⇢head = newNode students⇢tail = newNode } XXX { students⇢tail⇢next = newNode newNode⇢prev = students⇢tail students⇢tail = newNode } else { sucNode = curNode⇢next newNode⇢next = sucNode newNode⇢prev = curNode curNode⇢next = newNode sucNode⇢prev = newNode } }

else if (curNode == students⇢tail)

Which XXX would replace the missing statement in the following algorithm to remove a node from the singly-linked list? ListRemoveAfter(list, curNode) { if (curNode is null && list⇢head is not null) { sucNode = list⇢head⇢next list⇢head = sucNode if (sucNode is null) { XXX } } else if (curNode⇢next is not null) { sucNode = curNode⇢next⇢next curNode⇢next = sucNode if (sucNode is null) { list⇢tail = curNode } } }

list⇢tail = null

Assume an ordered list containing the English alphabet. Using linear search, how many letters are checked to locate the letter "K"?

11

When the Java code below is executed, the first two elements swapped are ____. int[] numbers = { 15, 39, 77, 14 }; selectionSort(numbers);

15 and 14

In a sorted list of prime numbers, how long will it take to search for 29 if each comparison takes 2 µs?

20 µs

Which of the following is an example of constant time O(1) ?

Accessing an element of an array

ListTraverseReverse must traverse a linked list in reverse order. Which XXX should replace the missing statement? ListTraverseReverse(list) { XXX } ListTraverseReverseRecursive(node) { if (node is not null) { ListTraverseReverseRecursive(node⇢next) Visit node } }

ListTraverseReverseRecursive(list⇢head)

Given the list (40, 42, 45, 46, 47), identify the commands for the following operations. Remove 45 Confirm that 45 is removed

Remove(list, 45) Search(list, 45)

Which XXX should replace the missing statement in the following algorithm? ListSearch(myData, key) { return ListSearchRecursive(key, myData⇢head) } ListSearchRecursive(key, node) { if (node is not null) { XXX { return node } return ListSearchRecursive(key, node⇢next) } return null }

if (node⇢data == key)

Which XXX completes the Java selectionSort() method? void selectionSort(int[] numbers) { for (int i = 0; i < numbers.length - 1; i++) { // Find index of smallest remaining element int indexSmallest = i; for (int j = i + 1; j < numbers.length; j++) { XXX { indexSmallest = j; } } // Swap numbers[i] and numbers[indexSmallest] int temp = numbers[i]; numbers[i] = numbers[indexSmallest]; numbers[indexSmallest] = temp; } }

if (numbers[j] < numbers[indexSmallest])

Which XXX completes the append() method in the Java LinkedList class for a singly-linked list? public void append(Node newNode) { if (m_head == null) { m_head = newNode; m_tail = newNode; } else { XXX m_tail = newNode; } }

m_tail.next = newNode;

In the Java binarySearch() method, what code is used to compute the middle index?

mid = (high + low) / 2;

Which XXX completes the prepend() method in the Java LinkedList class for a singly-linked list? public void prepend(Node newNode) { if (m_head == null) { m_head = newNode; m_tail = newNode; } else { XXX m_head = newNode; } }

newNode.next = m_head;

Which XXX completes the append() method in the Java LinkedList class for a doubly-linked list? public void append(Node newNode) { if (m_head == null) { m_head = newNode; m_tail = newNode; } else { m_tail.next = newNode; XXX m_tail = newNode; } }

newNode.previous = m_tail;

Which XXX completes the following algorithm for inserting a new node into a singly-linked list? ListInsertAfter(list, curNode, newNode) { if (list⇢head == null) { list⇢head = newNode list⇢tail = newNode } else if (curNode == list⇢tail) { list⇢tail⇢next = newNode list⇢tail = newNode } else { XXX } }

newNode⇢next = curNode⇢next curNode⇢next = newNode

In a singly-linked list with 1 element, the tail pointer ____ and the next pointer of the head node ____.

points to the head node, is null

Given the following list, which statements are executed for the operation ListRemove(students, node Sam)? (there was a graph) head->1rst data tail->last data

sucNode⇢prev = predNode predNode⇢next = sucNode

Which XXX completes the insertAfter() method in the Java LinkedList class for a doubly-linked list? public void insertAfter(Node currentNode, Node newNode) { if (m_head == null) { m_head = newNode; m_tail = newNode; } else if (currentNode == m_tail) { m_tail.next = newNode; newNode.previous = m_tail; m_tail = newNode; } else { Node successor = currentNode.next; newNode.next = successor; newNode.previous = currentNode; currentNode.next = newNode; XXX } }

successor.previous = newNode;

Identify the error in the following algorithm to search for a node in the singly-linked list of students. ListSearch(students, key) { curNode = students⇢head while (curNode is null) { if (curNode⇢data == key) { return curNode } curNode = curNode⇢next } return null }

while (curNode is not null)

What is the runtime complexity notation for the following algorithm? LowNum(listOfAgeGroups, listSize, myAge) { for (ctr = 0; ctr < listSize; ++ctr) { if (listOfAgeGroups[ctr] == myAge) return ctr } } }

O(N)

What is the Big O notation for the following algorithm? ctr = 0 while (ctr < N) { myAge = ctr val = ctr + 1 while (val < N) { if (ageOfPeople[val] < ageOfPeople[myAge]) myAge = val ++val } tempList = ageOfPeople[ctr] ageOfPeople [ctr] = ageOfPeople [myAge] ageOfPeople [myAge] = tempList ++ctr }

O(N^2)

The Big O notation of the composite function 5N^3 + O(N^2) is _____.

O(N^3)

Which XXX would replace the missing statements in the following code to prepend a node in a doubly-linked list? ListPrepend(students, newNode) { if (list⇢head == null) { list⇢head = newNode list⇢tail = newNode } else { XXX list⇢head = newNode } }

newNode⇢next = list⇢head list⇢head⇢prev = newNode


Related study sets

Geology Exam 2 DSM & Quiz Questions

View Set

Chapter 2: Traditional and Contemporary Management Perspectives

View Set

Types of Informative Presentations

View Set

Microbiology Homework Chapter 2 Study Guide

View Set

Chapter 6: Prices and Unemployment

View Set

DH 228- Muscles of head and neck

View Set

Part 2: Human Activities and Global Climate Change

View Set