0

I'm writing my first more serious program in C and I'm stuck. I need to sort this list to different, separate files so it will look like this:

BE30B Berlin 2014-04-02 Gale 02
BE30B Berlin 2014-04-02 Dobbs 15
OS43K Oslo 2014-04-03 Malik 34
BE30B Berlin 2014-04-02 Hatton 09
OS43K Oslo 2014-04-03 Lowe 21
OS43K Oslo 2014-04-03 Smith 03
BE30B Berlin 2014-04-02 Chapman 13
OS43K Oslo 2014-04-03 Murphy 41
BE30B Berlin 2014-04-02 Dawkins 19

Output:

BE30B.txt
Berlin 2014-04-02

02 Gale
09 Hatton
13 Chapman
15 Dobbs
19 Dawkins

I don't have an idea how to start writing it. I'm not too good at programming, I normally do html/css. My functions currently look like this and it prints the whole list on the screen.

struct Booking // creating a structure 
{
    char number[6];
    char dest[30];
    char date[11];
    char name[20];
    int seat;
};


struct Bkg
{
    struct Booking res;
    struct Bkg *next;
};

struct Bkg *head = NULL;

void add_on_top( char* argnumber, char* argdest, char* argdate, char* argname, int argseat)
{
    struct Bkg *temp=(struct Bkg*) malloc (sizeof(struct Bkg));

    strcpy(temp->res.number, argnumber);
    strcpy(temp->res.dest, argdest);
    strcpy(temp->res.date, argdate);
    strcpy(temp->res.name, argname);
    temp->res.seat = argseat;

    temp->next = head;
    head = temp;

}

void insert( char* argnumber, char* argdest, char* argdate, char* argname, int argseat)
{

    struct Bkg *head1 = head;
    if (head != NULL) {

        while (head1->next != NULL)
        {
            head1 = head1->next;
        }
        struct Bkg *temp = (struct Bkg*) malloc (sizeof(struct Bkg));
        strcpy(temp->res.number, argnumber);
        strcpy(temp->res.dest, argdest);
        strcpy(temp->res.date, argdate);
        strcpy(temp->res.name, argname);
        temp->res.seat = argseat;
        temp->next = NULL;
        head1->next = temp;

    }

    else
        add_on_top( argnumber, argdest, argdate, argname, argseat);
}

If someone could help me, I'd be very grateful. I just don't know how to sort it by the symbol, I can do the rest.

Symbol is the flight number: BE30B.

I didn't add main because there's not much going on there, I have struct Booking temp;, I open a file with reservation and read it, then process it using function fscanf and use my function insert(temp.number, temp.dest, temp.date, temp.name, temp.seat);

Writing a code in C is not my choice, this is something my school required only for this semester.

wintzy
  • 9
  • 2
  • Is the date after Berlin(2014-04-02) or Oslo(2014-04-03) always the same? – Rishikesh Raje Jan 18 '16 at 08:40
  • You have not added a `main` function. Please add it for proper understanding of the flow. – Rishikesh Raje Jan 18 '16 at 08:43
  • What do you mean sort by symbol ? – Nutan Jan 18 '16 at 08:49
  • What is "dynamic data structures"? – i486 Jan 18 '16 at 08:57
  • 1
    Too broad. Better start with the question: how to sort singly-linked list. As example of an algorithm, see in C++'s STL the implementation of the std::list::sort method. After you have sorted the list, you just group data with the same key when printing. – Dummy00001 Jan 18 '16 at 10:19
  • Have a look at this thread. Gives some ready made dictionary type functions in C... http://stackoverflow.com/questions/4384359/quick-way-to-implement-dictionary-in-c – Jimbo Jan 18 '16 at 10:30
  • The other useful structure (that you're already making) is a linked list – Jimbo Jan 18 '16 at 10:32
  • For future reference, if you have to write a program occasionally and it’s not really your forte, learning Python could be a good investment of your time. It’s a much higher level language than C and it takes care of a lot of the low-level stuff for you. – Tom Zych Jan 18 '16 at 10:53

1 Answers1

0

to get started:

define a struct

struct flight 
{
    char flightNumber[6];
    char destinationCity[30];
    char flightDate[11];
    char passengerName[30];
    unsigned passengerNumber;
}

use an array of struct flight

struct flight Flights[20];

open the source file for reading, be sure to add error checking

FILE *fp = NULL;
fp = fopen( "inputfile.txt", "r" );

read a line from the file using:

char *buffer = NULL;
size_t length = 0;

// loop through file
int recNum = 0;
while( -1 != getline( &buffer, &length, fp ) ) 

parse the fields

char *token = NULL;
token = strtok( buffer, " " ); //< add error checking for each call to strtok
strcpy( Flights[ recNum ].flightNumber, token );

token = strtok( NULL, " " );
strcpy( Flights[ recNum ].destinationCity, token );

token = strtok( NULL, " " );
strcpy( Flights[ recNum ].flightDate, token );

token = strtok( NULL, " " );
strcpy( Flights[ recNum ].passengerName, token );

token = strtok( NULL, " " );
Flights[ recNum ].passengerNumber = atoi(token);

recNum++;

end input loop

Sort the list, first on the passengerNumber field then sort again on the flightNumber

( a simple bubble sort would work for this small number of entries. you can google the internet for the algorithm )

in a loop, print the output

  1. where the change of the flightNumber causes the first line output format to be used
  2. then printing the passengerName+passengeNumber line output format

cleanup

fclose( fp );

This may not scale well for large input files, where the Flights[] might better be declared as:

struct flight **Flights = NULL;  

and use realloc() to get the memory for the array of pointer to struct flight

and use malloc() to get the actual memory area for each instance of a struct flight

if using the memory allocation functions, for efficiency, keep track of how many of the entries in the array are used and when full, double the number of entries. This minimizes the number of calls to realloc() which is important as each call to realloc() can result in the whole array being copied.

user3629249
  • 16,402
  • 1
  • 16
  • 17