In this image, I have to write a program that every time it reads in the letter i
, it will insert the value after into the linked list. If it reaches a line where there are two numbers ex: i 23 42
, it will input 42
after the 23
into the linked list.
So if I print the linked list up to line 3 of the txt file, it should print 23, 78, 900
. When it reaches line four, it should print 23, 42, 78, 900
.
I'm not sure how to have the program recognize that there are two values in the line instead of one to be able to input the second after the first value into the linked list.
EDIT**
struct node * addToList(struct node *base, int data)
{
struct node *newNode = NULL;
if (base == NULL)
{
base = malloc( sizeof(struct node));
// malloc defend
base->data = data;
base->next = NULL;
return base;
}
while (base->next != NULL)
{
base = base->next;
}
//Now at the end of the list
newNode = malloc( sizeof(struct node));
//Defend against bad malloc
base->next = newNode;
newNode->data = data;
newNode->next = NULL;
return base;
'this next part is in main'
while (fscanf(ifp, "%s", inBuf) != EOF)
{
fprintf(stdout, "%s\n", inBuf);
data = atoi(inBuf);
if (NULL == root)
{
root = addToList(root, data);
}
else
{
temp = addToList(root, data);
if (NULL == temp)
{
printf("Failed to add - good bye\n");
return -1;
}
}
I'm attempting to create a new function to handle the insert