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;
}