Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element
[Skip Breadcrumb Navigation]
Home  arrow Student Resources  arrow Quizzes  arrow Chapter 17

Chapter 17
Linked Data Structures

This activity contains 10 questions.

Question 1
1 What will the following program output?

class IntNode
{
public:
 int num;
 IntNode *next;
 IntNode();
 IntNode(int n);
};

IntNode::IntNode()
{
	next = NULL;
	num = -1;
}

IntNode::IntNode(int n)
{
	next = NULL;
	num = n;
}


// Main class
int main()
{	
	IntNode *head = NULL, *temp = NULL;

	// Construct linked list
	for (int i=0; i<5; i++) {
		temp = new IntNode(i);
		if (head==NULL) {
			head = temp;
		}
		else {
			temp->next = head;;
			head = temp;
		}
	}
	// Output list
	temp = head;
	while (temp->next != NULL) {
		cout << temp->num << " ";
		temp = temp->next;
	}
	cout << endl;
	return 0;
}
 
End of Question 1


Question 2
2 Which of the following routines will correctly delete the linked list that is created?

class IntNode
{
public:
 int num;
 IntNode *next;
 IntNode();
 IntNode(int n);
};

IntNode::IntNode()
{
	next = NULL;
	num = -1;
}

IntNode::IntNode(int n)
{
	next = NULL;
	num = n;
}

// Main class
int main()
{	
	IntNode *head = NULL, *temp = NULL;

	// Construct linked list
	for (int i=0; i<5; i++) {
		temp = new IntNode(i);
		if (head==NULL) {
			head = temp;
		}
		else {
			temp->next = head;;
			head = temp;
		}
	}
	// Output list
	temp = head;
	while (temp != NULL) {
		cout << temp->num << " ";
		temp = temp->next;
	}
	cout << endl;

	// CODE TO DELETE LIST GOES HERE

	return 0;
}
 
End of Question 2


Question 3
3 The following program constructs a list of numbers as follows:

0 => 1 => 2 => 3 => 4 => NULL

Which of the following code snippets inserts a node with the number 101 between the node with 2 and the node with 3? i.e. the list should look like:

0 => 1 => 2 => 101 => 3 => 4 => NULL


class IntNode
{
public:
 int num;
 IntNode *next;
 IntNode();
 IntNode(int n);
};

IntNode::IntNode()
{
	next = NULL;
	num = -1;
}

IntNode::IntNode(int n)
{
	next = NULL;
	num = n;
}

// Main class
int main()
{	
	IntNode *head = NULL, *tail = NULL;
	IntNode *temp = NULL;

	// Construct linked list
	for (int i=0; i<5; i++) {
		temp = new IntNode(i);
		if (head==NULL) {
			head = temp;
			tail = temp;
		}
		else {
			tail->next = temp;
			tail = temp;
		}
	}

	// CODE TO INSERT GOES HERE
	
	return 0;
}
 
End of Question 3


Question 4
4 The following program constructs a list of numbers as follows:

0 => 1 => 2 => 3 => 4 => NULL

Which of the following code snippets deletes the node with the number 3 from the list? i.e. the list should look like:

0 => 1 => 2 => 4 => NULL


class IntNode
{
public:
 int num;
 IntNode *next;
 IntNode();
 IntNode(int n);
};

IntNode::IntNode()
{
	next = NULL;
	num = -1;
}

IntNode::IntNode(int n)
{
	next = NULL;
	num = n;
}

// Main class
int main()
{	
	IntNode *head = NULL, *tail = NULL;
	IntNode *temp = NULL;

	// Construct linked list
	for (int i=0; i<5; i++) {
		temp = new IntNode(i);
		if (head==NULL) {
			head = temp;
			tail = temp;
		}
		else {
			tail->next = temp;
			tail = temp;
		}
	}

	// CODE TO DELETE GOES HERE
	
	return 0;
}
 
End of Question 4


Question 5
5 What is the output of the following program?

class IntNode
{
public:
 int num;
 IntNode *next;
 IntNode();
 IntNode(int n);
};

IntNode::IntNode()
{
	next = NULL;
	num = -1;
}

IntNode::IntNode(int n)
{
	next = NULL;
	num = n;
}


bool DeleteNode(IntNode *head, int target)
{
	IntNode *temp = head, *previous = NULL;

	while (temp!=NULL) {
		if (temp->num == target) {
			if (previous == NULL) {
			  head = head->next;
			  delete temp;
			  return true;
			}
			else {
			  previous->next = temp->next;
			  delete temp;
			  return true;
			}
		}
		else {
			previous = temp;
			temp = temp->next;
		}
	}
	return false;
}

void OutputList(IntNode *temp)
{
	while (temp != NULL) {
		cout << temp->num << " ";
		temp = temp->next;
	}
	cout << endl;
}

// Main class
int main()
{	
	IntNode *head = NULL, *tail = NULL;
	IntNode *temp = NULL;

	// Construct linked list
	for (int i=0; i<5; i++) {
		temp = new IntNode(i);
		if (head==NULL) {
			head = temp;
			tail = temp;
		}
		else {
			tail->next = temp;
			tail = temp;
		}
	}

	DeleteNode(head, 3);
	OutputList(head);
	DeleteNode(head, 0);
	OutputList(head);

	// Destroy list
	while (head != NULL) {
		temp = head;
		head = head->next;
		delete temp;
	}
	
	return 0;
}
 
End of Question 5


Question 6
6 The following program constructs a circular linked list. That is, the next pointer at the tail of the list refers back to the head of the list. Which of the following code snippets correctly outputs the contents of the circular linked list once?

class IntNode
{
public:
 int num;
 IntNode *next;
 IntNode();
 IntNode(int n);
};

IntNode::IntNode()
{
	next = NULL;
	num = -1;
}

IntNode::IntNode(int n)
{
	next = NULL;
	num = n;
}

// Main class
int main()
{	
	IntNode *head = NULL, *tail = NULL;
	IntNode *temp = NULL;

	// Construct linked list
	for (int i=0; i<5; i++) {
		temp = new IntNode(i);		
		if (head==NULL) {
			head = temp;
			tail = temp;	
			temp->next = head;
		}
		else {
			tail->next = temp;
			tail = temp;
			temp->next = head;
		}
	}

	// CODE TO OUTPUT GOES HERE

	return 0;
}
 
End of Question 6


Question 7
7 The following code constructs a doubly linked list of integers. The next field points to the next node in the list, while the previous field points to the previous node. This code constructs the list:

NULL <== 0 <==> 1 <==> 2 <==> 3 <==> 4 ==> NULL

Which of the following DeleteNode routines correctly searches for the node containing the target value and deletes it from the list?


class IntNode
{
public:
 int num;
 IntNode *next, *previous;
 IntNode();
 IntNode(int n);
};

IntNode::IntNode()
{
	next = NULL;
	previous = NULL;
	num = -1;
}

IntNode::IntNode(int n)
{
	next = NULL;
	previous = NULL;
	num = n;
}

// Main class
int main()
{	
	IntNode *head = NULL, *tail = NULL;
	IntNode *temp = NULL;

	// Construct linked list
	for (int i=0; i<5; i++) {
		temp = new IntNode(i);		
		if (head==NULL) {
			head = temp;
			temp->previous = tail;
			tail = temp;
		}
		else {
			tail->next = temp;
			temp->previous = tail;
			tail = temp;
		}
	}

	// Sample DeleteNode calls
	DeleteNode(head, 3);
	DeleteNode(head, 0);

	// Destroy list
	while (head != NULL) {
		temp = head;
		head = head->next;
		delete temp;
	}
	return 0;
}
 
End of Question 7


Question 8
8 The following code constructs a doubly linked list of integers. The next field points to the next node in the list, while the previous field points to the previous node. This code constructs the list:

NULL <== 0 <==> 1 <==> 2 <==> 3 <==> 4 ==> NULL

Which of the following code snippets will insert the value 101 into the list between the nodes with 2 and 3? e.g. the list should contain:

NULL <== 0 <==> 1 <==> 2 <==> 101 <==> 3 <==> 4 ==> NULL


class IntNode
{
public:
 int num;
 IntNode *next, *previous;
 IntNode();
 IntNode(int n);
};

IntNode::IntNode()
{
	next = NULL;
	previous = NULL;
	num = -1;
}

IntNode::IntNode(int n)
{
	next = NULL;
	previous = NULL;
	num = n;
}

// Main class
int main()
{	
	IntNode *head = NULL, *tail = NULL;
	IntNode *temp = NULL;

	// Construct linked list
	for (int i=0; i<5; i++) {
		temp = new IntNode(i);		
		if (head==NULL) {
			head = temp;
			temp->previous = tail;
			tail = temp;
		}
		else {
			tail->next = temp;
			temp->previous = tail;
			tail = temp;
		}
	}

	// CODE TO INSERT WOULD GO HERE

	// Destroy list
	while (head != NULL) {
		temp = head;
		head = head->next;
		delete temp;
	}
	return 0;
}
 
End of Question 8


Question 9
9 What is a common purpose of an iterator?
 
End of Question 9


Question 10
10 What is the output of the following code? It constructs a binary tree of numbers.

class Node
{
 public:
   int num;
   Node *left, *right;
   Node();
   Node(int n);
};

Node::Node()
{
  left=NULL;
  right=NULL;
  num=0;
}

Node::Node(int n)
{
  left=NULL;
  right=NULL;
  num=n;
}

void AddToTree(Node *root, Node *newNode)
{
  if (root->num > newNode->num) {
          if (root->left != NULL) AddToTree(root->left, newNode);
          else root->left = newNode;
  }
  else {
          if (root->right != NULL) AddToTree(root->right, newNode);
          else root->right= newNode;
  }
}


void PrintTree(Node *root)
{
	if (root!=NULL) {
		cout << root->num << " ";
		PrintTree(root->left);
		PrintTree(root->right);
	}
}

void DeleteTree(Node *root)
{
	if (root != NULL) {
		DeleteTree(root->left);
		DeleteTree(root->right);
		delete root;
	}
}

void main()
{
  Node *root = new Node(10);  
  AddToTree(root, new Node(5));
  AddToTree(root, new Node(15));  
  AddToTree(root, new Node(20));
  AddToTree(root, new Node(7));
  AddToTree(root, new Node(1));
  PrintTree(root);
  DeleteTree(root);
}
 
End of Question 10






Answer choices in this exercise appear in a different order each time the page is loaded.




Copyright © 1995-2008, Pearson Education, Inc., publishing as Pearson Addison Wesley
Legal and Privacy Terms
Pearson Education

[Return to the Top of this Page]