Linked List Practice Questions

Similar documents
Linked Lists and other Dynamic Data Structures

Dynamic Data Structures

BBM 201 DATA STRUCTURES

CSCE 2014 Final Exam Spring Version A

(Section : Computer Science)

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CS 1704 Homework 2 C++ Pointer Basics Fall 2002

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

Name: UTLN: CS login: Comp 15 Data Structures Midterm 2018 Summer

Data Structures (CS301) LAB

Ashish Gupta, Data JUET, Guna

Review for Test 1 (Chapter 1-5)

Section 1: True / False (2 points each, 30 pts total)

Largest Online Community of VU Students

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

C Pointers. 6th April 2017 Giulio Picierro

CA31-1K DIS. Pointers. TA: You Lu

CSI33 Data Structures

106B Final Review Session. Slides by Sierra Kaplan-Nelson and Kensen Shi Livestream managed by Jeffrey Barratt

Spring 2008 Data Structures (CS301) LAB

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Pointers: 2 Operators:

Come and join us at WebLyceum

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Introduction to Programming in C Department of Computer Science and Engineering

CS 106X Sample Final Exam #2

Topic 11 Linked Lists

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Largest Online Community of VU Students

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

CS201- Introduction to Programming Current Quizzes

EECE.2160: ECE Application Programming

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

True or False (15 Points)

Solution to CSE 250 Final Exam

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Exam 3 Chapters 7 & 9

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Summer Final Exam Review Session August 5, 2009

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Programming II (CS300)

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Array Elements as Function Parameters

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Solution for Data Structure

Pointers, Dynamic Data, and Reference Types

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CA341 - Comparative Programming Languages

Lesson 8: Linked List Deque

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Final Quiz-4 Time-1hr :30min UE:101-Algorithms & Programming

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

1. Which of the following best describes the situation after Line 1 has been executed?

CSCE 110 PROGRAMMING FUNDAMENTALS

Programming in C/C Lecture 2

CMSC 341 Lecture 7 Lists

More loops Ch

CSCE 110 PROGRAMMING FUNDAMENTALS

C:\Temp\Templates. Download This PDF From The Web Site

C Programming, Autumn 2013, Exercises for the Second Week

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

OF VICTORIA EXAMINATIONS- DECEMBER 2011 CSC

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

CS32 Discussion Week 3

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Largest Online Community of VU Students

Lecture 23: Pointer Arithmetic

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Chapter 13. Functions and Parameter Passing (Part 2)

FORM 2 (Please put your name and form # on the scantron!!!!)

Introduction to Computer Science II CS S-20 Linked Lists III

Parallel access to linked data structures

CS 216 Exam 1 Fall SOLUTION

mypoly

Section - Computer Science

Skip Lists: Motivation

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Introduction to Computer Systems. Exam 2. April 10, Notes and calculators are permitted, but not computers.

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

A collision is said to occur when two or more keys hash to the same index location.

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Print out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.

True or False (14 Points)

C++_ MARKS 40 MIN

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Dynamic Allocation of Memory

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

Transcription:

Linked List Practice Questions 1. The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function. /* head_ref is a double pointer which points to head (or start) pointer of linked list */ void reverse(node* head_ref) node* prev = NULL; node* current = head_ref; node* next; while (current!= NULL) next = current->link(); current->set_link(prev); prev = current; current = next; /*ADD A STATEMENT HERE*/ What should be added in place of "/*ADD A STATEMENT HERE*/", so that the function correctly reverses a linked list. A. head_ref = prev; B. head_ref = current; C. head_ref = next; D. head_ref = NULL; 2. The following function takes a simply-linked list as input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code is left blank. Choose the correct alternative to replace the blank line. node *move_to_front(node *head) node *p, *q; if ((head == NULL: (head->link() == NULL)) return head; q = NULL; p = head; while (p->link()!=null) q = p; p = p->link(); return head; A. q = NULL; p->link() = head; head = p; B. q->link() = NULL; head = p; p->link() = head; C. head = p; p->link() = q; q->link() = NULL;

D. q->link() = NULL; p->link() = head; head = p; 3. In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is A. log 2 n B. n/2 C. log 2 n 1 D. n 4. What are the time complexities of finding 8th element from beginning and 8th element from end in a singly linked list? Let n be the number of nodes in linked list, you may assume that n > 8. A. O(1) and O(n) B. O(1) and O(1) C. O(n) and O(1) D. O(n) and O(n) 5. You are given pointers to first and last nodes of a singly linked list, which of the following operations are dependent on the length of the linked list? A. Delete the first element B. Insert a new element as a first element C. Delete the last element of the list D. Add a new element at the end of the list 6. Consider the following function to traverse a linked list. void traverse(node *head) while (head->link()!= NULL) cout << head->data(); head = head->link(); Which of the following is FALSE about above function? A. The function may crash when the linked list is empty B. The function doesn't print the last node when the linked list is not empty

C. The function is implemented incorrectly because it changes head 7. What is the time complexity to count the number of elements in the linked list? a) O(1) b) O(n) c) O(logn) d) None of the mentioned 8. Implement a function (the header is shown as follows) to delete the last element in the singly linked list. node* removelast(node * head) 9. What is the functionality of the following piece of code? int function(int data) node* temp = header_ptr; int var = 0; while(temp!= NULL) if(temp->data() == data) return var; var++; temp = temp->link(); return -1; 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 10. Assume the following declarations and statements: node *P1, *P2; int *P3; P1 = new node; P2 = new node; P3 = new int; Show the output produced by the following segments of code. Indicate if an error occurs. 1) P1->set_data(123); P2->set_data(456); P1->set_link(p2);

2) P1->set_data(12); P1 = P2; 3) P1->set_data(123); P2->set_data(456); cout << P2->link()->data(); 4) P1->set_data(12); P3->set_data(34); P2->set_link(P3); cout << P3->data(); 5) P1->set_data(111); P2->set_data(222); cout << P1->data() << P2->data(); cout << P1->link()->link()->data(); 6) P1->set_data(12); P1 = P2; cout << P2->link()->data(); 11. Assume the following declarations and statements: node *P1, *P2, *P3, *P4; P1 = new node; P2 = new node; P3 = new node; P4 = new node; P1->set_data("Cat"); P2->set_data("Dog"); P3->set_data("Ewe"); P4->set_data("Rat"); P2->set_link(P3); P3->set_link(P4); P4->set_link(NULL); Show the output produced by the following segments of code. Indicate if an error occurs.

1) P1 = P2->link(); 2) P4 = P1; cout << P4->data(); 3) P4->set_data(P1->data()); cout << P4->data(); 4) P1->set_data(P3->link()->data()); cout << P1->data() << P1->link()->data(); 5) P2->set_link(P3->link()); 6) P4->set_link(P1); cout << P4->link()->data(); 7) P4->set_link(P2->link()); P3 = P1->link(); cout << P1->link()->data() << P2->link()->data() << P3->link->data() << P4->link()->data(); 8) P4->set_link(P3); P4->link()->link() = P2; P4->link()->link()->link() = P1; P1 = NULL; cout << P1 << P2->link()->data() << P3->link()->data() << P4->link();