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


typedef struct linlist{
    char *payload;
    struct linlist *next;
}linlist_cell, *linlist_p ;

linlist_p anchor;
linlist_p *anchor_adr = &anchor;

void linlist_free_cell(linlist_p junk);
void linlist_free(linlist_p *junk);
linlist_p linlist_alloc_cell(char* payload);
linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell);
linlist_p linlist_extract_first(linlist_p *anchor);
linlist_p linlistFind(char* payload, linlist_p anchor);
linlist_p linlistExtract(linlist_p *anchor, linlist_p cell);


int main() {
    linlist_p i;

    char buffer[255];
    char reverse_buffer[255][255];
    FILE *in = stdin;
    printf("REVERSE INPUT\n");

    while (fgets(buffer, 255, in))
    {
	if(linlistFind(buffer,anchor_adr) != NULL)
	{
		printf("adding to list");
        	linlist_p newcell = linlist_alloc_cell(buffer);
        	linlist_insert_first(anchor_adr,newcell);
	}
    }
    printf("\nNORMAL OUTPUT\n");
    int j = 0;
    for (i = anchor; i != NULL; i = i->next)
    {
        fprintf(stdout, "%s", i->payload);
        strcpy(reverse_buffer[j], i->payload);
        j++;
    }
    printf("REVERSE OUTPUT\n");
    j--;
    for (j; j >= 0; j--)
    {
        fprintf(stdout, "%s", reverse_buffer[j]);
    }

    linlist_free(&anchor);
}


linlist_p linlistExtract(linlist_p *anchor, linlist_p cell)
{
	if(*anchor == cell)
	{
		anchor=cell->next;
		return cell;
	}else
	{
		
	}
}

linlist_p linlistFind(char* payload, linlist_p anchor)
{
	printf("find\n");
	linlist_p cell = anchor;
	while(cell->next || *anchor)
	{
	printf("in while");
		if(strcmp(payload,cell->payload))
		{	
		printf("found same");
			return cell;
		}
		else
		{
		printf("found nothing");
			cell = cell->next;
		}
	}
	return NULL;
}


void linlist_free_cell(linlist_p junk)
{
    free(junk);
}

void linlist_free(linlist_p *junk){

    if ((*junk)->next == NULL)
    {
        free(*junk);
        return;
    }
    else
    {
        linlist_free(&((*junk)->next));
        free(*junk);
    }
}

linlist_p linlist_alloc_cell(char* payload)
{
    linlist_p newcell = malloc(sizeof(linlist_p));
    char *value;
    newcell->payload = strdup(payload);
    newcell->next = NULL;
    return (newcell);

}
linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell)
{
    newcell->next = *anchor;
    *anchor = newcell;
    return(*anchor);

}
linlist_p linlist_extract_first(linlist_p *anchor)
{
    if (*anchor == NULL)
    {
        return NULL;
    }
    else
    {
        linlist_p *return_var;
        return_var = anchor;
        *anchor = (*anchor)->next;
        return (*return_var);
    }

}
