Hi guys, having a bit of trouble with my printlist function (it prints out all 1's, instead of the values that i enter):
So if i input:
.
The output will be:
Any help would be great.
Cheers, Rob
Code:
#include <stdlib.h>
#include <stdio.h>
typedef struct L {
int data;
struct L *next;
} list;
list *insertList( int hd, list *tl ) {
list *t = malloc( sizeof( list ) ) ;
t->data = hd ;
t->next = tl ;
return t ;
}
list *somelist( void ) {
list *h = NULL ;
int i=1;
int c;
while(c=getchar()!='.') {
h=insertList( c, h );
i++;
}
return h ;
}
void printlist(list *head) {
int i;
list *current = head;
while(current!=NULL) {
printf("%d", current->data);
current=current->next;
}
}
int length(list *head) {
list *current = head;
int count = 0;
while (current != NULL) {
count++;
current = current->next;
}
printf("%d\n", count);
return count;
}
int main(void) {
list *z=somelist();
length(z);
printlist(z);
return 0;
}
So if i input:
Code:
12345
The output will be:
Code:
5
11111
Any help would be great.
Cheers, Rob