I’m having a bit of a problem when it comes to printing out this linked list.
The program is supposed to take a list of 10 characters from the user and print it out in the order it got it and then in reverse order (haven’t got that far yet). However, it’s not reading the first character.
For Example
“Please enter characters”
User types
a (program doesn’t read the a)
b
c
d
e
f
g
h
i
j
k
then it prints
b
c
d
e
f
g
h
i
j
k
Tried to make this as detailed as possible.
Thanks!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define strsize 30
typedef struct member
{
int number;
char fname[strsize];
struct member *next;
}RECORD;
RECORD* insert (RECORD *it);
RECORD* print(RECORD *it, int j);
int main (void)
{
int i;
double result;
RECORD *head, *p;
head=NULL;
result=10;
for (i=1; i<=result; i++)
head=insert (head);
print (head, result);
return 0;
}
RECORD* insert (RECORD *it)
{
RECORD *cur, *q;
int num;
char junk;
char first[strsize];
printf("Enter a character:");
scanf("%c", &junk);
scanf("%s", &first);
cur=(RECORD *) malloc(sizeof(RECORD));
strcpy(cur->fname, first);
cur->next=NULL;
if (it==NULL)
it=cur;
else
{
q=it;
while (q->next!=NULL)
q=q->next;
q->next=cur;
}
return (it);
}
RECORD* print(RECORD *it, int j)
{
RECORD *cur;
cur=it;
int i;
for(i=1;i<=j;i++)
{
printf("%s \n", cur->fname);
cur=cur->next;
}
return;
}
Not taking into account the other errors, your immediate problem is an extra
scanf. Thejunkcharacter is the one that gets ignored.Also, crank up the warning level of your compiler, and mind the warnings