Chapter 9: Linear Data Structures

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

For questions 8 - 11, assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. Assume Node temp references the last element of the linked list. Which of the following conditions is true about temp? (temp.info = = 0) (temp.next = = null) (temp = = head) (temp = = null) (temp.next = = null && temp.info = = null)

(temp.next = = null)

Assume that the countIt and sumIt methods in 13 and 14 receive a parameter Node temp, which references the first Node in a linked list where Node is a class that consists of data instances int info and Node next and further assume that the int variables count and sum are initialized to 0. *Which of the following methods could be used to count the number of items in the linked list? public int countIt(Node temp) { while(temp != null) { count += temp.info; temp = temp.next; } return count; } public int countIt(Node temp) { while(temp != null) { count++; } return count; } public int countIt(Node temp) { while(count != null) { count++; temp = temp.next; } return count; } public int countIt(Node temp) { while(temp != head) { count++; temp = temp.next; } return count; } public int countIt(Node temp) { while(temp != null) { if(next != null) count++; temp = temp.next; } return count; }

** public int countIt(Node temp) { while(count != null) { count++; temp = temp.next; } return count; }

*Abstract Data Types have which of the following object-oriented features? Information hiding Inheritance Polymorphism Message Passing All of the above

** Information hiding

For questions 23 - 24, assume Node is a class consisting of an int info and a Node next and header is a HeaderNode that has Node front, rear and int count where front references the first item in the list, rear references the last item in the list and count is the number of elements in the list. *Previously, to iterate through a linked list, we used a while loop. Which of the following for-loops could replace the previous while loop that would start at head and go until temp == null? for(Node temp = header.front, int j = 0; j < header.count; temp = temp.next) { ... } for(int j = 0; j < header.count; j++) { ... } for(Node temp = header.front; temp != header.rear; temp = temp.next) { ... } for(Node temp = header.front, int j = 0; j < header.count; temp = temp.next, j++) { ... } for(Node temp = header.front, int j = 0; j < header.count && temp != header.rear; temp = temp.next, j++) { ... }

** for(Node temp = header.front, int j = 0; j < header.count; temp = temp.next, j++) { ... }

*A variation of a linked list is a circular linked list where the last Node in the list has next = head rather than next = null. One problem with this type of list is that it wastes memory space since head already points at the first Node, so the last one does not need to there is no ability to add a new Node at the end of the list since the last Node points at the first Node it is more difficult to traverse the list since the old terminating condition, (next = = null), is no longer true for the last node a header Node for this type of list is more complex all of the above

** it is more difficult to traverse the list since the old terminating condition, (next = = null), is no longer true for the last node

For questions 23 - 24, assume Node is a class consisting of an int info and a Node next and header is a HeaderNode that has Node front, rear and int count where front references the first item in the list, rear references the last item in the list and count is the number of elements in the list. *This type of linked list is referred to as a singly linked list doubly linked list singly linked list with a header node doubly linked list with a header node singly linked list with a head and rear references

** singly linked list with a header node

For questions 8 - 11, assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. *Assume that the linked list has at least two Nodes in it. Which of the following instructions will return the second int value in the list? while (head != null) head = temp.next; while (temp != null) temp = temp.next; while (head != null) temp = temp.next; while (head != null) head = head.next; while (temp != null) head = head.next;

** while (head != null) temp = temp.next;

For questions 15 - 18, assume that a linked list consists of Node objects, where Node has two instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list list. What will be returned by return head.next.next.next.info; ? 20 11 13 19 12

19

For questions 15 - 18, assume that a linked list consists of Node objects, where Node has two instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list list. What is returned by return head.info; ? 20 11 13 19 6

20

Which of the following is considered an Abstract Data Type? array reference variable any of the primitive types (e.g., int, double, char) ArrayList all of the above

ArrayList

Which of the following criticisms of an array is applicable to a Java array? It is an inefficient structure to access random elements It only supports First-in First-out types of accesses It is fixed in size (static) It cannot be used to create an Abstract Data Type such as a Queue or Stack All of the above

It is fixed in size (static)

The expression LIFO stands for LIst FOundation LInk FOrmation Last In Front Of Last In First Out Long Int Float Object

Last In First Out

For questions 8 - 11, assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. Which of the following instructions would create an initially empty linked list? Node head = new Node( ); Node head = Node; Node head = null; Node head = new Node(0); Node head = list;

Node head = null;

Which of the following creates a queue data structure using the java.util classes? Queue q = new Queue(); Queue q = new Object(); Queue q = new LinkedList(); Queue q = new ArrayList() Queue q = Object[];

Queue q = new LinkedList();

In order to gain a last-in first-out access to data, which type of structure should be used? ArrayList Array Linked List Queue Stack

Stack

For questions 15 - 18, assume that a linked list consists of Node objects, where Node has two instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list list. What is the result to the linked list of the following instructions? Assume that newNode is a Node, already constructed. newNode.data = 1; newNode.next = head.next; head.next = newNode; The value 1 is inserted into the linked list before 20 The value 1 is inserted into the linked list after 20 and before 11 The value 1 is inserted into the linked list after 11 and before 13 The value 1 is inserted into the linked list after 13 and before 19 The value 1 is inserted into the linked list after 20 and the rest of the list is lost

The value 1 is inserted into the linked list after 20 and before 11

A linked list that stores int values would be comprised of a group of Nodes. We might define the Node by class Node { Node next; } class Node { int next; } class Node { int data; } class Node { int data; Node next; } class Node { int[ ] data; Node next; }

class Node { int data; Node next; }

An array can be classified as what type of object? dynamic ordered first-in first-out heteogeneous collection

collection

Assume Node2 is defined as follows: int data; Node2 a, b; where a refers to the Node2 before this one in a linked list and b refers to the Node2 after this one in a linked list. Node2 could then be used to create which of the following variations of a linked list? singly linked list doubly linked list singly linked list with a header node singly linked list with a top node circularly linked list

doubly linked list

A collection whose items are of different types is referred to as a(n) _______ type. homogeneous heterogeneous dynamic abstract vector

heterogeneous

The advantage of creating a BookList using a linked list instead of using an array is that the linked list offers easier access to a random element in the list uses less memory is easier to implement and debug can store types other than Books unlike the array is dynamic and so can be any size needed

is dynamic and so can be any size needed

For questions 15 - 18, assume that a linked list consists of Node objects, where Node has two instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list list. What will the statement head.next.next = head.next.next.next; accomplish? it will result in the list ending after 13 it will result in the value 13 being deleted from the list it will result in the list ending after 19 it will result in the value 19 being deleted from the list it will result in the list ending after 12

it will result in the value 13 being deleted from the list

One operation that we might want to implement on a Stack and a Queue is full, which determines if the data structure has room for another item to be added. This operation would be useful only if the Queue or Stack is implemented using an array only if the Queue or Stack is implemented using a linked list only for a Queue only for a Stack none of the above, a full operation is not useful at all

only if the Queue or Stack is implemented using an array

Which of the following operations is used to see the top item of a stack without removing it from the stack? push pop peek see top

peek

At the emergency animal hospital, those pets with more serious injuries are served first. Which data structure best represents this situation? queue priority queue stack linked list array

priority queue

Assume that the countIt and sumIt methods in 13 and 14 receive a parameter Node temp, which references the first Node in a linked list where Node is a class that consists of data instances int info and Node next and further assume that the int variables count and sum are initialized to 0. Which of the following methods could be used to sum all of the items in the linked list? public int sumIt(Node temp) { while(temp != null) { sum += temp.info; temp = temp.next; } return sum; } public int sumIt(Node temp) { while(temp != null) { sum += temp.info; } return sum; } public int sumIt(Node temp) { while(temp != null) { sum++; temp = temp.next; } return sum; } public int sumIt(Node temp) { while(temp != head) { sum += temp.info; temp = temp.next; } return sum; } public int sumIt(Node temp) { while(temp != null) { if(next != null) sum += temp.info; temp = temp.next; } return sum; }

public int sumIt(Node temp) { while(temp != null) { sum += temp.info; temp = temp.next; } return sum; }

For questions 8 - 11, assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. Assume that the linked list has at least two Nodes in it. Which of the following instructions will return the second int value in the list? return head.info; return head.next.info; return head.next.next.info; return head.next.next.next.info; It is not possible to return the second int value in the list using head.

return head.next.info;


Ensembles d'études connexes

TestOut PC Pro A+ 220-801 and 220-802

View Set

WCU Study Review 3: Ch. 16, 18, 19, 20

View Set

Back Muscles: Origin, Insertion, Innervation, Blood Supply & Action

View Set

СПО модуль - тема "Судоустрій"

View Set

Chapter 2 SmartBook (Operation Management)

View Set

Media Ethics and Law Rutgers Kremen

View Set

Urban Sociology - Experiencing Cities - Chapters 1-5

View Set