Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Building a Theater Inventory System with Hash Tables in C

Learn how to implement a hash table with separate chaining in C by building a theater inventory management system. This tutorial covers hash functions, linked list operations, and complexity tracking.

hash table C separate chaining theater inventory system COP 3502C assignment 6 hash function implementation C programming tutorial linked list hash table complexity tracking buy sell inventory C hash table project data structures C UCF computer science movie theater concession hash table example C memory management hash table performance

Introduction to Hash Tables in C

Hash tables are a fundamental data structure in computer science, offering average O(1) time complexity for insertions, deletions, and lookups. In this tutorial, we'll build a theater inventory system that uses a hash table with separate chaining to manage items like popcorn, soda, and candy. This assignment is typical for courses like COP 3502C at UCF and provides hands-on practice with dynamic memory allocation, linked lists, and hash functions.

Imagine you're managing a movie theater's concession stand. You need to track how many bags of popcorn you have, update prices quickly, and handle sales. A hash table lets you map item names (strings) to inventory records efficiently. We'll implement this in C, using a hash function provided in the assignment specification.

Understanding the Problem

The theater starts with $100,000 cash. Commands include buying supplies (type 1), selling items (type 2), and changing prices (type 3). For each buy or sell command, we output the item name, remaining quantity, and cash after the transaction. At the end, we print the final cash and the total complexity of all operations. Complexity is defined as the position of the item in the linked list (1-based) if found, or the length of the list plus one if not found (for buys).

This problem is a great way to understand hash table performance metrics. By summing complexities, you can evaluate how well your hash function distributes keys.

Hash Function Implementation

The assignment provides a specific hash function. Here's how it looks:

int hashfunction(char* word, int size) {
    int len = strlen(word);
    int res = 0;
    for (int i = 0; i < len; i++) {
        res = (res * 37 + word[i]) % size;
    }
    return res;
}

This function computes a hash by iterating over each character, multiplying the current result by 37, adding the ASCII value of the character, and taking modulo size (the number of buckets). The choice of 37 is common because it's a prime number that helps reduce collisions.

Data Structures

We'll define a node for the linked list (separate chaining) and a hash table structure:

typedef struct node {
    char name[25];
    int quantity;
    int price;
    struct node* next;
} node;

typedef struct hashTable {
    node** buckets;
    int size;
} hashTable;

Each bucket is a pointer to the head of a linked list. The hash table holds an array of these pointers.

Operations: Buy, Sell, Change Price

Buy Supplies

When buying, we compute the hash index, then search the linked list for the item. If found, we update the quantity and total cost. If not found, we create a new node and insert it at the beginning of the list (or any position, but head insertion is O(1)). The complexity for a buy is: if found, it's the position k (1-based); if not found, it's the length of the list before insertion + 1.

Sell Items

For a sell command, we locate the item in the hash table. It's guaranteed to exist. We sell up to the requested quantity, but if stock is insufficient, we sell all available. We update quantity and cash. Complexity is the position of the node in its linked list.

Change Price

Change price simply updates the price field of the node. No output is printed for this command, but complexity is still computed (position of the node).

Complexity Tracking

We maintain a global variable total_complexity. For each command, we calculate the complexity as described and add it. This allows us to output the sum at the end.

Example Walkthrough

Let's trace the sample input. The first command is buy popcorn 1000 3000. The hash function maps "popcorn" to some bucket. The list is empty, so complexity = 0 + 1 = 1. We insert a node with quantity 1000, price per unit = 3000/1000 = 3 (though price is stored as total purchase price? Actually, the assignment says price is the total purchase price for buy, but for sell and change_price, price is per unit. We need to store the selling price per unit. In the sample, after buying, we change_price to 6, so the stored price should be per unit. So for buy, we store the total purchase price? Wait, the sample output shows after buy popcorn 1000 3000, cash becomes 97000 (100000 - 3000). Then change_price popcorn 6 sets selling price to 6. So we need to store selling price separately. Let's store both cost (total purchase price) and selling price per unit. But the assignment only mentions price for sell and change_price. For simplicity, we'll store selling price per unit, initialized to 0, and update with change_price. For buy, we just subtract cost from cash and increase quantity. That matches sample.

After buying popcorn, we output: popcorn 1000 97000. Then buy soda 2000 1000 -> cash 96000, output soda 2000 96000. Then change_price popcorn 6 and soda 5 (no output). Then sell popcorn 50: we have 1000, sell 50, left 950, cash += 50*6 = 300, cash becomes 96300, output popcorn 950 96300. Then sell soda 100: cash += 100*5 = 500, cash 96800, output soda 1900 96800. Then change_price popcorn 8 (no output). Then sell popcorn 90: cash += 90*8 = 720, cash 97520, output popcorn 860 97520. Then sell soda 1899: we have 1900, sell 1899, left 1, cash += 1899*5 = 9495, cash becomes 107015, output soda 1 107015. Then buy soda 10 3: cost 3, cash 107012, output soda 11 107012. Final cash 107012, total complexity 10 (as given).

Implementation Tips

  • Use dynamic memory allocation for nodes and the hash table array.
  • Remember to free memory to avoid leaks, though for a single-run program it's not critical.
  • Handle string comparisons with strcmp.
  • For complexity calculation, traverse the linked list and count positions.
  • Test with sample input to verify output.

Connecting to Real-World Trends

Hash tables are everywhere. For instance, in the 2026 FIFA World Cup, a hash table could map player names to their statistics for real-time updates. Similarly, in AI chatbots like ChatGPT, hash tables help store conversation contexts quickly. In finance, stock tickers are hashed to retrieve price data. Understanding hash tables opens doors to building efficient systems.

Conclusion

By implementing this theater inventory system, you've practiced hash table construction, separate chaining, and complexity analysis. These skills are essential for coding interviews and real-world software development. Keep experimenting with different hash functions and load factors to see how performance changes.