-1

Picture of Text

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

ancd3ea4
  • 195
  • 1
  • 1
  • 14
  • 1
    can you show us what you have tried so far? – Stephen Docy Feb 21 '18 at 01:10
  • There are so many implementation of linked list, how should we know which one you are using? Please post code, better please post a [mcve] – Pablo Feb 21 '18 at 01:11
  • This is a basic problem to do with parsing. Maybe googling some examples of parsing? – mustachioed Feb 21 '18 at 01:16
  • Could there ever be more than 2 numbers on a line? If there could be an arbitrary number of them, then you'd need to use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or perhaps POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) to read lines, and then follow the guidelines in [Using `sscanf()` in loops](https://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops) to read the numbers from the string. – Jonathan Leffler Feb 21 '18 at 01:49

1 Answers1

1

Continue using "%s" or " %c" to read the command. Notice that there is a space before the %c: this is to consume the linebreak before reading the caracter (try it without the space and see what happens).

When you read an f or a d, issue another scanf("%d", ...) to get the number.

For the i, you can simply issue a scanf("%d%d", ...) and then check its return value. If it is 1, then only one number was read. If it is 2, then there were two integers.

Do not forget to check for EOF when trying to read the next command.

giusti
  • 3,156
  • 3
  • 29
  • 44