I am supposed to create a linked list with each node having structure:
struct stack{
char paint;
int index;
struct stack *next;
}
typedef struct stack s;
I am Supposed to create linked list with m nodes by reading m inputs such as:
A 7
B 2
C 1
D 4 ....
so I tried using a for loop
like this which is failing and also i tried to sequential scanf
s like
scanf("%c",&c);
scanf("%d",&b);
I also tried this:
for(i=0;i<m;i++)
{
scanf("%c %d",&c,&b);
temp=(s *)malloc(sizeof(s));
temp->paint=c;
temp->index=b;
temp->next=head;
head=temp;
temp=NULL;
}
What am I doing wrong? I am actually scared of scanf
so I usually use getchar
all the time and if possible please explain how scanf
works when inputting from terminal or files which have n
rows of inputs. And also alternative statement of while((c=getchar())!='\n')
using scanf
?