Thread: Linked List problem

  1. #1
    unregistered
    Guest

    Linked List problem

    How do I write a C program that will use a linked list of structures to
    represent the customers of a bank?
    Each structure has to be designed
    to hold information regarding a persons
    how would i start? ima newbie to this and i dont quite get it

    (1) Last name
    (2) First name
    (3) Middle initial
    (4) Social Security number
    (5) Bank balance

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    A quick look in the tutorials of this site turned up This
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My post here should clue you in.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    Looks like homework to me.....

    but anyway first define the structure:

    [CODE]
    typedef struct customer
    {
    char fname[20];
    char lname[20];
    char initial;
    long socialnum;
    double balance;
    struct customer *next;
    } CUSTOMER;

    then create a pointer to this:

    CUSTOMER *bankrecords;

    assign memory:

    bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));

    then do what ya want with it ie to add a name do:

    strcpy(bankrecords->fname,"FRED");

    to add a new customer go to the end of the list, ie
    tempptr = bankrecords;
    while(tempptr->next!=NULL)
    tempptr=tempptr->next;

    then assign more memory
    tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
    and repeat the adding, I'm sure you can work out the rest otherwise its not really homework where ya learning anything at all.
    Hope this helps

  5. #5
    Unregistered
    Guest
    thank you all very much, you have been of great help, it is a homework assignment but becuase im a biology major and not a computer science major its difficult for me but im working on it

  6. #6
    Unregistered
    Guest
    i have a problem i need to be able to read data from a file and im not sure how to do it. This is what i have so far.

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

    struct customer
    {
    char fname[20];
    char lname[20];
    char initial;
    long socialnum;
    double balance;
    struct customer *next;
    };


    int main(void)
    {
    FILE *infile;


    infile= fopen("data.dat","r");




    fclose(data.dat)
    return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets reads a line.
    fscanf works like scanf.
    fgetc reads a character.

    That should get you started. When all else fails, consult the man pages.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Unregistered
    Guest
    those will not work when im trying to read the data into a structure. I have a data file that has info like this:

    Moon Warren H 637801463 19473.08

    So it has to be able to read the first name into bankrecords.fname and last name into bankrecords.lname and so on.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    fscanf will work perfectly, just do:

    Code:
    fscanf(infile,"%s %s %c %d %f",(*bankrecords)->fname,(*bankrecords)->lname,bankrecords->initial,bankrecords->account,bankrecords->balance);
    think the pointers and *'s are right not 100% but pretty sure about the first two, and slightly to lazy to double check

    p.s. make sure you assign the memory before you do the fscanf or you'll have problems.

  10. #10
    Unregistered
    Guest
    When i use
    bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
    it gives me errors

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    you need to assign your structure a name, if you have a look at the code example I gave it will work:

    Code:
    typedef struct customer 
    { 
      char fname[20]; 
      char lname[20]; 
      char initial; 
      long socialnum;
      double balance; 
      struct customer *next; 
    } CUSTOMER;
    the last line : } CUSTOMER;
    allow you to treat this as a standard data type and decalre variables like:
    CUSTOMER john;
    and then you have a variable called john.
    alternatevly you could use:

    bankrecords = (struct customer *) malloc(sizeof(struct customer));
    I think, but the first way will work and make it heaps easier to assign new variables, etc.

  12. #12
    Unregistered
    Guest
    I have this and it says my variables are not identified.

    [code]

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

    struct customer
    {
    char fname[20];
    char lname[20];
    char initial;
    long socialnum;
    double balance;
    struct customer *next;
    };


    int main()
    {
    FILE *infile;
    struct customer *bankrecords;
    int count=1;

    bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));


    if((infile = fopen("data.dat","r")) == NULL) {
    printf("Data file could not be opened.\n");
    exit(1);
    }
    infile=fopen("data.dat","r");
    printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
    printf("-----------------------------------------------------\n");
    while(!feof(infile)) {
    //fread(&bankrecords,sizeof(struct customer),1,infile);
    fscanf(infile,"%s %s %c %d %f",bankrecords.fname,bankrecords.lname,bankrecord s.initial,bankrecords.socialnum,bankrecords.balanc e);

    printf("\n %s",bankrecords.fname);


    }


    fclose(infile);
    return 0;
    }

    [code\]

  13. #13
    Unregistered
    Guest
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    typedef struct customer
    {
    char fname[20];
    char lname[20];
    char initial;
    long socialnum;
    double balance;
    struct customer *next;
    }CUSTOMER;


    int main()
    {
    FILE *infile;

    int count=1;

    bankrecords = (struct customer *) malloc(sizeof(struct customer));


    if((infile = fopen("data.dat","r")) == NULL) {
    printf("Data file could not be opened.\n");
    exit(1);
    }
    infile=fopen("data.dat","r");
    printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
    printf("-----------------------------------------------------\n");
    while(!feof(infile)) {
    //fread(&bankrecords,sizeof(struct customer),1,infile);
    fscanf(infile,"%s %s %c %d %f",bankrecords.fname,bankrecords.lname,bankrecord s.initial,bankrecords.socialnum,bankrecords.balanc e);

    printf("\n %s",bankrecords.fname);


    }


    fclose(infile);
    return 0;
    }

    ok im gettin error messages that say.............

    --------------------Configuration: lab5 - Win32 Debug--------------------
    Compiling...
    lab5.cpp
    C:\Documents and Settings\All Users\Documents\lab5.cpp(22) : error C2065: 'bankrecords' : undeclared identifier
    C:\Documents and Settings\All Users\Documents\lab5.cpp(22) : error C2440: '=' : cannot convert from 'struct customer *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.fname' must have class/struct/union type
    C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.lname' must have class/struct/union type
    C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.initial' must have class/struct/union type
    C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.socialnum' must have class/struct/union type
    C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2228: left of '.balance' must have class/struct/union type
    C:\Documents and Settings\All Users\Documents\lab5.cpp(36) : error C2228: left of '.fname' must have class/struct/union type
    Error executing cl.exe.

    lab5.obj - 8 error(s), 0 warning(s)

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    don't use dot notation to access that structure's data fields. Use the -> operator.

    struct customer *john;

    john -> balance = 2.54;

    And try,..
    CUSTOMER bankrecords = (struct customer *) malloc(sizeof(struct customer));
    http://www.KBeutler.com

  15. #15
    Unregistered
    Guest
    ok where are you gettin john->balance from? and i still get errors with my code

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

    typedef struct customer
    {
    char fname[20];
    char lname[20];
    char initial;
    long socialnum;
    double balance;
    struct customer *next;
    }CUSTOMER;


    int main()
    {
    FILE *infile;

    int count=1;

    CUSTOMER bankrecords = (struct customer *) malloc(sizeof(struct customer));


    if((infile = fopen("data.dat","r")) == NULL) {
    printf("Data file could not be opened.\n");
    exit(1);
    }
    infile=fopen("data.dat","r");
    printf("NUM ADDRESS LAST\tFIRST MI\tSSN BALANCE NEXT\n");
    printf("-----------------------------------------------------\n");
    while(!feof(infile)) {
    //fread(&bankrecords,sizeof(struct customer),1,infile);
    fscanf(infile,"%s %s %c %d %f",CUSTOMER bankrecords->fname,CUSTOMER bankrecords->lname,CUSTOMER bankrecords->initial,CUSTOMER bankrecords->socialnum,CUSTOMER bankrecords->balance);

    printf("\n %s",bankrecords->fname);


    }


    fclose(infile);
    return 0;
    }

    --------------------Configuration: lab5 - Win32 Debug--------------------
    Compiling...
    lab5.cpp
    C:\Documents and Settings\All Users\Documents\lab5.cpp(22) : error C2440: 'initializing' : cannot convert from 'struct customer *' to 'struct customer'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2275: 'CUSTOMER' : illegal use of this type as an expression
    C:\Documents and Settings\All Users\Documents\lab5.cpp(13) : see declaration of 'CUSTOMER'
    C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2146: syntax error : missing ')' before identifier 'bankrecords'
    C:\Documents and Settings\All Users\Documents\lab5.cpp(34) : error C2059: syntax error : ')'
    C:\Documents and Settings\All Users\Documents\lab5.cpp(36) : error C2819: type 'customer' does not have an overloaded member 'operator ->'
    C:\Documents and Settings\All Users\Documents\lab5.cpp(6) : see declaration of 'customer'
    C:\Documents and Settings\All Users\Documents\lab5.cpp(36) : error C2227: left of '->fname' must point to class/struct/union
    Error executing cl.exe.

    lab5.obj - 6 error(s), 0 warning(s)

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, 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, 10:21 PM