Thanks for the help, though it doesnt seem to work.
The code below is my last update, I have no got it to print the last word first. Although the list is no longer linked together so I cannot figure out how to reverse the other words in the list.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct List{
char x;
struct List *next;
}list;
list *insertlist(char hd, list *a1){
list *a = calloc(1,sizeof(list));
a->x = hd;
a->next = a1;
return a;
free (a);
}
list *mainlist(){
list *b = NULL;
char i = 0;
b = insertlist('\n',b);
while (i != '.'){
i = getchar();
b = insertlist(i,b);
}
return b;
}
list *lookup(char c, list *head){
if (head == NULL)
return NULL;
while (head->x != c){
head = head -> next;
}
return head;
}
list *reversewords(list *a,list *b){
list *temp = NULL;
list *c = calloc(1,sizeof(list));
a = a->next;
a = lookup('\n', a);
b = b->next;
b = b->next;
b = lookup('\n', b);
while(a != b)
{
c = a->next;
a->next = temp;
temp = a;
a = c;
}
a = temp;
return a;
}
int main(){
list *d = NULL ;
printf("Please enter the phrase you wish to store in a list: \n");
d = mainlist();
d = reversewords(d,d);
if(d != 0) {
printf("\n");
while(d->next != 0) {
printf("%c", d->x);
d = d->next;
}
printf("%c", d->x);
}
return 0;
}