Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct queueNode {
- char *string;
- struct queueNode *next;
- };
- struct queue {
- struct queueNode *head;
- struct queueNode *tail;
- };
- char* stringInput() {
- int i = 0;
- int key = 0;
- char *string = nullptr;
- if (!(string = (char*)malloc(sizeof(char)))) {
- printf(" Memory error.\n");
- return NULL;
- }
- while ((key = getchar()) != '\n') {
- if (!(string = (char*)realloc(string, sizeof(char) * (i + 1)))) {
- printf(" Memory error.\n");
- return NULL;
- }
- string[i++] = key;
- }
- string[i] = '\0';
- return string;
- }
- int numberInput() {
- int number = 0;
- char buffer = 0;
- printf(" ");
- while ((!(scanf_s(" %d%c", &number, &buffer, 1))) || (buffer != '\n') || (number <= 0)) {
- printf(" You've entered the wrong value. Please, enter again: ");
- rewind(stdin);
- }
- return number;
- }
- struct queueNode *createQueue(struct queue *queuePoints) {
- struct queueNode *node = nullptr;
- if (!(node = (struct queueNode*)malloc(sizeof(struct queueNode)))) {
- return nullptr;
- }
- node->string = stringInput();
- node->next = NULL;
- if (queuePoints->tail == NULL) {
- queuePoints->head = queuePoints->tail = node;
- return node;
- }
- queuePoints->tail->next = node;
- queuePoints->tail = node;
- return node;
- }
- bool isSymmetric(struct queueNode *string) {
- int i = 0;
- int j = 0;
- int counter = 0;
- for (i = 0, j = strlen(string->string) - 1; i < (strlen(string->string) / 2); i++, j--) {
- if (string->string[i] == string->string[j]) {
- counter++;
- }
- }
- if (counter == 0) {
- return false;
- }
- if (counter == strlen(string->string) / 2) {
- return true;
- } else {
- return false;
- }
- }
- bool showQueue(struct queue *queuePoints) {
- struct queueNode *temp = queuePoints->head;
- bool flag = false;
- while (temp) {
- if (isSymmetric(temp)) {
- if (!flag) {
- printf(" These strings are symmetric:\n\n");
- flag = true;
- }
- printf(" %s\n\n", temp->string);
- }
- temp = temp->next;
- }
- if (!flag)
- return false;
- return true;
- }
- int main() {
- int switchOption = 0;
- char buffer = 0;
- int size = 0;
- struct queue *queuePoints = nullptr;
- if (!(queuePoints = (struct queue*)malloc(sizeof(struct queue)))) {
- return 0;
- }
- queuePoints->head = queuePoints->tail = NULL;
- printf(" Nice to meet you. Please, choose the required option:\n\n");
- do {
- printf(" 1. Queue elements input. \n 2. Queue elements output. \n 3. Exit. \n ");
- while (!(scanf_s("%d%c", &switchOption, &buffer, 1)) || switchOption < 1 || switchOption > 3 || buffer != '\n') {
- printf(" You've entered the wrong value. Please, enter again:\t");
- rewind(stdin);
- }
- system("CLS");
- switch (switchOption) {
- case 1:
- system("CLS");
- createQueue(queuePoints);
- break;
- case 2:
- system("CLS");
- if (!queuePoints->head) {
- printf(" Queue is empty.\n\n");
- break;
- }
- if (!showQueue(queuePoints)) {
- printf(" None are symmetric.\n\n");
- }
- break;
- case 3:
- system("CLS");
- free(queuePoints);
- printf("Thank you for using this programm!\nHave a good day!\n\n");
- system("exit");
- }
- } while (switchOption != 3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement