Skip to content
Saarrthi Rana

School project / Winter 2025

Data structures labs

Seven weekly labs in C++ for my Data Structures course.

Role
Solo
Team
Data Structures course
When
Winter 2025
C++ListsQueuesTrees
The start of the Lab 2 circle puzzle code on a dark code card
What it is

Each lab was about picking the right container for a problem and using it properly.

Each week I solved a problem using one of the containers we were given.

What I did
  1. Merged two linked lists into one.
  2. Solved the Josephus puzzle on a circular list, where every second person in a circle is out until one is left.
  3. Worked out how long a checkout line takes when there's more than one till.
  4. Used a hash table to check whether a list has the same number twice close together.
  5. Changed a search tree so each value becomes the total of all the bigger values.
  6. Made a class that hands back the same object every time you ask with the same key.
  7. Wrote a mouse handler that prints where you click and drag.
How it works

The circle puzzle

I walk around the list, wrapping back to the start at the end, and take out every second person until one is left.

The checkout line

Each customer goes to the till that will be free first. The answer is how long the busiest till takes.

Code

Code I wrote

Short parts of my own code from this project. I left out anything the course gave us.

The circle puzzle (Lab 2)

Main.cpp, C++

My part of the lab. The template code around it is left out.
    if (list.empty()) return;

    auto it = list.begin(); // Start

    while (list.size() > 1) {
        // Move iterator skipPos1 timeto find the next person to eliminate
        for (int i = 0; i < skipPos - 1; ++i) {
            ++it;
            if (it == list.end()) it = list.begin(); // Move in circle
        }

        // Move up to the person to be eliminated
        auto toErase = it;
        ++it;
        if (it == list.end()) it = list.begin(); // valid iterator

        // Print eliminated player
        std::cout << *toErase << " was killed." << std::endl;

        // Erase eliminated player
        list.erase(toErase);
    }

    // Print the last remaining person
    std::cout << "Last person left standing is " << *list.begin() << "." << std::endl;

Totals of the bigger values (Lab 5)

Main.cpp, C++

Visits the biggest values first and keeps a running total.
	if (node == nullptr)
		return;

	//right subtree first 
	greaterSumHelper(node->getRight(), sum);


	// Update sum with the current node value

	int originalValue = node->getValue();
	sum += originalValue;
	Value newValue = sum - originalValue;

	node->setValue(newValue);

	//Process left subtree
	greaterSumHelper(node->getLeft(), sum);
Output

Program output

Console output from running the build. I cut long runs of the same line short.

// Lab 1, merge two linked lists
Populating list lhs...
// (adds 0 to 9)
Populating list rhs...
// (adds 9 to 0)
Merging lists...
0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0

// Lab 2, the circle puzzle (8 people, every 2nd one out)
Person 2 was killed.
Person 4 was killed.
Person 6 was killed.
Person 8 was killed.
Person 3 was killed.
Person 7 was killed.
Person 5 was killed.
Last person left standing is Person 1.

// Lab 3, checkout line (customers {10, 2, 3, 3}, 2 tills)
Total is 10

// Lab 4, the same number twice within 3 places ({1, 2, 3, 4, 4})
Hash table has duplicates

// Lab 5, totals of the bigger values in a search tree
In-Order BST Traversal: 1 2 3 6 7
greaterSumTree(root, 0) rearranges to 18 16 13 7 0

// Lab 6, the same key gives back the same object
Multiton Constructor
Multiton Constructor
c1 is stored in memory address 000002798E71B690
c2 is stored in memory address 000002798E71BC30
c3 is stored in memory address 000002798E71B690

The containers came from our instructor's templates. I wrote the part of each lab that solves the problem.

Next school project

Donut cafe in 3ds Max

A cafe scene and a snowman for my 3D modelling course.