CS211 - Algorithms and Data Structures II
Tutorial 5

T Naughton, CS NUIM
Back to CS211 home


This tutorial reviews some fundamentals on classes and linked lists.
Problems T5.1, 3.2, 5.A, 5.1

Problem T5.1 Create a class that maintains a description of your favourite pet.

Problem 3.2 Below is a diagram of a linked list stored in memory. What would happen to the data structure if code to 'Insert the string "Betty" after "Boo" in the list' is executed. Answer this question by drawing diagrams describing each step (yes, like a cartoon!) and clearly explain each step. (For example, the first step is to create a temporary pointer and make it point to the same thing as the head of the list. The second step is to compare what the temporary pointer is pointing to with the string "Boo". If they are the same do.... if not do....)

Problem 3.2
Figure for Problem 3.2

Problem 5.A A computer program is required that reads positive integers, one per line, from the keyboard into a list. The user indicates that they are finished by entering any negative integer (the negative integer itself is not stored). The computer program then prints out the list of numbers, on a single line, separated with blank spaces. Write this program using classes and a linked list.

Problem 5.1 Update the solution to Problem 5.A by adding the following functionality:

  • return true/false if a given integer is in the list,
  • add a node to the end of the list,
  • delete the entire list,
  • count the number of elements in the list.
    You can test the functions by calling them in main().