Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <string.h>
- #define STR_MAX_LEN 20
- #define FRIENDS_NUMBER 3
- typedef struct personNode {
- char name[STR_MAX_LEN];
- int age;
- bool vip;
- struct personNode* next;
- } personNode;
- void freeLine(personNode* head);
- void search(personNode* head);
- void printLine(personNode* head);
- void reverse(personNode** head_ref);
- void removeFromLine(personNode** head);
- void insertAtBegin(personNode** head, personNode* newNode);
- void israeliInsertToLine(personNode** head, char names[][STR_MAX_LEN], personNode* newNode);
- int printSelectMenu();
- int lineLength(personNode* head);
- personNode* addToLine(personNode* head);
- personNode* addVIPToLine(personNode* head);
- int main()
- {
- int choice = 0;
- personNode* head = NULL;
- while (1)
- {
- choice = printSelectMenu();
- switch (choice)
- {
- case 1:
- printLine(head);
- break;
- case 2:
- head = addToLine(head);
- break;
- case 3:
- removeFromLine(&head);
- break;
- case 4:
- head = addVIPToLine(head);
- break;
- case 5:
- search(head);
- break;
- case 6:
- reverse(&head);
- break;
- case 7:
- printf("Goodbye!");
- freeLine(head);
- getchar();
- return 0;
- }
- }
- freeLine(head);
- getchar();
- return 0;
- }
- /*
- * Prints the menu and returns the user selection
- * input: -
- * output: user selection
- */
- int printSelectMenu()
- {
- int choice = 0;
- printf("Welcome to MagshiParty Line Management Software!\n"
- "Please enter your choice from the following options :\n"
- "1 - Print line\n"
- "2 - Add person to line\n"
- "3 - Remove person from line\n"
- "4 - VIP guest\n"
- "5 - Search in line\n"
- "6 - Reverse line\n"
- "7 - Exit\n");
- scanf("%d", &choice);
- getchar();
- return choice;
- }
- /* Prints the line
- * input: head of the line
- * output: -
- */
- void printLine(personNode* head)
- {
- personNode* current_node = head;
- printf("%d people in line:\n", lineLength(head));
- while (current_node != NULL)
- {
- printf("Name: %s, Age: %d\n", current_node->name, current_node->age);
- current_node = current_node->next;
- }
- }
- /* Counts the length of the line
- * input: the head of the line
- * output: the length of the line
- */
- int lineLength(personNode* head)
- {
- if (!head) // if list empty
- {
- return 0;
- }
- else
- {
- return 1 + lineLength(head->next);
- }
- }
- /* Inserts new node into the end of the main node
- * input: head of the main node, new node
- * output: -
- */
- void insertAtEnd(personNode** head, personNode* newNode)
- {
- if (!*head)
- {
- *head = newNode;
- }
- else
- {
- personNode* p = *head;
- while (p->next)
- {
- p = p->next;
- }
- p->next = newNode;
- }
- }
- /* Creates new node (type of - person)
- * input: -
- * output: -
- */
- personNode* createPersonNode()
- {
- personNode* newPersonNode = (personNode*)malloc(sizeof(personNode));
- strcpy(newPersonNode->name, "No name");
- newPersonNode->age = 0;
- newPersonNode->vip = false;
- newPersonNode->next = NULL;
- return newPersonNode;
- }
- /* Custom fgets() with built-in '\n' remove
- * input: string to input, string size
- * output: -
- */
- void myFgets(char str[], int n)
- {
- fgets(str, n, stdin);
- str[strcspn(str, "\n")] = 0;
- }
- /* asks user to input guest to add him into the line
- * input: head of the line
- * output: new head of the line
- */
- personNode* addToLine(personNode* head)
- {
- int i = 0;
- personNode* newNode = NULL;
- char friends[FRIENDS_NUMBER][STR_MAX_LEN] = { 0 };
- printf("Welcome guest!\n");
- newNode = createPersonNode();
- printf("Enter name: ");
- myFgets(newNode->name, STR_MAX_LEN);
- printf("Enter age: ");
- scanf("%d", &(newNode->age));
- getchar();
- printf("Enter name of 3 friends:\n");
- for (i = 0; i < FRIENDS_NUMBER; i++)
- {
- myFgets(friends[i], STR_MAX_LEN);
- }
- israeliInsertToLine(&head, friends, newNode);
- return head;
- }
- /* Inserts new node into the main node "Israeli line type"
- * input: head of the main node, names of the friends (2d-array), new node
- * output: -
- */
- void israeliInsertToLine(personNode** head, char names[][STR_MAX_LEN], personNode* newNode)
- {
- if (*head)
- {
- personNode* p = *head;
- if (0 == strcmp((*head)->name, names[0]) || 0 == strcmp((*head)->name, names[1]) || 0 == strcmp((*head)->name, names[2]))
- {
- newNode->next = (*head)->next;
- (*head)->next = newNode;
- return;
- }
- else
- {
- while (p->next &&
- (0 != strcmp(p->next->name, names[0]) || 0 != strcmp(p->next->name, names[1]) || 0 != strcmp(p->next->name, names[2])))
- {
- p = p->next;
- }
- if (p->next)
- {
- newNode->next = p->next;
- p->next = newNode;
- return;
- }
- }
- }
- insertAtEnd(head, newNode);
- }
- /* asks user to input guest to remove him from the line
- * input: head of the node
- * output: -
- */
- void removeFromLine(personNode** head)
- {
- char name[STR_MAX_LEN] = { 0 };
- printf("Enter name to remove:\n");
- myFgets(name, STR_MAX_LEN);
- personNode* p = *head;
- personNode* dNode = NULL;
- if (*head)
- {
- if (0 == strcmp((*head)->name, name))
- {
- *head = (*head)->next;
- free(p);
- printf("%s removed from line\n", name);
- return;
- }
- else
- {
- while (p->next &&
- 0 != strcmp(p->next->name, name))
- {
- p = p->next;
- }
- if (p->next)
- {
- dNode = p->next;
- p->next = dNode->next;
- free(dNode);
- printf("%s removed from line\n", name);
- return;
- }
- }
- }
- printf("%s not in line\n", name);
- }
- /* asks user to input guest to add him into the begin of the line (VIP client)
- * input: head of the main node
- * output: new head with the new client
- */
- personNode* addVIPToLine(personNode* head)
- {
- personNode* newNode = NULL;
- printf("VIP GUEST!\n");
- newNode = createPersonNode();
- printf("Enter name: ");
- myFgets(newNode->name, STR_MAX_LEN);
- printf("Enter age: ");
- scanf("%d", &(newNode->age));
- getchar();
- insertAtBegin(&head, newNode);
- return head;
- }
- /* Inserts the new node into the begin of the head node
- * input: head of the main node, new node
- * output: -
- */
- void insertAtBegin(personNode** head, personNode* newNode)
- {
- if (!*head)
- {
- *head = newNode;
- }
- else
- {
- newNode->next = *head;
- *head = newNode;
- }
- }
- /* Reverses the given node
- * input: node to reverse
- * output: -
- */
- void reverse(personNode** head)
- {
- personNode* prev = NULL;
- personNode* current = *head;
- personNode* next = NULL;
- while (current) {
- // Store next
- next = current->next;
- // Reverse current node's pointer
- current->next = prev;
- // Move pointers one position ahead.
- prev = current;
- current = next;
- }
- *head = prev;
- printf("Line reversed!\n");
- }
- /* asks user to input string to search in the given node (line)
- * input: head of the main node (line)
- * output: -
- */
- void search(personNode* head)
- {
- char searchFor[STR_MAX_LEN ] = { 0 };
- printf("Enter name to search:\n");
- myFgets(searchFor, STR_MAX_LEN);
- personNode* current = head; // Initialize current
- while (current != NULL)
- {
- if (strcmp(current->name, searchFor) == 0)
- {
- printf("%s in line\n", searchFor);
- }
- current = current->next;
- }
- printf("%s not in line\n", searchFor);
- }
- /* Frees the memory of the given node (line)
- * input: head of the main node to free his memory
- * output: -
- */
- void freeLine(personNode* head)
- {
- personNode* tmp;
- while (head)
- {
- tmp = head;
- head = head->next;
- free(tmp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement