首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >列表的C语言列表

列表的C语言列表
EN

Stack Overflow用户
提问于 2018-06-07 02:15:15
回答 1查看 1.9K关注 0票数 -1

我遇到了一个问题,我正在尝试创建一个列表,其中inicioC指的是客户端的第一个节点,客户端的每个节点都将有一个称为inicioA的租赁列表。问题是,我不知道如何保存租赁的第一个指针,比如,客户端的第一个节点只保存一次,但每次租赁的第一个节点不会不同吗?

以下是结构:

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;

这是我用来将文件中的信息提取到列表列表中的代码

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;
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-07 07:26:01

Arraylist (C中的pointers数组)是实现list of list的一个很好的选择,但当您被迫这样做时,我将向您展示如何在C中开发list of list的一个简单方法。

这很简单,假设你有一个由3个数字组成的列表: 1,2,3即list_1,2个数字的列表: 5,6即这些list_1和list_2可以以类似于此场景中列表链接above.Look中的数字的方式链接起来

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

list_2:5->6->Null

list_of_list:list_1->list_2->Null,即(1->2->3->Null) -> (5->6->Null) -> Null

下面是一个在最后插入ints列表的示例程序:

#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);
}

此程序的输出:

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

我希望你明白这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50727025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档