Examination 1 - Data Structures | CSCI 270, Exams of Data Structures and Algorithms

Material Type: Exam; Class: DATA STRUCTURES; Subject: Computer Science - CSCI; University: Texas A & M University-Commerce; Term: Unknown 2005;

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-i45-1
koofers-user-i45-1 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 270 Exam 1 Linked Lists ( 30 June ) Name _________________________
Summer 2005
40 points total
1) (10 pts, 1pt each) Circle the correct answer, True or False, for each of the following
questions:
a. True False When an objects lifetime is over, the object’s destructor is
automatically called.
b. True False Nodes of a singly-linked list contain two members, a data member
and a pointer to the next node of the list.
c. True False A call to new returns a pointer to newly allocated memory.
d. True False Implementing a List data type using linked nodes is more efficient
in all cases than implementing the List using an array.
e. True False The three main features of an abstract list type are 1) its elements
can be of different types 2) it has a finite length and 3) the
elements have no special ordering.
f. True False NULL is used as a special value in linked lists to indicate that there
is no next element in the list.
g. True False You may dereference a NULL pointer in C++ without any problems.
h. True False A call to new to allocate new memory will always succeed.
i. True False Doubly-linked lists facilitate both forward and backward traversal of
the linked list.
j. True False The purpose of using a dummy head node in a list is so that every
node in the list has a valid predecessor.
pf3
pf4
pf5

Partial preview of the text

Download Examination 1 - Data Structures | CSCI 270 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CSCI 270 Exam 1 Linked Lists ( 30 June ) Name _________________________ Summer 2005 40 points total

  1. (10 pts, 1pt each) Circle the correct answer, True or False, for each of the following questions:

a. True False When an objects lifetime is over, the object’s destructor is automatically called. b. True False Nodes of a singly-linked list contain two members, a data member and a pointer to the next node of the list. c. True False A call to new returns a pointer to newly allocated memory.

d. True False Implementing a List data type using linked nodes is more efficient in all cases than implementing the List using an array. e. True False The three main features of an abstract list type are 1) its elements can be of different types 2) it has a finite length and 3) the elements have no special ordering. f. True False NULL is used as a special value in linked lists to indicate that there is no next element in the list.

g. True False You may dereference a NULL pointer in C++ without any problems. h. True False A call to new to allocate new memory will always succeed. i. True False Doubly-linked lists facilitate both forward and backward traversal of the linked list. j. True False The purpose of using a dummy head node in a list is so that every node in the list has a valid predecessor.

  1. (10 pts, 2 pts each) Using the following linked list and node pointers p1, p2 and p3:

Draw a similar diagram for each of the parts a-e to show how this configuration changes when the given program segment is executed, or explain why an error occurs.

a. p2 = p2->next;

b. p3->next = p1;

c. p1->next->data = p3->next->data; Error, access violation

d. p3->next = p2->next; p2->next = p1->next;

  1. (10 pts) Given the following standard declarations for a Node of a linked list:

class Node { ElementType data; Node* next; } typedef Node* NodePointer; NodePointer first;

Assume that first is pointing to a linked list of such nodes.

a. (3 pts) Write a loop that will count the number of nodes in the list pointed to by first. The result (e.g. number of nodes in the list) should be stored in an integer.

int size = 0; NodePointer ptr = first; while (ptr != NULL) { size++; ptr = ptr->next; }

b. (3 pts) Write a loop that will find the nth^ node in a list. For example, using an integer variable int n , if n=5 you should find the fifth node (where the first nodes is 1, the second is 2, etc.) in the list. When you are done, a pointer to the nth^ node should end up in a NodePointer variable called ptr ;

int n = 5; NodePointer ptr = first; n--; while ((n != 0) && (ptr != NULL)) { ptr = ptr->next; n--; }

c. (4 pts) Assume that your code in part b works and you now have a pointer to the nth^ node in a list, pointed to by NodePointer ptr variable, and also a pointer to the first node in a list, pointed to by the NodePointer first variable. Write statements to exchange the nth and first nodes positions, so that the nth node becomes the first node and the first node becomes the nth node. Please change pointers in the list, do not simply swap the data members of the two nodes. It might be helpful to draw a diagram of a list to figure out the sequence of pointer assignments that need to be made. Also, recall that in order to swap two variables you need a temporary variable, declaring a temporary pointer or two will be necessary to hold information while you are swapping the nodes in the list.

skipped

Problem is that you need the prev of the node to be swapped with the first node. Without the previous node, this is a difficult problem to solve (you could find the previous, given the ptr by doing a search). Also we have to worry about if we are swapping the first node with the second node, which requires special handling. Possibly other special cases.