Quiz 1 data structures

¡Supera tus tareas y exámenes ahora con Quizwiz!

A linked list class is defined with the following heading. public class UnorderedLinkedList<T> extends LinkedListClass<T> What is the proper syntax for creating a reference variable of the linked list to hold strings?

UnorderedLinkedList list;

. How would you delete a node in the singly linked list? The position to be deleted is given

) public void delete(int pos) { if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { Node temp = head; for(int i=1; i<pos; i++) { temp = temp.getNext(); } temp.setNext(temp.getNext().getNext()); } size--; }

Given the fixed size of an array is not important, which class is more efficient at storing and retrieving information?

an array because of the reduced code and efficient storage allocation

. What is the functionality of the following piece of code? public int function(int data) { Node temp = head; int var = 0; while(temp != null) { if(temp.getData() == data) { return var; } var = var+1; temp = temp.getNext(); } return Integer.MIN_VALUE; }

Find and return the position of the given element in the list

What is the functionality of the following code public void function(Node node) { if(size == 0) head = node; else { Node temp,cur; for(cur = head; (temp = cur.getNext())!=null; cur = temp); cur.setNext(node); } size++; }

Inserting a node at the end of the list

. What is the difference between building a linked list FORWARD and BACKWARDS?

Nothing, only the insertion of the information at the head or tail of the linked list

What is the functionality of the following piece of code public int function() { Node temp = tail.getPrev(); tail.setPrev(temp.getPrev()); temp.getPrev().setNext(tail); size--; return temp.getItem(); }

Return the element at the tail of the list and remove it from the list

Which of these is an application of linked lists

To implement file systems b) For separate chaining in hash-tables c) To implement non-binary trees d) All of the mentioned

. Which of the following is false about a doubly linked list?

a) We can navigate in both the directions b) It requires more space than a singly linked list c) The insertion and deletion of a node take a bit longer d) None of the mentioned d

What are the basic components of a linked list

data members for the information to be stored and a link to the next item

What does ADT stand for

Abstract Data Type

. The instance variables first and last (or head and tail) below to which class of a linked list?

class LinkedListClass

What should the default constructor of the LinkedListClass perform?

. Initialize the first and last (or head and tail) and set count to zero

. How does a doubly linked list (DLL) compare to the single linked list (SLL)

A DLL has two links for traversing the list but does not execute faster when traversing

Which of the following is not a disadvantage to the usage of array

Accessing elements at specified positions

A linked list is different from an array, because why

An array is fixed in size but a linked list is dynamically sizable

. What does the following code represent? public interface LinkedListADT<T> extends Cloneable

C. An abstract class used to further define a linked list class .

What is the purpose of using an ADT for the linked list class?

C. To force a specific set of methods to be used for the subclasses

. Which of the following code fragments properly inserts 50 into the linked list at position after node p? 1. newNode = new Node(); newNode.info = 50; p.link = newNode; newNode.link = p.link; 2. newNode = new Node(50, p); 3. newNode = new Node(); newNode.info = 50; newNode.link = p.link; p.link = newNode;

Code 3

What is a memory efficient double linked list

Each node has only one pointer to traverse the list back and forth

. A linked list must contain a maximum size component to manage the last item?

False

Internally, a linked list is implemented with an array for the storage of the information?

False

Consider the following doubly linked list: head-1-2-3-4-5-tail What will be the list after performing the given sequence of operations? Node temp = new Node(6,head,head.getNext()); Node temp1 = new Node(0,tail.getPrev(),tail); head.setNext(temp); temp.getNext().setPrev(temp); tail.setPrev(temp1); temp1.getPrev().setNext(temp1);

c) head-6-1-2-3-4-5-0-tail

. In addition to the info and link components, the linked list must also contain what other components

head and tail pointers to the first and last nodes

Consider the following doubly linked list: head-1-2-3-4-5-tail What will be the list after performing the given sequence of operations? Node temp = new Node(6,head,head.getNext()); head.setNext(temp); temp.getNext().setPrev(temp); Node temp1 = tail.getPrev(); tail.setPrev(temp1.getPrev()); temp1.getPrev().setNext(tail);

head-6-1-2-3-4-tail

What is the proper code for accessing the information of the second item in a linked list

head.link.info

What method checks if a List Interface (ArrayList, LinkedList) or Collection is empty?

isEmpty()

What does the following fragment of code do with a linked list? current = head; while (current != null) { current = current.link; }

it traverses the list

Which of the following performs deletion of the last element in the list? Given below is the Node class

public Node removeLast() { if(size == 0) return null; Node cur; Node temp; cur = head; while(cur.getNext() != null) { temp = cur; cur = cur.getNext(); } temp.setNext(null); size--; return cur; }

. How do you insert a node at the beginning of the list?

public class insertFront(int data) { Node node = new Node(data, head, head.getNext()); node.getNext().setPrev(node); head.setNext(node); size++; }

Which of the following piece of code has the functionality of counting the number of elements in the list?

public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { size++; cur = cur.getNext(); } return size; }

. How do you insert an element at the beginning of the list

public void insertBegin(Node node) { node.setNext(head); head = node; size++; }

Given the Node class implementation, select one of the following that correctly inserts a node at the tail of the list.

public void insertRear(int data) { Node node = new Node(data,tail.getPrev(),tail); node.getPrev().setNext(node); tail.setPrev(node); length++; }

Which of the following piece of code removes the node from a given position

public void remove(int pos) { if(pos<0 || pos>=size) { System.out.println("Invalid position"); return; } else { if(head == null) return; if(pos == 0) { head = head.getNext(); if(head == null) tail = null; } else { Node temp = head; for(int i=1; i<position; i++) temp = temp.getNext(); } temp.getNext().setPrev(temp.getPrev()); temp.getPrev().setNext(temp.getNext()); } size--; }

What is a node used for in a linked list?

to store the information and the link to the next item


Conjuntos de estudio relacionados

Selective/Differential Media Quiz

View Set

History 102 Midterm Multiple Choice Study Guide Chapter 19

View Set

MyProgrammingLab Starting out with Python Ch.5

View Set

Unit 2 SOCIO: Race and Ethnicity

View Set

Com Arts 100: Chapter 7 (Gathering Materials)

View Set