0

Okay so I've been doing a program which would read elements of a txt file using scanf (cmd input redirection). A new node must be created for every entry in the file and add it at the end of the list. Here's my code so far:

struct Elem{
    int Atnum;
    char Na[31];
    char Sym[4]; 
};


struct nodeTag {
    struct Elem entry; 
    struct nodeTag *pNext; // pointer to the next node
};

typedef struct nodeTag Node;

The function that would initialize it is this:

Node *
InitializeList(Node *pFirst, int n)
{
    int i;
    Node *head, *temp = 0;

   pFirst = 0;

   for (i=0; i<n; i++){
        head  = (Node *)malloc(sizeof(Node));
        scanf("%d", &head->entry.AtNum); 
        scanf("%s", head->entry.Na);
        scanf("%s", head->entry.Sym);

        if (pFirst != 0)
        {
            temp->pNext = head;
            temp = head;
        }
        else
        {
            pFirst = temp = head;
        }
        fflush(stdin); 
        temp->pNext = 0;
    }

    return pFirst;
}

and lastly, print it

void
Print( Node *pFirst )
{
    Node *temp;
    temp = pFirst;
    printf("\n status of the linked list is\n");
    while (temp != 0)
    {
        printf("%d %s %s", temp->entry.AtNum, temp->entry.Na, temp->entry.Sym);
        temp = temp -> pNext;
    }

}

Now, I can't get the program to run properly. No run-time errors though but the output seems to be garbage. I've been working for hours for this and I cant' get my head around it. Thank you for your help!

Erail
  • 169
  • 1
  • 8
  • 1
    You put them in reversed order, thus, you have to return *temp*, not *pFirst* from your `InitializeList()`. And given above the lines `temp->pNext = 0;` and `pFirst = temp` are wrong. – 0andriy Mar 12 '17 at 10:06
  • Be wary of [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin); it doesn't necessarily do what you want/expect unless you're on Windows. – Jonathan Leffler Mar 12 '17 at 10:08
  • Hey mate, could you give me a feedback about my answer since I noted you didn't reply yet? I'm ready to help you – UrbiJr Mar 13 '17 at 20:27

1 Answers1

0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Elem
{
    int AtNum;
    char Na[31];
    char Sym[4]; 
};

struct nodeTag
{
    /*  entry must be a pointer in order to not lose the values
    and/or encounter memory conflicting errors
    */
    struct Elem *entry; 
    struct nodeTag *pNext;
};

typedef struct nodeTag Node;

// insert node at the first location
Node *insertFirst(Node *head, struct Elem *data)
{

    Node *node = (Node *) malloc(sizeof(Node));

    // fill in data
    node->entry = data;

    /*  point it to old first node
        in simple words: "put this node before the head"
    */
    node->pNext = head;

    // point first to new first node
    head = node;

    return head;

}

Node *InitializeList(int n)
{
    int i;
    Node *head = NULL;
    struct Elem *data;

    for (i = 0; i < n; i++)
    {
        // allocate memory for the struct Elem of each node
        data = (struct Elem*) malloc(sizeof(struct Elem));
        scanf("%d", &data->AtNum); 
        scanf("%s", data->Na);
        scanf("%s", data->Sym);
        head = insertFirst(head, data);        
    }

    return head;
}

//display the list
void printList(Node *head)
{
   Node *ptr = head;

   printf("\nStatus of the linked list is:\n");

   //start from the beginning
   while(ptr != NULL)
   {
      printf("%d %s %s", ptr->entry->AtNum, ptr->entry->Na, ptr->entry->Sym);
      printf("\n");
      ptr = ptr->pNext;
   }

}

int main(int argc, char *argv[])
{
    Node *head;

    head = InitializeList(3);
    printList(head);

    return 0;
}

I hope I didn't come too late! If not, please check this answer as the solution, thanks! :-)

UrbiJr
  • 133
  • 1
  • 2
  • 20