lublackbr0103 / LinkedList

0 stars 0 forks source link

Possible fix.. #1

Open allyssonf opened 3 years ago

allyssonf commented 3 years ago

Sugestão de código para resolver teu problema:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Include proper lib according to OS.
#ifdef _WIN32
    #include <conio.h>
#else
    #include <unistd.h>
#endif

#include <string.h>

#define ESC 0x1B

// Consider adding another member
// *previous to this linked list 
typedef struct NodeData {
    int data;
    struct NodeData* next;
} Node;

// Create custom clear screen to match OS.
void clrscr() {
    #ifdef _WIN32
        system("cls");
    #else
        system("clear");
    #endif
}

void insert_beginning(Node** head, int value) {
    Node* new_node = malloc(sizeof(Node));

    if (new_node == NULL) {
        exit(-1);
    } else {
        new_node->data = value;
        new_node->next = *head;
        *head = new_node;
    }
}

void insert_end(Node** head, int value) {
    Node* new_node = malloc(sizeof(Node));

    if (new_node == NULL) {
        exit(-2);
    } else {
        new_node->data = value;
        new_node->next = NULL;

        if (head == NULL) {
            *head = new_node;
        } else {
            // Consider adding an auxiliary pointer
            // to the last element on linked list.
            Node* aux = *head;

            while (aux->next != NULL) {
                aux = aux->next;
            }

            aux->next = new_node;
        }
    }
}

// Recursively clears list memory
void deallocate(Node* head) {
    if (head == NULL) {
        return;
    } else {
        deallocate(head->next);
        free(head);
    }
}

void display() {
    printf("------LINKED LIST------\n\n");
    printf("----------------------------------------------------------------------------------\n\n\n\n");
    printf("----------------------------------------------------------------------------------\n");
    clrscr();
}

int main() {
    Node* list = NULL;
    int num_input, option;
    // Limits number to 10 digits!
    char user_input[10];
    bool key;

    do {
        fflush(stdin);
        clrscr();
        printf("------LINKED LIST------\n\n");
        printf("ADD TO BEGINNING\tPress [1]\nADD TO THE END\t\tPress [2]\nFINISH\t\t\tPress [0]\n\n");
        printf("Enter your option: ");
        scanf("%d", &option);
        clrscr();

        key = true;

        if (option == 0) {
            clrscr();
            break;
        } else if (option == 1) {
            do {
                fflush(stdin);
                clrscr();
                printf("------LINKED LIST END------\n\n");
                printf("To exit\tPress [ESC]\n");
                printf("\nENTER THE VALUE OF ELEMENT: ");

                // Reads user input.
                fgets(user_input, sizeof(user_input), stdin);

                // Check if user pressed ESC.
                if (user_input[0] == ESC) {
                    key = false;
                } else {
                    // Try to convert input (char) to number
                    int result = sscanf(user_input, "%d", &num_input);

                    // No char converted, probably letters.
                    if (result <= 0) {
                        printf("Invalid input!\nPlease enter only numbers...\n");
                        sleep(2);
                    } else {
                        insert_beginning(&list, num_input);
                        printf("VALUE INSERTED!\n");
                        sleep(1);
                    }
                }
            } while (key);
        } else if (option == 2) {
            do {
                fflush(stdin);
                clrscr();
                printf("------LINKED LIST BEGINNING------\n\n");
                printf("\nTo exit\tPress [ESC]\n");
                printf("ENTER THE VALUE OF ELEMENT: ");

                // Reads user input.
                fgets(user_input, sizeof(user_input), stdin);

                // Check if user pressed ESC.
                if (user_input[0] == ESC) {
                    key = false;
                } else {
                    // Try to convert input (char) to number
                    int result = sscanf(user_input, "%d", &num_input);

                    if (result <= 0) {
                        printf("Invalid input!\nPlease enter only numbers...\n");
                        sleep(2);
                    } else {
                        insert_end(&list, num_input);
                        printf("VALUE INSERTED!\n");
                        sleep(1);
                    }
                }
            } while (key);
        } else {
            clrscr();
            printf("------LINKED LIST------\n\n");
            printf("Invalid input... Try again!\n");
            clrscr();
        }
    } while (true);

    Node* current_node = list;

    printf("Processing...");

    clrscr();
    display();
    printf("------LINKED LIST------\n\n");
    printf("----------------------------------------------------------------------------------\n");
    printf("Your linked list:\n\n");

    while (current_node != NULL) {
        printf("Data [%d] ", current_node->data);
        current_node = current_node->next;
    }

    printf("\n----------------------------------------------------------------------------------");
    printf("\nThanks for using Linked List app!\n");

    deallocate(list);

    list = NULL;

    return 0;
}
lublackbr0103 commented 3 years ago

Sugestão de código para resolver teu problema:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Include proper lib according to OS.
#ifdef _WIN32
    #include <conio.h>
#else
    #include <unistd.h>
#endif

#include <string.h>

#define ESC 0x1B

// Consider adding another member
// *previous to this linked list 
typedef struct NodeData {
    int data;
    struct NodeData* next;
} Node;

// Create custom clear screen to match OS.
void clrscr() {
    #ifdef _WIN32
        system("cls");
    #else
        system("clear");
    #endif
}

void insert_beginning(Node** head, int value) {
    Node* new_node = malloc(sizeof(Node));

    if (new_node == NULL) {
        exit(-1);
    } else {
        new_node->data = value;
        new_node->next = *head;
        *head = new_node;
    }
}

void insert_end(Node** head, int value) {
    Node* new_node = malloc(sizeof(Node));

    if (new_node == NULL) {
        exit(-2);
    } else {
        new_node->data = value;
        new_node->next = NULL;

        if (head == NULL) {
            *head = new_node;
        } else {
            // Consider adding an auxiliary pointer
            // to the last element on linked list.
            Node* aux = *head;

            while (aux->next != NULL) {
                aux = aux->next;
            }

            aux->next = new_node;
        }
    }
}

// Recursively clears list memory
void deallocate(Node* head) {
    if (head == NULL) {
        return;
    } else {
        deallocate(head->next);
        free(head);
    }
}

void display() {
    printf("------LINKED LIST------\n\n");
    printf("----------------------------------------------------------------------------------\n\n\n\n");
    printf("----------------------------------------------------------------------------------\n");
    clrscr();
}

int main() {
    Node* list = NULL;
    int num_input, option;
    // Limits number to 10 digits!
    char user_input[10];
    bool key;

    do {
        fflush(stdin);
        clrscr();
        printf("------LINKED LIST------\n\n");
        printf("ADD TO BEGINNING\tPress [1]\nADD TO THE END\t\tPress [2]\nFINISH\t\t\tPress [0]\n\n");
        printf("Enter your option: ");
        scanf("%d", &option);
        clrscr();

        key = true;

        if (option == 0) {
            clrscr();
            break;
        } else if (option == 1) {
            do {
                fflush(stdin);
                clrscr();
                printf("------LINKED LIST END------\n\n");
                printf("To exit\tPress [ESC]\n");
                printf("\nENTER THE VALUE OF ELEMENT: ");

                // Reads user input.
                fgets(user_input, sizeof(user_input), stdin);

                // Check if user pressed ESC.
                if (user_input[0] == ESC) {
                    key = false;
                } else {
                    // Try to convert input (char) to number
                    int result = sscanf(user_input, "%d", &num_input);

                    // No char converted, probably letters.
                    if (result <= 0) {
                        printf("Invalid input!\nPlease enter only numbers...\n");
                        sleep(2);
                    } else {
                        insert_beginning(&list, num_input);
                        printf("VALUE INSERTED!\n");
                        sleep(1);
                    }
                }
            } while (key);
        } else if (option == 2) {
            do {
                fflush(stdin);
                clrscr();
                printf("------LINKED LIST BEGINNING------\n\n");
                printf("\nTo exit\tPress [ESC]\n");
                printf("ENTER THE VALUE OF ELEMENT: ");

                // Reads user input.
                fgets(user_input, sizeof(user_input), stdin);

                // Check if user pressed ESC.
                if (user_input[0] == ESC) {
                    key = false;
                } else {
                    // Try to convert input (char) to number
                    int result = sscanf(user_input, "%d", &num_input);

                    if (result <= 0) {
                        printf("Invalid input!\nPlease enter only numbers...\n");
                        sleep(2);
                    } else {
                        insert_end(&list, num_input);
                        printf("VALUE INSERTED!\n");
                        sleep(1);
                    }
                }
            } while (key);
        } else {
            clrscr();
            printf("------LINKED LIST------\n\n");
            printf("Invalid input... Try again!\n");
            clrscr();
        }
    } while (true);

    Node* current_node = list;

    printf("Processing...");

    clrscr();
    display();
    printf("------LINKED LIST------\n\n");
    printf("----------------------------------------------------------------------------------\n");
    printf("Your linked list:\n\n");

    while (current_node != NULL) {
        printf("Data [%d] ", current_node->data);
        current_node = current_node->next;
    }

    printf("\n----------------------------------------------------------------------------------");
    printf("\nThanks for using Linked List app!\n");

    deallocate(list);

    list = NULL;

    return 0;
}