Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,204 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,354 people online right now. Registration is fast and FREE... Join Now!




Problems writing to a linked list

 
Reply to this topicStart new topic

Problems writing to a linked list

jv1986
21 May, 2008 - 11:45 AM
Post #1

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 3

Having problems writing things to a linked list. Below you can see my whole code, the bit that is causing errors is written in the main function. It is supposed to work by inputting the 5 values entered by the user into the 1st 3 nodes of the linked list. It returns the following error

cc linktest7.c -o linktest7
linktest7.c: In function ‘record_create_after’:
linktest7.c:28: error: expected expression before ‘record’
linktest7.c: In function ‘main’:
linktest7.c:63: warning: assignment from incompatible pointer type
linktest7.c:73: warning: assignment from incompatible pointer type
linktest7.c:79: error: cannot convert to a pointer type
make: *** [linktest7] Error 1


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

typedef struct student {
    char ID[10];
    char Name[50];
    char Date[10];
    float Min_temp;
    float Max_temp;
} record;

typedef struct node_record {
    record data;
    struct node_record *next;
} NODE;

NODE *record_create(record data) {
    NODE *node;
    if(!(node=malloc(sizeof(NODE)))) return NULL;
    node->data = data;
    node->next = NULL;
    return node;
}

NODE *record_create_after(NODE *node, void *data) {
        NODE *newnode;
        newnode = record_create(record data);
        newnode->next = node->next;
        node->next = newnode;
        return newnode;
}

int list_foreach(NODE *node, int(*func)(void*), int(*func2)(float*)) {
      while(node) {
         if(func(node->data.ID)!=0) return -1;
         if(func(node->data.Name)!=0) return -1;
         if(func(node->data.Date)!=0) return -1;
         if(func2((void*)&(node->data.Min_temp))!=0) return -1;
         if(func2((void*)&(node->data.Max_temp))!=0) return -1;

         node=node->next;
      }
      return 0;
}

int printstring(void *s) {
      printf("%s \n",(char *)s);
      return 0;
}

int printfloat(float *f) {
      printf("%.3f \n",*f);
      return 0;
}



int main() {

        record *head_ptr;
    NODE *record_list;
        head_ptr = &record_list;
        int i,a,b,c,d,e;
        
        for(i=0;i<=3;i++){

            if(head_ptr == NULL) {
               printf("Enter Data : \n");
               scanf("%s %s %s %f %f",&a,&b,&c,&d,&e);
               record experiment = {a,b,c,d,e};
               record_list = record_create(experiment);
               head_ptr = &record_list;
            }
            else {
               printf("Enter Data : \n");
               scanf("%s %s %s %f %f",&a,&b,&c,&d,&e);
               record experiment = {a,b,c,d,e};
               record_create_after(record_list, (void*)experiment);
               }
        }

          printf("Initial list:\n");
    list_foreach(record_list, printstring, printfloat);
    putchar('\n');

    return 0;
}


This peice of code works when in place of the main function above

CODE
int main() {
    NODE *record_list;

    record experiment = {"12","bob","12/12/12",3,4};

    record_list = record_create(experiment);

    printf("Initial list:\n");
    list_foreach(record_list, printstring, printfloat);
    putchar('\n');

    return 0;
}


Any help with this would be massively apreciated.

This post has been edited by jv1986: 21 May, 2008 - 12:00 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month