链表初始化

时间:2016-12-19 18:48:52

标签: c linked-list dynamic-allocation

我正在尝试使用链接列表更新我正在练习的上一个程序,以查看它们的工作原理。 我有一个包含公交车座位数的txt文件。读取此bus.txt文件之后是为特定链表和numberofseats动态分配内存..(例如45个席位)。 我写下面的代码,只是为了测试它是如何工作的,我试图把A放在每个乘客的全名中,0代表phonenr等。 当我尝试打印45个座位以确定它是否正常工作时,我会得到一个无限循环,并且永久打印出A .. 我错过了什么?

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

int i,j,numberofseats,temp;     
char platenr[8],selection;      
char firstname[20],lastname[20]; 
char phone[11];                  
char *p;                         

typedef struct psg               
    {
    char fullname[40];
    unsigned short phonenr[10]; 
    unsigned int seatnr;        
    struct psg *next
    }PASSENGERS;                


PASSENGERS* readfile(char *platenr, int *seatnr)
    {
    char buff[60];
    FILE *businfo;
    businfo = fopen ("bus.txt","r");
    if (businfo == NULL)
        {
        printf("Error Opening File, check if file bus.txt is present");
        exit(1);
        }
    else                                            
        {
        fscanf(businfo,"%s %d",platenr, seatnr);  
        printf("Bus Licence plate Nr is: %s, and Number of Seats is: %d", platenr, *seatnr); 
        PASSENGERS *p = malloc(*seatnr * sizeof(PASSENGERS)); 
        if (p==NULL)            
        {
            puts("Unable to allocate memory");
            exit(1);
        }
        return p;                                                      
        }

    }



int main()
{
PASSENGERS *passenger, *tmp, *start=NULL;
passenger = readfile(platenr,&numberofseats);
for (i=0;i<numberofseats;i++)
    {
    passenger->next=NULL;
    strcpy (passenger->fullname,"A");
    passenger->seatnr=i+1;
    for (j=0;j<10;j++)
        passenger->phonenr[j]=0;
    if (start==NULL)
        start=passenger;
    else{
        tmp=start;
    while (tmp->next !=NULL) tmp=tmp->next;
    tmp->next=passenger;
    }
    }
tmp=start;
while(tmp!=NULL)
{
    printf ("%s",tmp->fullname);
    tmp=tmp->next;
}
}

1 个答案:

答案 0 :(得分:1)

谢谢大家!我已经更改了我的代码,根据您的注释使其成为链接列表,并且它在函数之外工作。但是当试图将它传递到一个函数中时,我仍有一些问题..

#include <stdio.h>              
#include <string.h>             
#include <stdlib.h>             
int i,j,numberofseats,temp;     
char platenr[8],selection;      
char firstname[20],lastname[20]; 
char phone[11];                  
char *p;                         

typedef struct psg                   {
    char fullname[40];
    unsigned short phonenr[10]; 
    unsigned int seatnr;        
    struct psg *next
    }PASSENGERS;                

void readfile(char *platenr, int *seatnr, PASSENGERS passenger,PASSENGERS tmp,  PASSENGERS start)
    {
    char buff[60];
    FILE *businfo;
    businfo = fopen ("bus.txt","r");
    if (businfo == NULL)
        {
        printf("Error Opening File, check if file bus.txt is present");
        exit(1);
        }
    else                                                                
        {
        fscanf(businfo,"%s %d",platenr, seatnr);      
        printf("Bus Licence plate Nr is: %s, and Number of Seats is: %d", platenr, *seatnr); 
        for (i=0;i<numberofseats;i++)
        {
        passenger  = (PASSENGERS *) malloc (sizeof(PASSENGERS));
        if (p==NULL)            
            {
            puts("Unable to allocate memory");
            exit(1);
            }
        passenger->next=NULL;
        strcpy (passenger->fullname,"A");
        passenger->seatnr=i+1;
        for (j=0;j<10;j++)
            passenger->phonenr[j]=0;
        if (start==NULL)
            start=passenger;
        else{
            tmp=start;
        while (tmp->next !=NULL) tmp=tmp->next;
        tmp->next=passenger;
            }
        }
        tmp=start;
        while(tmp!=NULL)
            {
            printf ("%s",tmp->fullname);
            tmp=tmp->next;
            }

        return p;                                                       
        }

    }



int main()
{
PASSENGERS *passenger, *tmp, *start=NULL;
readfile(platenr,&numberofseats, passenger, tmp, start,);

}