Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Veja se a ideia esta correta(programa ainda nao compila)

–2 votes
asked May 11, 2021 by paulo magalhaes (1,480 points)
#include <stdio.h>

#include <stdlib.h>

struct no {

    int dado;

    struct no *prox;

};

void imprimir(struct no *prim) {

    struct no *atual = prim;

    system("clear");

    printf("\nLista: ");

    while (atual != NULL) {

        printf("%d -> ", atual->dado);

        atual = atual->prox;

    }

    printf("NULL");

}

struct no *insere_inicio(struct no *prim) {

    int num;

    printf("\nInsira o elemento no inicio da lista: ");

    scanf("%d", &num);

    struct no *novo = (struct no *) malloc(sizeof(struct no *));

    novo->dado = num;

    novo->prox = prim;

    return novo;

}

struct no *insere_final(struct no *prim) {

    int num;

    printf("Insira o elemento no final da lista: ");

    scanf("%d", &num);

    struct no *novo = (struct no *) malloc(sizeof(struct no *));

    novo->dado = num;

    novo->prox = NULL;

    if (prim == NULL) return novo;

    struct no *ultimo = prim;

    while (ultimo->prox != NULL) {

        ultimo = ultimo->prox;

    }

    ultimo->prox = novo;

    return prim;

}

struct no *remove_fim(struct no *prim) {

    if (prim == NULL) {

        printf("Lista Vazia!");

        return NULL;

    }

    if (prim->prox == NULL) {

        printf("Removido do final!");

        free(prim);

        return NULL;

    }

    struct no *penultimo = prim;

    struct no *ultimo = prim->prox;

    while (ultimo->prox != NULL) {

        penultimo = ultimo;

        ultimo = ultimo->prox;

    }

    penultimo->prox = NULL;

    free(ultimo);

    printf("Removido do final!");

    return prim;

}

int main() {

    int op;

    struct no *prim = NULL;

    do {

        system("clear");

        printf("\n<1> - Inserir no inicio");

        printf("\n<2> - Inserir no final");

        printf("\n<3> - Remover no inicio");

        printf("\n<4> - Remover no final");

        printf("\n<5> - Imprimir");

        printf("\n<10> - Sair do programa\n\n");

        printf("Digite sua opcao: ");

        scanf("%d", &op);

        switch (op) {

            case 1:

                prim = insere_inicio(prim);

                break;

            case 2:

                prim = insere_final(prim);

                break;

            case 3:

                prim = remove_inicio(prim);

                break;

            case 4:

                prim = remove_fim(prim);

                break;

            case 5:

                imprimir(prim);

                break;

        }

    } while (op != 10);

    return 0;

}

1 Answer

0 votes
answered May 11, 2021 by Peter Minarik (86,130 points)
edited May 11, 2021 by Peter Minarik

I had a quick read of your code but since I do not speak Portuguese and C mixed with (abbreviated?) Portuguese is translated rather poorly to English via Google Translate it gave me a headache lol.

I think your code does what it should do.

I think it does more as you were only required to put new elements in the end and read from only the beginning (this is what a queue is, not sure why you added the extra functionality, when that wasn't in the original instructions).

If I were to make the code, instead of traversing the linked list to find the end, I'd just maintain two pointers: one that points to the beginning of the list (where you remove elements) and one that points to the end (where you add elements).

Your implementation works too, but the above suggested is more computation-effective (works quicker).

Below is my take on the solution:

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

typedef struct __Element
{
    int number;
    struct __Element * next;
} Element;

static Element * head = NULL;
static Element * tail = NULL;

void Enqueue(int number)
{
    Element * element = (Element *)malloc(sizeof(Element));
    element->number = number;
    element->next = NULL;
    if (!head)
        head = element;

    if (tail)
        tail->next = element;
 
    tail = element;
}

bool TryDequeue(int * number)
{
    if (!head)
        return false;

    Element * element = head;

    *number = head->number;
    head = head->next;
    if (!head)
        tail = head;

    free(element);
    return true;
}

static void PrintQueue()
{
    printf("Elements in the queue:\n");
    printf("----------------------\n");

    if (!head)
    {
        printf("Nothing is in the queue.\n");
        return;
    }

    for (Element * element = head; element; element = element->next)
        printf("%d\n", element->number);
}

static void UserEqnueue()
{
    printf("Enqueue:\n");
    printf("--------\n");
    printf("Number to add to the end of the queue: ");
    int number;
    scanf("%d", &number);
    Enqueue(number);
}

static void UserDequeue()
{
    printf("Dequeue:\n");
    printf("--------\n");
    int number;
    if (!TryDequeue(&number))
    {
        printf("Nothing is in the queue.\n");
        return;
    }
    
    printf("Number removed from the begining of queue: ");
    printf("%d\n", number);
}

int main()
{
    int choice;
    do
    {
        printf("QUEUE DEMO\n");
        printf("==========\n");
        printf("1. Enqueue element\n");
        printf("2. Dequeue element\n");
        printf("3. List all the element in the queue\n");
        printf("4. Exit\n");
        printf("? ");
        do
        {
            scanf("%d", &choice);
        } while (choice < 1 || choice > 4);
        printf("\n");
        switch (choice)
        {
            case 1: UserEqnueue(); break;
            case 2: UserDequeue(); break;
            case 3: PrintQueue(); break;
        }
        printf("\n");
    } while (choice != 4);
    
    int ignore;
    while (TryDequeue(&ignore)) ; // Notice: empty instruction in loop body. TryDequeue will keep removing items until nothing is left
    printf("Bye-bye");

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...