COM1201 Spring 1997 Professor Fell QUIZ 1 Solution
Problem 1: Assume the following declarations:
struct Node {
	char data;
	Node* next;
};

Node* list;
And that the following linked list has been created:


For each of the following code segments start with this list and, draw a NEAT picture showing the resulting list(s), any unreferenced nodes, and where the pointers list and p point.

// a. Change List 1
	for (Node* p = list; p != NULL; p = p->next)
		if (p->data == 'C')
			if (p->next != NULL)
				p->next = p->next->next;

list
p = NULL

// b. Change List 2

	for (Node* p = list; p != NULL; p = p->next)
		if (p->next != NULL)
			p->next = p->next->next;

list
p = NULL

// c. Change List 3

	for (Node* p = list; p != NULL; p = p->next)
		if (p->data == 'A' || p->data == 'E')
			p->data = '*';				

list
p = NULL

Problem 2: Write a code segment that will change the data field of every node but the last one in list

list
to a 'Z'. (So list: FEDCBA becomes list ZZZZZA)
for (Node* p = list; p != NULL; p = p->next)
	if (p->next)
		p->data = 'Z';

Last Updated: April 6, 1997 9:06 pm by

Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1101/QUIZ/Q1/quiz1Solution.html