-1

im having a problem, im trying to make a list of a list where inicioC refers to the first node of clients, and every node of clients will have a list of rentals, referred as inicioA. The thing is, i dont know how to save the first pointer of the rentals, like, the first node of clients will only be saved one time, but wont the first node of every rental be different?

this are the structs:

typedef struct _aluguer
{
    int id, estado, diaI, mesI, anoI, diaE, mesE, anoE;
    struct _aluguer *prox;
}aluguer, *pAluguer;

typedef struct _registo
{
    int nif,nalu;
    char nome[100];
    struct _registo *prox;
    pAluguer *inicioA;
}registo, *pRegisto;

and this is the code I use to extract the info from a file into the list of lists

pRegisto iniC(pRegisto inicioC, pAluguer inicioA)
{

FILE *c;
int j, nif, nalu, l=0;
char nome[100];

c = fopen("clientes.txt", "r"); // abrir ficheiro

if(c == NULL)
{
    printf("Erro ao abrir ficheiro %s", "clientes.txt");
    exit(0);
}


while(fscanf(c, "%d %d %s", &nif, &nalu, nome) == 3) //format of info
{
    pRegisto novoC = malloc(sizeof(registo));
    if(novoC == NULL)
    {
        printf("erro alocacao memoria\n");
        return inicioC;
    }
    novoC -> prox = NULL;
    pAluguer inicioA = NULL;

    pRegisto aux = inicioC;
    pRegisto p = NULL;

    novoC->nif=nif;
    novoC->nalu=nalu;
    strcpy(novoC->nome, nome);

    while(aux != NULL)
    {
        p = aux;
        aux = aux->prox;
    }

    if( aux == inicioC)
    {
        inicioC=novoC;
    }
    else
    {
        p->prox = novoC;
    }


    for(j=0; j<novoC->nalu; j++) // repeat is equal to the number of rentals
    {
        l++;
        pAluguer novoA = malloc(sizeof(aluguer));

        if(novoA == NULL)
        {
            printf("erro alocacao memoria\n");
            return inicioC;
        }
        novoA -> prox = NULL;

        pAluguer aux = inicioA;
        pAluguer p = NULL;

        fscanf(c, "%d %d", &(novoA->id), &(novoA->estado));

        if(novoA->estado == 0)
        {
            fscanf(c, " %d %d %d", &(novoA->diaI), &(novoA->mesI), &(novoA->anoI));
        }
        else
        {
            fscanf(c, " %d %d %d %d %d %d", &(novoA->diaI), &(novoA->mesI), &(novoA->anoI), &(novoA->diaE), &(novoA->mesE), &(novoA->anoE));
        }


        while(aux != NULL)
        {
            p = aux;
            aux = aux->prox;
        }

        if( aux == inicioA)
        {
            inicioA=novoA;
        }
        else
        {
            p->prox = novoA;
        }
    }
}

fclose(c);

return inicioC;
}
krpra
  • 466
  • 1
  • 4
  • 19
Walter Palma
  • 27
  • 1
  • 5
  • You can use `array of pointers` instead. – krpra Jun 06 '18 at 18:31
  • Well, im forced to do it this way, I just dont know how im gonna save the first pointer of rental of every node of client – Walter Palma Jun 06 '18 at 18:42
  • 2
    You will want to review: [Is it a good idea to **typedef** pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). (hint: the answer is No) – David C. Rankin Jun 06 '18 at 20:06

1 Answers1

0

Arraylist (Array of pointers in C) is good choice for implementing list of list but as you are forced to do in this way, I am showing a Simple method how you can develop list of list in C.

It is very simple,suppose you have a list of 3 numbers: 1,2,3 i.e list_1,list of 2 numbers: 5,6 i.e list_2.Now these list_1 and list_2 can be linked in a similar way as the numbers in list linked above.Look at this scenario

list_1:1->2->3->Null

list_2:5->6->Null

list_of_list : list_1->list_2->Null i.e. (1->2->3->Null) -> (5->6->Null) -> Null

Here is a Sample program to insert list of ints in Last:

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

// for list of Intergers
struct node{
    int Data;
    struct node *next;
};

// this is for list of nodes i.e list of lists
struct list{
    struct node *start;
    struct list *listnext;
};


// insert integers in list 

void insertNode(struct node **head ,int data){
    struct node *temp,*current;
    temp=malloc(sizeof(struct node));
    temp->Data=data;
    temp->next=NULL;

    if((*head)==NULL){
        (*head)=temp;
    }
    else{
        current=(*head);
        while(current->next!=NULL){
            current=current->next;
        }
        current->next=temp;
    }
}

//  insert lists of integers in a list

void insertList(struct list **Listhead,struct node *head){
    struct list *temp,*current;
    temp=malloc(sizeof(struct list));
    temp->start=head;
    temp->listnext=NULL;
    if((*Listhead)==NULL){
        (*Listhead)=temp;
    }
    else{
        current=(*Listhead);
        while(current->listnext!=NULL){
            current=current->listnext;
        }
        current->listnext=temp;
    }
}

// Show all the list with their data

void show(struct list *Listhead){
    int i=1;
    struct list *current;
    struct node *currentlist;
    current=Listhead;
    while(current!=NULL){
        currentlist=current->start;
        printf("List %d: ",i);
        while(currentlist!=NULL){
            printf("%d ",currentlist->Data);
            currentlist=currentlist->next;
        }
        i++;
        printf("\n");
        current=current->listnext;
    }
}

int main(){
    struct node *head1=NULL,*head2=NULL,*head3=NULL; // 3 lists of integers
    struct list *Listhead=NULL;  // Listhead will be the head of lists of list
    insertNode(&head1,20);  // inserting in first list
    insertNode(&head1,13);
    insertNode(&head1,22);
    insertNode(&head1,18);
    insertNode(&head2,42);  // inserting in second list
    insertNode(&head2,15);
    insertNode(&head3,12);  // inserting in third list
    insertNode(&head3,14);
    insertNode(&head3,28);
    insertList(&Listhead,head1); // inserting lists in list
    insertList(&Listhead,head2);
    insertList(&Listhead,head3);
    show(Listhead);
}

Output for this Program:

List 1: 20 13 22 18
List 2: 42 15
List 3: 12 14 28

I hope you got the point.

krpra
  • 466
  • 1
  • 4
  • 19