Linked Lists Worksheet #2

Assume the node declaration shown below throughout this worksheet.

	struct node
	{
	     someType data;
	     node* next;
	     node (someType D, node *N)
		: data (D),
		  next (N)
	     { } 	// all field initialized in initializer list 
	};	// notice semi-colon

1. Write a function that will split a linked list into two linked lists, so that successive nodes go to different lists, of no specific order (the first, third, and all odd-numbered nodes to the first list, and the second,fourth, and all even-numbered nodes to the second).

	void Split (node* &list, node* &odds, node* &evens)
	// Pre  - list is not empty 
	// Post - Odds contains the first, third, fifth, etc. elements of list.  
	//        Evens contains the second, fourth, sixth, etc. elements of list.  
	//	  List is empty 
	{

2. Write a function with prototype void reverse(node* &head); that reverses the order of the elements of a linked list pointed to by the parameter of the function.

3. Write a function that rotates the elements of a list with n elements so that when the rotation is completed, the last element becomes the first and the first, the second, the second, the third, etc.... Your function should have list and n as a parameters.

4. Assuming that you have a linked list with a pointer to the head of the list, write a function called copy which takes two pointers to lists, A and B as arguments. copy should make B a copy of A and return A unchanged.


Continue to:  Unit 6  / Prev  / Next