Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   LINKED LIST PROBLEM..Plz help me Out (http://www.go4expert.com/forums/linked-list-problemplz-help-t22462/)

Programming_Kills 17Jun2010 15:49

LINKED LIST PROBLEM..Plz help me Out
 
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;
    }
}


jimblumberg 17Jun2010 20:56

Re: LINKED LIST PROBLEM..Plz help me Out
 
What error messages are you getting? Also check for matching {}.

Jim

Programming_Kills 17Jun2010 21:35

Re: LINKED LIST PROBLEM..Plz help me Out
 
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;
 }
}


jimblumberg 17Jun2010 22:02

Re: LINKED LIST PROBLEM..Plz help me Out
 
Quote:

Originally Posted by Programming_Kills (Post 69372)
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*)'"

In the code you supplied, I do not see where you implement the search_distination_duplicate function.

Jim.

Programming_Kills 17Jun2010 22:05

Re: LINKED LIST PROBLEM..Plz help me Out
 
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;


}

jimblumberg 17Jun2010 22:10

Re: LINKED LIST PROBLEM..Plz help me Out
 
Ok then you have two typos in your code because you are defining and calling search_distination_duplicate not search_destination_duplicate.

Jim

Programming_Kills 17Jun2010 22:21

Re: LINKED LIST PROBLEM..Plz help me Out
 
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...

jimblumberg 17Jun2010 22:29

Re: LINKED LIST PROBLEM..Plz help me Out
 
Its hard to tell without having the file that you are reading. What error messages are you getting if any?

Programming_Kills 17Jun2010 22:31

Re: LINKED LIST PROBLEM..Plz help me Out
 
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...

jimblumberg 17Jun2010 23:51

Re: LINKED LIST PROBLEM..Plz help me Out
 
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?


All times are GMT +5.5. The time now is 20:04.