vielen Dank für die Hilfe mit Stapel, jetzt habe ich Warteschlange selbst geschrieben .. Aber ich habe nur ein Problem. Wenn ich nur ein Element hinzufüge und es drucken möchte, druckt das Programm diese Nummer im Unendlichen. Was ist falsch an meiner Push/Print-Funktion?Warteschlange in C, schlecht Drucken
#include <stdio.h>
#include <stdlib.h>
typedef struct Element Element;
typedef struct Element
{
int value;
Element* next;
} Element;
Element* first = NULL;
Element* last = NULL;
void pop();
void push(int x);
void printqueue();
int main()
{
int x;
int warunek;
do
{
printf("\nMENU\n");
printf("1. Push[1]\n2. Pop[2]\n3. Print[3]\n0. Wyjdz[0]\n");
printf("Podaj warunek: ");
scanf("%d", &warunek);
switch(warunek)
{
case 1:
{
printf("Give a number: ");
scanf("%d", &x);
push(x);
}
break;
case 2: pop();
break;
case 3: printqueue();
break;
default: printf("Bad value.\n");
}
}
while(warunek != 0);
return 0;
}
void push(int x)
{
Element* pNewItem = (Element*)malloc(sizeof(Element));
pNewItem->value = x;
pNewItem->next = NULL;
if(first == NULL && last == NULL)
{
first = last = pNewItem;
}
last->next = pNewItem;
last = pNewItem;
}
void pop()
{
Element* pNewItem = first;
if(first == NULL)
printf("Queue is empty.\n");
else if(first == last)
first = last = NULL;
else
first = first->next;
free(pNewItem);
}
void printqueue()
{
Element* temp = first;
printf("\nContent of queue\n");
while(temp != NULL)
{
printf("%d\n", temp->value);
temp = temp->next;
}
printf("\n");
}
'zuletzt-> next = pNewItem; last = pNewItem; '->' else {last-> next = pNewItem; letzte = pNeueItem; } ' – BLUEPIXY
Wenn Sie" 0 "auswählen, wird es als" schlechter Wert "bezeichnet. – BLUEPIXY