linked list problem

This is a discussion on linked list problem within the C Programming forums, part of the General Programming Boards category; hey guys am trying to build a linked list that reads info from a text file into the list. but ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    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 ..


    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);
    main()
    {
      FILE *fptr;
      char filename[255];
      NodePtr node = NULL;
      int d1,d2,d3;
    printf("enter filename\n");
      scanf ("%s",filename);
    printf("%s\n",filename);
      fptr = fopen(filename,"r");
    
      if (fptr == NULL)
            {
          printf ("file cannot be found");
          exit(0);
            }
    
    while (!feof(fptr))
         {
    
        fscanf (fptr, "%d %d %d", &d1,&d2,&d3);
    
        insert(&node , d1,d2,d3 );
    
        print(node);
          }
    fclose(fptr);
    }
    
      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;
              }
               printf("NULL\n\n");
              }
        }
    can anyone please help me out here..it has been bugging my brains out..
    any help would be much appreciated

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    978
    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:
    Code:
    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
    Posts
    4
    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
    Location
    Russia
    Posts
    116
    Code:
    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, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21