Created
October 18, 2017 20:34
-
-
Save azzarello/4e92f9a82e4e6476e92a5f7ece4c194c to your computer and use it in GitHub Desktop.
restaurantLinkedList created by thewizardofazz - https://repl.it/Mqxv/1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Connor Azzarello | |
// COEN 11 - Lab 5 | |
// Wednesday Lab | |
#include <stdio.h> | |
#include <string.h> | |
void insert(); | |
void search(); | |
void show(); | |
struct node { | |
char name[21]; | |
int size; | |
struct node *next; | |
}; | |
struct node * head; | |
struct node * tail; | |
int main() { //asks the user for a mode selection, converts the string to a corresponding number to be used in the switch statement | |
int c = -1; | |
while (c != 3) { | |
char s[21]; | |
printf("Enter mode command: \n"); | |
scanf("%s", & s); | |
if (!strcmp(s, "insert")) { | |
c = 0; | |
} else if (!strcmp(s, "search")) { | |
c = 1; | |
} else if (!strcmp(s, "list")) { | |
c = 2; | |
} else if (!strcmp(s, "quit")) { | |
c = 3; | |
} else { | |
printf("Invalid mode, try again\n"); | |
} | |
switch (c) { | |
case 0: | |
insert(); | |
break; | |
case 1: | |
search(); | |
break; | |
case 2: | |
show(); | |
break; | |
case 3: | |
printf("Thank you for using this program!"); | |
break; | |
} | |
} | |
return 0; | |
} | |
void insert() { // prompts the guest for their name and table size, then either creates a head node or continues to the end of the list and adds a new tail node | |
struct node * ptr = head; | |
struct node * r = head; // r serves as a placeholder pointer | |
char name[21]; | |
int size; | |
int error = 0; | |
printf("What is your name? \n"); | |
scanf("%s", name); | |
while (!(ptr==NULL)) { | |
if (!strcmp(ptr->name, name)) { | |
printf("Duplicate name entered, try again\n"); | |
error = 1; | |
break; | |
} | |
r = ptr; | |
ptr = ptr->next; | |
} | |
ptr = r; | |
if (error == 0) { | |
printf("What is your group size? \n"); | |
scanf("%d", &size); | |
if (ptr == NULL) { // when head must be created | |
head = (struct node * ) malloc(sizeof(struct node)); | |
strcpy(head->name, name); | |
head->size = size; | |
tail = head; | |
tail->next = NULL; | |
} else { // when head already exists | |
ptr = (struct node * ) malloc(sizeof(struct node)); | |
strcpy(ptr->name, name); | |
ptr->size = size; | |
r->next = ptr; | |
tail = ptr; | |
tail->next = NULL; | |
} | |
} | |
} | |
void search() { //traverses the oldest to newest list to find a reservation with a size less than or equal to the size of the table that can be seated. once it is found, it is printed and that entry is removed | |
struct node * ptr = head; | |
struct node * r = head; | |
int size; | |
printf("How big is the table that may be seated?\n"); | |
scanf("%d", &size); | |
while (!(ptr == NULL)) { | |
if(ptr->size <= size) { | |
printf("%s, %d\n", ptr->name, ptr->size); | |
if(!ptr == head) { | |
r->next = ptr->next; | |
} else { | |
head = ptr->next; | |
r = head; | |
} | |
break; | |
} | |
r = ptr; | |
ptr = ptr->next; | |
} | |
} | |
void show() { // traverses the list printing the name and size of every reservation left in the system | |
struct node * ptr = head; | |
char name[21]; | |
int size; | |
if (ptr == NULL) { | |
printf("There are no waiting list entries, try another mode\n"); | |
} else { | |
while (!ptr == NULL) { | |
printf("%s, %d\n", ptr->name, ptr->size); | |
ptr = ptr -> next; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment