Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING C NOT C++, I need to use <stdio.h> Your task is to create a sorted list (b

ID: 3708145 • Letter: U

Question

USING C NOT C++, I need to use <stdio.h>

Your task is to create a sorted list (by flight number) of flights with the information provided by the Flights.txt file that is provided as part of this assignment. The flight data consists of: airlines, flight number, arrival time, and departure time. You must also prompt the user to see if there is a flight to be deleted, once you have them in your linked list. The user can select a flight number and you must delete that flight from the linked list, and reprint the list. Continue asking the user if they want to delete a flight until the user types in ‘Q’ or ‘q’. Then exit the program.

1. The Flights.txt file name should be used as an argument to your program from the command line. Flights.txt should not be hard coded into your program. I will use a differently named text file to test your code. The file I use may also have a different number of flights.

2. You must read each line of the file, putting each flight into a structure, that is then used to create a linked list of flights.

3. You are not allowed to use any arrays to store your data, only the linked list.

4. Your program should dynamically allocate memory using calls to calloc or malloc when creating a new structure for the linked list. Similarly, when a structure is deleted, it should also be freed with free()

5. You must keep the linked list in a sorted order so that as you read the flights, you can insert the new flight into the correct place in the linked list. Also, when you delete a flight, the list should still be in order.

6. Print each flight as you read it into your program, then print the final sorted list before ending your program.

7. Functions should be used to insert, print, and delete the flight structures.

Any help with this code would be awesome and will recieve an upvote

Explanation / Answer

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

struct Flight{
   char airlines[2];
   int flNum;
   char arrival[4];
   char departure[4];
};

struct Node{
   struct Flight fl;
   struct Node *next;
};

struct Node *add(struct Node *head,struct Flight fl){
     struct Node *q = (struct Node *)malloc(sizeof(struct Node));
     strcpy(q->fl.airlines, fl.airlines);
     q->fl.flNum = fl.flNum;
     strcpy(q->fl.arrival, fl.arrival);
     strcpy(q->fl.departure, fl.departure);
     q->next = NULL;
     if (head == NULL){
        head = q;
     }
     else {
        struct Node *p = head;
        if (p->fl.flNum > fl.flNum){
           q->next = head;
           head = q;
        }
        else {
            struct Node *r = p->next;
            while(r->fl.flNum < fl.flNum && r != NULL){
                 p=r;
                 r=r->next;       
            }
            q->next = p->next;
            p->next = q;
        }
     }
     return head;
        
}

void print(struct Node *head){
    struct Node *p;
    p = head;
    while (p != NULL){
       printf("%s %d %s %s ", p->fl.airlines,p->fl.flNum, p->fl.arrival, p->fl.departure );
       p = p->next;
    }
}

void delete(struct Node *head, int n){
   struct Node *p;
   p = head;
   if (p!= NULL){
       if (p->fl.flNum == n){
          head = head->next;
          free(p);
       }
       else {
           struct Node *r = head->next;
           while(r!= NULL && r->fl.flNum != n){
               p = r;
               r = r->next;
           }
           if (r == NULL){
              printf("Not found ");
           }
           else {
               p->next = r->next;
               free(r);
           }
       }
   }
}

int main(int argc, char *argv[]){

   FILE *fp;
   struct Node *head = NULL;
   struct Flight fl;
   char line[40];
   char ch[2];
   int n;

   fp = fopen(argv[1],"r");
   if (fp == NULL){
       printf("Error opening file ");
       return 0;
   }
   while (fgets(line,40,fp)!=NULL){
      char *ch;
      ch = strtok(line," ");
      strcpy(fl.airlines,ch);
      ch = strtok(NULL," ");
      fl.flNum = atoi(ch);
      ch = strtok(NULL," ");
      strcpy(fl.arrival,ch);   
      ch = strtok(NULL," ");
      strcpy(fl.departure,ch);
      printf("%s %d %s %s ", fl.airlines,fl.flNum, fl.arrival, fl.departure );
      head = add(head,fl);              
   }
   while(1){
      printf("p to print, d to delete, q to quit ");
      scanf("%s",ch);
      if (ch[0] == 'q')
         break;
      if (ch[0] == 'p')
         print(head);
      if (ch[0] == 'd'){
         printf("Enter the flight number to delete:");
         scanf("%d",&n);
         delete(head,n);  
      }     
   }
   print(head);
}