1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

LINKED LIST PROBLEM..Plz help me Out

Discussion in 'C' started by Programming_Kills, Jun 17, 2010.

  1. Programming_Kills

    Programming_Kills New Member

    Joined:
    Jun 14, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Hello Guyz..
    i am working on a simple program that works to communicate between two airports..
    i wrote some of the code but whenever i am compiling i get an error message plz help me out as soon as possible the code is...:)
    THANKS IN ADVANCE
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    typedef struct TAG_LIST{
        char flight[10];
        char departure[3];
        char arrival[3];
        struct TAG_LIST *next;
    }LIST_TYPE;
    typedef struct Airport_LIST{
        int count_flight;
        struct distination_LIST *destination;
        struct Airport_LIST *next;
    }Airport_LIST;
    typedef struct distination_LIST{
        char arrival_airport[3];
        struct distination_LIST *next;
    }distination_LIST;
    
    LIST_TYPE *create_list(FILE *fp);
    LIST_TYPE *Add_to_tail(LIST_TYPE *list1,LIST_TYPE *list2);
    void print_file(LIST_TYPE *list);
    
    main()
    {
        FILE * fp;
        LIST_TYPE *file_list;
        Airport_LIST *pnt_list_airport;
        printf("PRINTS FROM THE LIST OF AIRPORTS\n");
        fp=fopen("flights.txt","r");
        if(fp==NULL)
            printf("Sorry Error In reading the File\n");
        else
        {
            file_list=create_list(fp);
            fclose(fp);
      }
    
    
    LIST_TYPE *create_list(FILE *fp)
    {
        char temp_line[40];
        LIST_TYPE *headlist,*list;
        headlist=NULL;
        while(fgets(temp_line,40,fp)!=NULL)
        {
            list=(LIST_TYPE *)malloc(sizeof(LIST_TYPE));
            sscanf(temp_line,"%s%s%s",list->flight,list->departure,list->arrival);
            headlist=Add_to_tail(headlist,list);
        }
        return headlist;
    
    }
    LIST_TYPE *Add_to_tail(LIST_TYPE *list,LIST_TYPE *item)
    {
        LIST_TYPE *pntlist;
        pntlist=list;
        if(pntlist==NULL)
        {
            pntlist=item;
            item->next=NULL;
            list=pntlist;
        }
        else
        {
            while(pntlist->next!=NULL){
                pntlist=pntlist->next;}
            pntlist->next=item;
            item->next=NULL;
            list=pntlist;
        }
        return list;
        }
    void print_file(LIST_TYPE *pnt_list)
    {
        LIST_TYPE *list;
        list=pnt_list;
        printf("................File List.................\n\n\n");
        while(list!=NULL)
        {
            printf("\t%.6s\t",list->flight);
            printf("\t%.3s\t",list->departure);
            printf("\t%.3s\n",list->arrival);
            list=list->next;
        }
    }
     
    Last edited by a moderator: Jun 17, 2010
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    What error messages are you getting? Also check for matching {}.

    Jim
     
  3. Programming_Kills

    Programming_Kills New Member

    Joined:
    Jun 14, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    bro let me give you the complete syntax i solved...it shows me some error like " [Linker error] undefined reference to `search_distination_duplicate(Airport_LIST*, char*)'"

    my complete code is...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    typedef struct TAG_LIST{
        char flight[10];
        char departure[3];
        char arrival[3];
        struct TAG_LIST *next;
    }LIST_TYPE;
    typedef struct distination_LIST{
        char arrival_airport[3];
        struct distination_LIST *next;
    }distination_LIST;
    typedef struct Airport_LIST{
        int count_flight;
        char airport_departure[3];
        struct distination_LIST *destination;
        struct Airport_LIST *next;
    }Airport_LIST;
    
    
    LIST_TYPE *create_list(FILE *fp);
    LIST_TYPE *Add_to_tail(LIST_TYPE *list1,LIST_TYPE *list2);
    void print_list(Airport_LIST *list);
    void print_file(LIST_TYPE *list);
    Airport_LIST *order_airport(LIST_TYPE *list);
    int search_airport_duplicate(Airport_LIST *list,char *s);
    Airport_LIST *ordered_insertion(Airport_LIST *List_a,Airport_LIST *list_a_tem);
    distination_LIST *add_head_d(distination_LIST *dest,distination_LIST *list_dest);
    int search_distination_duplicate(Airport_LIST *dest,char *s);
    main()
    {
        FILE * fp;
        LIST_TYPE *file_list;
        Airport_LIST *pnt_list_airport;
        printf("PRINTS FROM THE LIST OF AIRPORTS\n");
        fp=fopen("flights.txt","r");
        if(fp==NULL)
            printf("Sorry Error In reading the File\n");
        else
        {
            file_list=create_list(fp);
            fclose(fp);
            if(file_list!=NULL)
            {
            print_file(file_list);
            pnt_list_airport=order_airport(file_list);
            print_list(pnt_list_airport);
            }
            else
                printf("List is Not Created\n");
        }
    }
    
    
    LIST_TYPE *create_list(FILE *fp)
    {
        char temp_line[40];
        LIST_TYPE *headlist,*list;
        headlist=NULL;
        while(fgets(temp_line,40,fp)!=NULL)
        {
            list=(LIST_TYPE *)malloc(sizeof(LIST_TYPE));
            
            sscanf(temp_line,"%s%s%s",list->flight,list->departure,list->arrival);
            headlist=Add_to_tail(headlist,list);
        }
        return headlist;
    
    }
    LIST_TYPE *Add_to_tail(LIST_TYPE *list,LIST_TYPE *item)
    {
        LIST_TYPE *pntlist;
        pntlist=list;
        if(pntlist==NULL)
        {
            pntlist=item;
            item->next=NULL;
            list=pntlist;
        }
        else
        {
            while(pntlist->next!=NULL){
                pntlist=pntlist->next;}
            pntlist->next=item;
            item->next=NULL;
            list=pntlist;
        }
        return list;
        }
    void print_file(LIST_TYPE *pnt_list)
    {
        LIST_TYPE *list;
        list=pnt_list;
        printf("................File List.................\n\n\n");
        while(list!=NULL)
        {
            printf("\t%.6s\t",list->flight);
            printf("\t%.3s\t",list->departure);
            printf("\t%.3s\n",list->arrival);
            list=list->next;
        }
    }
    
    Airport_LIST * order_airport(LIST_TYPE *headlist)
    {
        LIST_TYPE *file_line,*scan_list;
        file_line=headlist;
        Airport_LIST *list_a,*list_a_temp;
        distination_LIST *list_d_temp;
        while(file_line!=NULL)
        {
            scan_list=headlist;
            if(search_airport_duplicate(list_a,file_line->departure)==0)
            {
                list_a_temp=(Airport_LIST *)malloc(sizeof(Airport_LIST));
                strcpy(list_a_temp->airport_departure,file_line->departure);
                list_a_temp->count_flight=0;
                list_a_temp->next=NULL;
                while(scan_list!=NULL)
                {
                    if(strncmp(file_line->departure,scan_list->departure,3)==0)
                    {
                        if(search_distination_duplicate(list_a_temp,scan_list->arrival)==0)
                        {
                            list_a_temp->count_flight++;
                            //now create a new distination
                            list_d_temp=(distination_LIST *)malloc(sizeof(distination_LIST));
                            strcpy(list_d_temp->arrival_airport,scan_list->arrival);
                            list_a_temp->destination=add_head_d(list_a_temp->destination,list_d_temp);
                        }
                    }
                    scan_list=scan_list->next;
                }
               list_a=ordered_insertion(list_a,list_a_temp);
    }
            file_line=file_line->next;
        }
    
        return list_a;
        
    }
    int search_airport_duplicate(Airport_LIST *list,char s[])
    {
        Airport_LIST *pntlist;
        pntlist=list;
        int dup=0;
        while(pntlist!=NULL)
        {
            if(strncmp(pntlist->airport_departure,s,3)==0)
                dup++;
            pntlist=pntlist->next;
        }
        if(dup!=0)
            return 1;
        else
            return 0;
    }
    int search_destination_duplicate(Airport_LIST *list,char *s)
    {
        distination_LIST *pntlist;
        pntlist=list->destination;
        int dup=0;
        while(pntlist!=NULL)
        {
            if(strncmp(pntlist->arrival_airport,s,3)==0)
                dup++;
            pntlist=pntlist->next;
        }
        if(dup!=0)
            return 1;
        else
            return 0;
    
    
    }
    
    distination_LIST *add_head_d(distination_LIST *dest,distination_LIST *list_dest)
    {
        distination_LIST *temp;
        temp=dest;
        dest=list_dest;
        list_dest->next=temp;
        return dest;
    }
    
    Airport_LIST *ordered_insertion(Airport_LIST *List_a,Airport_LIST *list_a_tem)
    {
        Airport_LIST *pntlist,*temp,*prev_list;
        pntlist=List_a;
        if(pntlist!=NULL)
        {
            pntlist=list_a_tem;
            list_a_tem->next=NULL;
            return pntlist;
        }
        else  if(list_a_tem->count_flight<=pntlist->count_flight)
        {
            temp=pntlist;
            pntlist=list_a_tem;
            list_a_tem->next=temp;
            return pntlist;
        }
        else
        {
            prev_list=pntlist;
            pntlist=pntlist->next;
            while(pntlist!=NULL  && (list_a_tem->count_flight > pntlist->count_flight))
            {
                prev_list=pntlist;
                pntlist=pntlist->next;
            }
            temp=prev_list->next;
            prev_list->next=temp;
            list_a_tem->next=temp;
            return List_a;
        }
    }
    void print_list(Airport_LIST *list)
    {
        Airport_LIST *pntlist;
        pntlist=list;
        while(pntlist!=NULL)
        {
            printf("  %.3s",pntlist->airport_departure);
            while(pntlist->destination!=NULL)
            {
                printf("\t\t  %.3s\n",pntlist->destination->arrival_airport);
                pntlist->destination=pntlist->destination->next;
    }
            printf("\n");
            pntlist=pntlist->next;
     }
    }
     
    Last edited by a moderator: Jun 17, 2010
  4. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    In the code you supplied, I do not see where you implement the search_distination_duplicate function.

    Jim.
     
  5. Programming_Kills

    Programming_Kills New Member

    Joined:
    Jun 14, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    int search_destination_duplicate(Airport_LIST *list,char *s)
    {
    distination_LIST *pntlist;
    pntlist=list->destination;
    int dup=0;
    while(pntlist!=NULL)
    {
    if(strncmp(pntlist->arrival_airport,s,3)==0)
    dup++;
    pntlist=pntlist->next;
    }
    if(dup!=0)
    return 1;
    else
    return 0;


    }
     
  6. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    Ok then you have two typos in your code because you are defining and calling search_distination_duplicate not search_destination_duplicate.

    Jim
     
  7. Programming_Kills

    Programming_Kills New Member

    Joined:
    Jun 14, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    thanks bro...
    its actually one function just spell mistake..
    but still after reading two lines from the file the program get crashes....
    can you tell me why...
     
  8. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    Its hard to tell without having the file that you are reading. What error messages are you getting if any?
     
  9. Programming_Kills

    Programming_Kills New Member

    Joined:
    Jun 14, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    112341 MXP JDH
    223452 PWR RDH
    387893 BGM RME
    454564 KHR PRS
    545454 LHR MDD
    656565 JDH PWR



    these are the contents of the file...
     
  10. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    One of the problems is that in your structures departure and arrival are defined as char departure[3], and the string you are reading from the file is 4 characters wide, remember the terminating NULL.

    Also have you insured that you are reading the file correctly and you are creating the array of structures correctly?
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    "bro", WHAT ARE THE ERRORS? You said there were errors, we can't read your mind or see your screen, so you have to tell us these things.
     
  12. Programming_Kills

    Programming_Kills New Member

    Joined:
    Jun 14, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Write a C program that reads a file containing information about direct flights connecting a set of
    airports. Each line in the file has the following format:
    <flight number> <departure airport code> <destination airport code>
    where the flight numbers are strings of up to 6 characters and the airport codes are strings of 3
    characters.
    The C program must be able to manage files with a maximum of N different airports, and with any
    number of flights. The program must compute and write, for each different airport occurring in the
    file, the list of airports that are directly reachable from that airport and the list of airports from
    which it can be directly reached.
     

Share This Page