You are here:

C/Linked List

Advertisement


Question
Hi, i am having problems reversing elements in a linked list , for example i want to type in hello world and then the program respond dlrow olleh. my code is:

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


typedef struct linked_list {
char d;
struct linked_list *next;
} Element;

Element *insert(char h, Element **t){

Element *y = calloc( 1, sizeof(Element));
y->d = h;
y->next = *t;
*t = y;
return y;
}

int main(void)
{
Element *list = NULL;
char a;
printf("please type in sentence\n");

while((a = getchar()) != '.'){
list=insert (a, list);

}
for(; list ;list=list->next);
printf("%s\n", list);
}

i cant spot the error, according to my compiler i am passing incopatible types to insert in my main function , any help would be appreciated.

many thanks


Answer
Do you want help from me to remove warnings/errors.
Or do you need help in implementing the algorithm for reverse?

For removing the warning, this is what you have to do.
change:
list=insert (a, list);
to:
list=insert (a, &list);

Hope this helps.....

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.