Library contents

C First assignment in a bounded seat register

Register the first customer for a seat without replacing an existing reservation.

A simple example

First assignment in a bounded seat register

  1. Start with A = 1
  2. Do this Insert A = 2 only if absent
  3. Result A stays 1
The key already exists, so the original value is kept.

Syntax

C syntax
if (seat.owner == NULL)
Run these examples in C

Save one example at a time as lesson.c. Use C17 and a Unix-style shell.

cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lesson

Keep the first reservation for a seat

Two students ask for the same seat at a school play. The desk enters the requests in order and needs the first confirmed student's name to survive the second request.

From the programstruct Seat seat = {"A1", NULL}
ResultMira
lesson.c
#include <stdio.h>
#include <string.h>

int main(void) {
    struct Seat { const char *number; const char *owner; };
    struct Seat seat = {"A1", NULL};
    const char *requests[] = {"Mira", "Sam"};
    for (size_t i = 0; i < 2; ++i) {
        if (strcmp(seat.number, "A1") == 0 && seat.owner == NULL) seat.owner = requests[i];
    }
    puts(seat.owner);
    return 0;
}
Output
Mira

Mira arrives before Sam and keeps the reservation for A1.

Advertisements

Reserve the kiln

Two potters ask to use the same afternoon slot. Two people claim the same slot in order. Keep the first recorded name and read it after both requests.

GivenFirst Lin · next Bo
Worked resultLin

Assign a library carrel

Two students request the only window desk. Two people claim the same slot in order. Keep the first recorded name and read it after both requests.

GivenFirst Dev · next Uma
Worked resultDev

What to watch for

This bounded record table uses NULL to mean available. It models the reservation task without requiring a hash-map library.

Documentation for First assignment in a bounded seat register