DS Quiz 1

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

Given the Node class implementation, select one of the following that correctly inserts a node at the tail of the list. public class Node { protected int data; protected Node prev; protected Node next; public Node(int data) { this.data = data; prev = null; next = null; } public Node(int data, Node prev, Node next) { this.data = data; this.prev = prev; this.next = next; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Node getPrev() { return prev; } public void setPrev(Node prev) { this.prev = prev; } public Node getNext { return next; } public void setNext(Node next) { this.next = next; } } public class DLL { protected Node head; protected Node tail; int length; public DLL() { head = new Node(Integer.MIN_VALUE,null,null); tail = new Node(Integer.MIN_VALUE,null,null); head.setNext(tail); length = 0; } } a)public void insertRear(int data) { Node node = new Node(data,tail.getPrev(),tail); node.getPrev().setNext(node); tail.setPrev(node); length++; } b)public void insertRear(int data) { Node node = new Node(data,tail.getPrev(),tail); node.getPrev().getPrev().setNext(node); tail.setPrev(node); length++; } c)public void insertRear(int data) { Node node = new Node(data,tail.getPrev(),tail); node.getPrev().setNext(tail); tail.setPrev(node); length++; } d)public void insertRear(int data) { Node node = new Node(data,head,tail); node.getPrev().setNext(node); tail.setPrev(node); length++; }

A

How do you insert a node at the beginning of the list? a)public class insertFront(int data) { Node node = new Node(data, head, head.getNext()); node.getNext().setPrev(node); head.setNext(node); size++; } b)public class insertFront(int data) { Node node = new Node(data, head, head); node.getNext().setPrev(node); head.setNext(node); size++; } c)public class insertFront(int data) { Node node = new Node(data, head, head.getNext()); node.getNext().setPrev(head); head.setNext(node); size++; } d)public class insertFront(int data) { Node node = new Node(data, head, head.getNext()); node.getNext().setPrev(node); head.setNext(node.getNext()); size++; }

A

How do you insert an element at the beginning of the list? a)public void insertBegin(Node node) { node.setNext(head); head = node; size++; } b)public void insertBegin(Node node) { head = node; node.setNext(head); size++; } c)public void insertBegin(Node node) { Node temp = head.getNext() node.setNext(temp); head = node; size++; } d) None of the mentioned

A

How would you delete a node in the singly linked list? The position to be deleted is given. a) 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--; } b)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()); } size--; } c)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().getNext(); } temp.setNext(temp.getNext().getNext()); } size--; } d)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=0; i<pos; i++) { temp = temp.getNext(); } temp.setNext(temp.getNext().getNext()); } size--; }

A

What is a memory efficient double linked list? a) Each node has only one pointer to traverse the list back and forth b) The list has breakpoints for faster traversal c) An auxiliary singly linked list acts as a helper list to traverse through the doubly linked list d) None of the mentioned

A

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++; } a) Inserting a node at the beginning of the list b) Deleting a node at the beginning of the list c) Inserting a node at the end of the list d) Deleting a node at the end of the list

A

Which of the following performs deletion of the last element in the list? Given below is the Node class. class Node { protected Node next; protected Object ele; Node(Object e,Node n) { ele = e; next = n; } public void setNext(Node n) { next = n; } public void setEle(Object e) { ele = e; } public Node getNext() { return next; } public Object getEle() { return ele; } } class SLL { Node head; int size; SLL() { size = 0; } } a) 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; } b) public void removeLast() { if(size == 0) return null; Node cur; Node temp; cur = head; while(cur != null) { temp = cur; cur = cur.getNext(); } temp.setNext(null); return cur; } c) public void removeLast() { if(size == 0) return null; Node cur; Node temp; cur = head; while(cur != null) { cur = cur.getNext(); temp = cur; } temp.setNext(null); return cur; } d) public void removeLast() { if(size == 0) return null; Node cur; Node temp; cur = head; while(cur.getNext() != null) { cur = cur.getNext(); temp = cur; } temp.setNext(null); return cur; }

A

Which of the following piece of code removes the node from a given position? a) 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--; } b)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.getNext()); temp.getPrev().setNext(temp.getPrev()); } size--; } c)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().getNext(); } temp.getNext().setPrev(temp.getPrev()); temp.getPrev().setNext(temp.getNext()); } size--; } d) None of the mentioned

A

Which of the following piece of code has the functionality of counting the number of elements in the list? a)public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { size++; cur = cur.getNext(); } return size; } b)public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { cur = cur.getNext(); size++; } return size; } c)public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { size++; cur = cur.getNext(); } } d)public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { size++; cur = cur.getNext().getNext(); } return size; }

A or B

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); a) head-6-1-2-3-4-5-tail b) head-6-1-2-3-4-tail c) head-1-2-3-4-5-6-tail d) head-1-2-3-4-5-tail

B

What are the basic components of a linked list? A. head and tail are the only important components B. data members for the information to be stored and a link to the next item C. generic class because without this linked list is not possible

B

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(); } a) Return the element at the tail of the list but do not remove it b) Return the element at the tail of the list and remove it from the list c) Return the last but one element from the list but do not remove it d) Return the last but one element at the tail of the list and remove it from the list

B

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); a) head-0-1-2-3-4-5-6-tail b) head-1-2-3-4-5-6-tail c) head-6-1-2-3-4-5-0-tail d) head-0-1-2-3-4-5-tail

C

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; } a) Find and delete a given element in the list b) Find and return the given element in the list c) Find and return the position of the given element in the list d) Find and insert a new element in the list

C

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. newNopde = new Node(); newNode.info = 50; newNode.link = p.link; p.link = newNode; A. Code 1 B. Code 2 C. Code 3 D. Code 2 and 3

C

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

Which of these is an application of linked lists? a) To implement file systems b) For separate chaining in hash-tables c) To implement non-binary trees d) All of the mentioned

D


Conjuntos de estudio relacionados

HBS - Lesson 2.1 and 2.2 Assessment

View Set

Chapter Exam - Life premiums and benefits

View Set

Psych Exam #4 Canvas Quiz (Ch. 12, 14, 15, 16)

View Set

Wong : Musculoskeletal or Articular Dysfunction

View Set

chinese 好词好句(part 1)

View Set