Thread: linked list problem

  1. #1
    Registered User
    Join Date
    Apr 2009

    linked list problem

    hey guys

    am trying to build a linked list that reads info from a text file into the list.
    but i get an error saying ..request for member nextPtr in something not a structure or union..

    this is my code ..

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct listnode
     int data1;
     int data2;
     int weight;
     struct listnode *nextPtr;
    typedef struct listnode LISTNODE ;
    typedef LISTNODE *NodePtr;
    NodePtr insert(NodePtr *, int ,int , int);
    void print(NodePtr);
    NodePtr createNode (NodePtr *, int, int, int);
      FILE *fptr;
      char filename[255];
      NodePtr node = NULL;
      int d1,d2,d3;
    printf("enter filename\n");
      scanf ("%s",filename);
      fptr = fopen(filename,"r");
      if (fptr == NULL)
          printf ("file cannot be found");
    while (!feof(fptr))
        fscanf (fptr, "%d %d %d", &d1,&d2,&d3);
        insert(&node , d1,d2,d3 );
      NodePtr createNode (NodePtr *anode ,int value1,int value2,int value3)
         NodePtr node;
         node  = malloc(sizeof(LISTNODE));
         node->data1 = value1;
         node->data2 = value2;
         node->weight = value3;
         node->nextPtr = NULL;
         return node;
    NodePtr insertnew(NodePtr *node, int value1, int value2, int value3 )
        NodePtr newNode ;
        newNode = createNode(&newNode,value1,value2,value3);
         newNode->nextPtr = node->nextPtr;  ( AM GETTING THE ERROR.....
         node->nextPtr = newNode;                      ON THESE 2 LINES ) 
         return newNode;
    void print(NodePtr newNode)
         if( newNode == NULL)
              printf("List is empty\n\n");
         else {
             printf("The List is:\n");
         while (newNode != NULL )
              printf ("%d %d %d --->", newNode->data1, newNode->data2, newNode->weight);
              newNode = newNode->nextPtr;
    can anyone please help me out has been bugging my brains out..
    any help would be much appreciated

  2. #2
    Registered User
    Join Date
    Sep 2007
    node is a pointer to a pointer to a struct, not a pointer to a struct. The typedefing of a pointer makes this somewhat obscured, which is why I'd recommend not doing that. Instead, you would have something like:
    struct node *insert(struct node **node, int val);
    This way it's very clear what the various types are.

    The solution is the same, in either case: use something like (*node)->next = blah, although for your program I see no reason why you'd pass in a pointer to a pointer.

  3. #3
    Registered User
    Join Date
    Apr 2009
    ok thanx for the input cas... i have changed the code and no longer use insert... but now am getting a segmentation error.. i have posted a seperate thread for that..i am thinking its my fscanf or something..

    ur help wud be much appreciated .

  4. #4
    Registered User
    Join Date
    Apr 2009
    NodePtr insert(NodePtr *, int ,int , int);
    NodePtr insertnew(NodePtr *node, int value1, int value2, int value3 )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM
Website Security Test