Algorithim help. Struct in Dbl. Linked List

Hello, first I am not asking for help with writing my code, but just thinking through the steps required to complete this task. I have a Doubly Linked List with Nodes that store movie data (showtimes, rating, maximum seating). I need to able to allow the user to select which showtime they want to attend, and then decrement the remaining seats in that showing.

Example:
A Dog's Purpose
1) 6:00PM
2) 9:00PM

Hidden Figures
3) 1:05PM
4) 4:10PM
5) 7:15PM
6) 10:20PM

The problem I am having is, I don't know how to tie the users option (1 or 2 or 6), to the corresponding show time. How do I decrement the "Nth" showtime when the user chooses "N". Just by asking the question I have a feeling the answer is stupid easy and I'm just overthinking it, but I've been staring at this problem for hours.

My program is made up of like 8 different files, so I will try to post enough info for the problem in question. Thank you for your time everyone.




1
2
3
4
5
6
7
8
9
10
11
// Movie File

typedef struct {
   char title[MAX_STRING];
   int lengthHour; // 24-hour time
   int lengthMinute;
   char rating[MAX_RATING];
   int numShowings;
   mytime_t time[MAX_SHOWINGS];
   int maxSeating[MAX_SHOWINGS];
} movie_t;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Node File
typedef struct node_t {
   movie_t movie;
   struct node_t* nextPtr;
   struct node_t* prevPtr;
} node_t;
node_t* initNode (movie_t);

node_t* initNode (movie_t data) {
   node_t* nodePtr = (node_t*) malloc (sizeof (node_t));
   nodePtr->movie = data;
   nodePtr->nextPtr = NULL;
   nodePtr->prevPtr = NULL;
   return nodePtr;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Function from my DblLinkedList.c that displays the relevant showtimes
void displayShowTimes (const dbl_linked_list_t* list) {
   node_t* curPtr = list->headPtr;
   int showingCounter = 0;
   if (curPtr != NULL) {
      while (curPtr != NULL) {
         printf ("\n%s\n", curPtr->movie.title);
         int i;
         for (i  = 0; i < curPtr->movie.numShowings; i++ ) {
            printf (" %2d) %s\n", showingCounter+1, convertTime (curPtr->movie.time[i]));
            showingCounter++;
            if (curPtr->movie.maxSeating[i] <= 0) {
               printf ("   Sold Out");
            }
         }
         curPtr = curPtr->nextPtr;
      }
   }
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// THIS IS MAIN

#include <stdio.h>                 // get, fget, scanf, printf
#include <stdlib.h>                // srand, rand, atof, NULL


#include "movie.h"
#include "mytime.h"
#include "dbllinkedlist.h"
#include "node.h"


int main (int argc, char* argv[]) {
   dbl_linked_list_t list;
   node_t* nodePtr;
   movie_t movie;
   FILE* inputFile;
   int i, menuChoice = 0;

   // Exit Program if there is no specified file.
   if (argc < 2) {
      printf ("Usage Error: No database specified. Contact your administrator.\n");
      exit (EXIT_FAILURE);
   }
   // Open the File Stream
   inputFile = fopen (argv[1], "rb");
   // Exit Program if specified file does not exist.
   if (inputFile == NULL) {
      printf ("Movie Database Error. Contact your administrator. File %s does not exist. \n", argv[1]);
      exit (EXIT_FAILURE);
   }
   // Get a character from the file stream, and put it back.
   // Used to set EOF flag if needed.
   int c = fgetc (inputFile);
   ungetc (c, inputFile);
   // Exit Program if the file is empty.
   if (feof(inputFile)) {
      printf ("Contact your administrator. File %s is empty.\n", argv[1]);
      exit (EXIT_FAILURE);
   }

   // Initialize Dbl Linked List
   createList (&list);

   // Read in the first movie
   fread (&movie, sizeof (movie_t), 1, inputFile);
   // Read data for 1 movie, initialize node with that data, and insert node into list.
   // Continue until EOF is reached.
   while (!feof (inputFile)) {
      // Initialize a node with data for 1 movie.
      nodePtr = initNode (movie);
      // Insert that node into the list.
      insertNode (&list, nodePtr);
      // Read data for the next movie.
      fread (&movie, sizeof (movie_t), 1, inputFile);
   }
   fclose (inputFile);

   while (menuChoice != 3) {
      // Displays Menu to the user and prompts for option.
      printf ("Welcome to Online Ticket System\n");
      printf ("\t1) Show all movies\n\t2) Search for a movie\n\t3) Quit\nOption: ");
      scanf ("%d", &menuChoice);

      // Show all movies
      if (menuChoice == 1) {
         showAllMovies (&list);
      }
      // Search by movie title and display showings.
      else if (menuChoice == 2) {
         displayShowTimes (&list);
      }
      // Exit the Program
      else if (menuChoice == 3) {
         printf ("Thank you for using the program. Goodbye now.\n");
         exit (EXIT_SUCCESS);
      }
      // If user does not enter 1, 2, or 3, displays Error.
      else {
         printf ("Menu Error. Invalid choice; please try again\n");
      }
   }

   // Deallocates memory for all nodes linked in the list
   deleteList (&list);
   return 0;
}
Topic archived. No new replies allowed.