- #1
Firestrider
- 104
- 0
Can someone help me on how exactly to do this? I'm trying to read an integer string and each "digit" in the string is put at the front of the linked list (i.e. reverse order). When I print it out I want it to reverse again. I know I'm not implementing it right because when I run it the program will output a lot of numbers.
Code:
struct integer
{
int digit;
struct integer* next;
};
struct integer* read_integer(char* stringInt)
{
int index;
struct integer* current = NULL;
current = (struct integer*)(malloc(sizeof(struct integer)));
for (index = 0; index < strlen(stringInt); index++)
{
current->digit = stringInt[index] - '0';
current->next = (struct integer*)(malloc(sizeof(struct integer)));
current = current->next;
}
return current;
}
void print(struct integer* p)
{
while (p != NULL)
{
printf("%d", p->digit);
p = p->next;
}