1

I have the following structures:

typedef struct trans {
    int code;
    int issuersAccsID;
    double amount;
    int type;
    char date [100];
    int secondPartysId;
    char description [41];
    trans *next;
} transaction;


typedef struct acct{
    int id;
    char bank [50];
    int type;
    double balance;
    acct *next;
    transaction *tnext;
} account;


struct client{
    int id;
    char name [150];
    char city [200];
    char phone [30];
    client *next;
    account *anext;
};

When I insert elements in the list "clients" it's working fine, but the problem is when I go to the "sublist", because it inserts the last element infinitely.

void fileExists(client **p){
    client *ax = new client, *t = *p;
    scanf("%d",ax->id);
    scanf("%s",ax->name);
    ax->city="Example";
    scanf("%s",ax->phone);
        if (t==NULL)
            *p=ax;
                else{
                /*Insert at the end of the list*/
                    t = *p;
                    ax->next = NULL;
                        while(t->next != NULL)
                            t=t->next;
                    t->next = ax;
                }


/************************* ACCOUNTS ******************************/
    scanf("%d",auxid);
    scanf("%s",auxb);
    scanf("%d",auxt);
    scanf("%lf",auxbal);
                /*Insert at the end of the list*/
                    ax->anext = new account;
                    t = *p;
                    t->anext = (*p)->anext;
                    ax->anext->id = auxid;
                    strcpy(ax->anext->bank,auxb);
                    ax->anext->type = auxt;
                    ax->anext->balance = auxbal;
                    ax->anext->next = NULL;
                        if (t->anext == NULL)
                            (*p)->anext = ax->anext;
                        else while(t->anext->next != NULL)
                            t->anext=t->anext->next;
                    t->anext->next = ax->anext;
}

To explain better what's happening let's say we insert 2 clients

Client A: 1. ID = 4 2. Name = Frank 3. City = Example 4. Phone = 1111

Client A also has the following accounts

Account A: 1. ID = 3333 2. Bank = Exampl 3. Type = 2 4. Balance = 35.3

Account B: 1. ID = 4444 2. Bank = Exam 3. Type = 1 4. Balance = 38.3

Client B: 1. ID = 6 2. Name = Riley 3. City = Example 4. Phone = 2222

Client B also has the following accounts

Account A: 1. ID = 6666 2. Bank = Ex 3. Type = 2 4. Balance = 77.3

Account B: 1. ID = 8888 2. Bank = E 3. Type = 1 4. Balance = 7542.3

Account C: 1. ID = 9998 2. Bank = Ex 3. Type = 2 4. Balance = 752.63

When I finish inserting in the linked list the account list for Client A looks like this:

Account B -> Account B -> Account B -> Account B (...) nonstop. How could I solve it?

1 Answer 1

2
  1. You are using a mixture of C++ and C in the code with new and scanf You can use malloc as shown in the example below

  2. In the structure declarations, the code given will not compile. trans *next; should be changed to struct trans *next;. Similarly acct *next; to be changed to struct acct *next;

  3. In the client case, you should set ax->next = NULL for the case (t == NULL) also

  4. For inserting the account, I have modified the code by making use of the ax variable in the client case as below.

    account * acc = malloc (sizeof (account));
    account * acc2;
    if (acc == NULL)  {  exit (1); } // or any other method to handle memory error
    
    scanf("%d",acc->code);
    scanf("%s",acc->bank);
    scanf("%d",acc->type);
    scanf("%lf",acc->balance);        
    
    acc->next = NULL;
    
    if (ax->anext == NULL) 
    {
        ax->anext = acc;
    }
    else
    {
        acc2 = ax->anext;
        while (acc2->next != NULL)
        {
            acc2 = acc2->next;
        }
        acc2->next = acc;
    }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.