Witam,
Mam za zadanie napisanie programu odwracającego kolejność łańcuchów tekstu podawanych z wejścia. Program ma zapamiętywać wprowadzane dane wykorzystując stos implementowany jako listę.
Oprócz tego mam zadane, że do przechowywania danych na liście należy wykorzystać strukturę zawartą zaraz na początku mojego kodu, a do czytania danych z wejścia należy użyć funkcji fgets
.
Udało mi się zrobić coś takiego. Nie wiem, jak wyświetlić wynik. Proszę o pomoc, o wskazanie, co robię nie tak...
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Element
{
char *Text;
struct Element *next;
};
struct Element *head = NULL;
int push(char *txt)
{
struct Element *nowyElement = (struct Element *)malloc(sizeof(struct Element));
if(nowyElement == NULL)return 0;
else
{
nowyElement->Text = txt;
nowyElement->next = head;
head = nowyElement;
return 1;
}
} /* push() */
void clear(void)
{
struct Element *temp;
while( head != NULL )
{
temp = head;
head = head->next;
free(temp->Text);
temp->Text = NULL;
free(temp);
}
}
int isEmpty(void)
{
if (head == NULL)return 1;
else return 0;
} /* isEmpty() */
int pop(char *txt)
{
if (isEmpty()) return 0;
else
{
struct Element *temp = head;
txt = head->Text;
head = head->next;
free(temp);
return 1;
}
} /* pop() */
int main()
{
char buf[20];
printf("Wpisz dane, a ja wypisze je w odwrotnej kolejnosci.\n");
while(fgets(buf, (int)sizeof(buf), stdin), buf[1] != '\0')
{
push(buf);
};
printf("A oto i wpisane dane w odwrotnej kolejnosci:\n");
pop(buf);
clear();
return 0;
}