Question

***In C Programming*** TASK 1: Complete itinerary.h Create a struct Destination that represents a node in...

***In C Programming***

TASK 1: Complete itinerary.h

Create a struct Destination that represents a node in a liked list. It contains the three letter airport code and a pointer to the next node int the linked list.  You will need to write this struct.

This file also includes the prototypes. They have all been written for you. There is no need to add the function comments for the prototypes.

TASK 2: Write all functions in itinerary.c

Write the following functions, which will be used in the main.

  • Destination *create( char *code ) - Dynamically allocates a new node with the airport code data and returns a pointer to that new node.
  • Destination *insertAfter( Destination *head, Destination *node, char *key ) - This function enters a new node, created by the create() function, into the linked list. Normally, the function will add the new node after the node that has an airport code that matches key. However, there are two exceptions to this rule: 1) if the key is equal to the special code ZZZ, add the new node to the front, and 2) if the key was not found in the list, add the new node to the end of the linked list. It should also handle insertion should the linked list be empty. It returns the head of the linked list.
  • Destination *find( Destination *head , char *key ) - This function traverses the linked list to find a node that has an airport code that matches key and returns it. If it did not find this airport code, it returns NULL.
  • Destination *removeNode( Destination *head, char *key ) - This function traverses the linked list to find a node that has an airport that matches key and removes it from the linked list. If it is not found, nothing happens to the linked list. It returns the head of the linked list.
  • void print( Destination *head ) - Prints out each destination (airport code) in the itinerary on a separate line.
  • void printItinerary( Destination *head ) - Prints out the itinerary showing each segment pair between airports on a different line. This requires traversing the linked list and printing out each destination and the next destination. For example, if your linked list had these destinations in this order: AUS,JFK,YUL,LHR the printed itinerary would look like this:

AUS-JFK
JFK-YUL
YUL-LHR

  • Destination *destruct( Destination * ) - Destructor for the linked list. It goes through the entire linked list and free() up each nodes and returns NULL at the end.

TASK 3: Complete function in main.c

The main function has an infinite loop where it will call the printMenu() function and ask the user to make a selection.

The switch statement will process the user input. Depending on the selection, the main() will ask the user for more inputs and then call the appropriate functions in itinerary.c. The base code for the main has already been started for you. See the comments for specific tasks each selection requires. Always assume the user may not always enter a valid value for the menu selection.

TASK 4: Makefile

Create a Makefile that will create the executable.

#include <stdio.h>
#include "itinerary.h"
#include "main.h"

/* 
    printMenu
    ---------------------------
    This function prints the main menu.
    Returns: Nothing
*/
void printMenu()
{
    printf( "\n#########################################\n" );
    printf( "#            Trip Planner Menu          #\n" );
    printf( "#########################################\n" );
    printf( " 1 - Insert a Destination\n" );
    printf( " 2 - Remove a Destination\n" );
    printf( " 3 - Print Destinations\n" );
    printf( " 4 - Print Itinerary\n" );
    printf( " 5 - Clear Itinerary\n" );
    printf( " 0 - Quit\n" );
    printf( "Enter your selection: " );
}

int main( void )
{
        /* declare all your variables here */
        int choice;
    
        while( TRUE )
        { 
                printMenu();

        /* adapt the scanf() to handle invalid input */
        scanf( "%d", &choice );
        
        switch( choice )
        {
            case 1:
                /* 1) ask the user to enter the airport code
                   2) check to see if the airport code exists in the list, if it does, print an error and break;
                   2) if the list is empty, call create() and insertAfter() to create and insert it into the linked list
                   3) if the list is not empty, ask the user where in the list they want to add the node. The user will enter the airport code in which this new node will be added AFTER in the linked list. Then, call create() and insertAfter() to create and insert it into the linked list. Note that if the user enters ZZZ as the airport code, it will add the new node to the beginning and if the user enters an airport code, it adds the new node to the end. 
                   4) Print a message saying the destination was added
                */
                break;
            case 2:
                /* 1) ask the user to enter an airport code
                   2) check to see if the airport code exists in the list with find(), if it does not exist, print an error and break;
                   3) call remove() the airport code from the list  
                   4) print a message saying the destination was removed
                */
                break;
            case 3:
                /* call print() */
                break;
            case 4:
                /* call printItinerary() */
                break;
            case 5:
                /* call destruct() */
                break;
            case 0:
                /* print a farewell message and call destruct() */
                return 1;
            default:
                /* print a message saying that the input choice was not valid */
                break;
        }
        }

    return 0;
}
#ifndef MAIN_H
#define MAIN_H

#define TRUE 1
#define FALSE 0

/* 
    printMenu
    ---------------------------
    This function prints the main menu.
    Returns: Nothing
*/
void printMain();

#endif

Sample Output

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 1
Enter the airport code for the destination: SAT
SAT added

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 1
Enter the airport code for the destination: DFW
Enter the airport code for which new destination is added after. Enter ZZZ to add to beginning of list: SAT
DFW added

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 1
Enter the airport code for the destination: JFK
Enter the airport code for which new destination is added after. Enter ZZZ to add to beginning of list: ZZZ
JFK added

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 3

Destinations in Itinerary
JFK
SAT
DFW

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 1
Enter the airport code for the destination: SAT
ERROR: Destination already exists.

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 1
Enter the airport code for the destination: BKK
Enter the airport code for which new destination is added after. Enter ZZZ to add to beginning of list: JFK
BKK added

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 3

Destinations in Itinerary
JFK
BKK
SAT
DFW

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 2
Enter the airport code for the destination: HKG
ERROR: Destination does not exist.

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 4

Itinerary
JFK-BKK
BKK-SAT
SAT-DFW
Total Segments: 3

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 5
Itinerary cleared.
#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 1
Enter the airport code for the destination: SAT
SAT added

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 3

Destinations in Itinerary
SAT

#########################################
#            Trip Planner Menu          #
#########################################
 1 - Insert a Destination
 2 - Remove a Destination
 3 - Print Destinations
 4 - Print Itinerary
 5 - Clear Itinerary
 0 - Quit
Enter your selection: 0
Good-bye
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Itenary.h

#pragma once
#ifndef ITENARY_H
#define ITENARY_H

struct Destination
{
   char code[4];
   struct Destination* next;
};

struct Destination *create(char *code);
struct Destination *insertAfter(struct Destination *head, struct Destination *node, char *key);
struct Destination *find(struct Destination *head, char *key);
struct Destination *removeNode(struct Destination *head, char *key);
void print(struct Destination *head);
void printItinerary(struct Destination *head);
struct Destination *destruct(struct Destination *);

#endif

//itenary.c

#include<stdio.h>
#include "itenary.h"

struct Destination *create(char *code)
{
   struct Destination* node = NULL;
   node = (struct Destination*)malloc(sizeof(struct Destination));
   strcpy(node->code, code);
   node->next = NULL;
   return node;
}

struct Destination *find(struct Destination *head, char *key)
{
   struct Destination* traverse = head;

   while (traverse != NULL)
   {
       if (!strcmp(traverse->code, key))
           break;

       traverse = traverse->next;
   }
   return traverse;
}

struct Destination *destruct(struct Destination *head)
{
   if (head == NULL)
       return NULL;

   struct Destination* traverse = head;
   while (head != NULL)
   {
       traverse = head;
       head = head->next;
       free(traverse);
   }
   return NULL;
}

void printItinerary(struct Destination *head)
{
   struct Destination* traverse = head;

   if (head == NULL)
       return;
   if (head->next == NULL)
       printf("%s", head->code);

   while (traverse->next != NULL)
   {
       printf("%s-%s\n", traverse->code, traverse->next->code);
       traverse = traverse->next;
   }
}

void print(struct Destination *head)
{
   struct Destination* traverse = head;

   if (head == NULL)
       return;

   while (traverse != NULL)
   {
       printf("%s\n", traverse->code);
       traverse = traverse->next;
   }
}

struct Destination *removeNode(struct Destination *head, char *key)
{
   struct Destination* traverse = head;

   if (head == NULL) return head;
   if (head->next == NULL)
   {
       if (!strcmp(head->code, key))
           return NULL;
   }

   while (traverse->next != NULL)
   {
       if (!strcmp(traverse->next->code, key))
           break;

       traverse = traverse->next;
   }

   if (traverse->next != NULL)
   {
       return head;
   }
   else
   {
       struct Destination* temp = traverse->next->next;
       traverse->next = temp;
       return head;
   }
}

struct Destination *insertAfter(struct Destination *head, struct Destination *node, char *key)
{
   struct Destination* traverse = head;

   if (head == NULL)
   {
       head = node;
       return head;
   }
   else
   {
       char zString[4] = { 'Z','Z','Z','\0' };
       while (traverse->next != NULL)
       {
           if (!strcmp(zString, key))
           {
               node->next = head;
               head = node;
               return head;
           }
           else if (!strcmp(traverse->code, key))
           {
               struct Destination* temp = NULL;
               if (traverse->next != NULL)
               {
                   temp = traverse->next;
                   traverse->next = node;
                   node->next = temp;
               }
               else
               {
                   traverse->next = node;
               }
               return head;
           }
           traverse = traverse->next;
       }
       traverse->next = node;
       return head;
   }
}

//main.c

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

/*
printMenu
---------------------------
This function prints the main menu.
Returns: Nothing
*/
void printMenu()
{
   printf("\n#########################################\n");
   printf("# Trip Planner Menu #\n");
   printf("#########################################\n");
   printf(" 1 - Insert a Destination\n");
   printf(" 2 - Remove a Destination\n");
   printf(" 3 - Print Destinations\n");
   printf(" 4 - Print Itinerary\n");
   printf(" 5 - Clear Itinerary\n");
   printf(" 0 - Quit\n");
   printf("Enter your selection: ");
}

int main(void)
{
   /* declare all your variables here */
   int choice;
   struct Destination* head = NULL;

   while (TRUE)
   {
       printMenu();

       /* adapt the scanf() to handle invalid input */
       scanf("%d", &choice);

       switch (choice)
       {
       case 1:
               printf("Enter the airport code : ");
               char code[4];              
               scanf("%s", &code);
               if (head == NULL)
               {
                   head = create(code);
                   insertAfter(NULL, head, code);
                   printf("Destination Added succesfully ");
               }
               else
               {
                   struct Destination* temp = find(head, code);
                   if (temp != NULL)
                   {
                       printf("Airport Code already Exists.");
                   }
                   else
                   {
                       printf("Enter the airport code after which you want to insert new Airport code: ");
                       char key[4];
                       scanf("%s", &key);
                       temp = create(code);
                       head = insertAfter(head, temp, key);
                       printf("Destination Added succesfully ");
                   }
               }          
          
           break;
       case 2:
           printf("Enter the airport code : ");
           char coder[4];
           scanf("%s", &coder);
           if (head == NULL)
           {
               printf("Airpot code does not exists");
           }
           else
           {
               struct Destination* temp = find(head, coder);
               if (temp == NULL)
               {
                   printf("Airport Code does not Exists.");
               }
               else
               {                  
                   removeNode(head, coder);
                   printf("Destination removed succesfully ");
               }
           }
           break;
       case 3:
           print(head);
           break;
       case 4:
           printItinerary(head);
           break;
       case 5:
           head = destruct(head);
           printf("Itenary cleared");
           break;
       case 0:
           printf("Good-Bye");
           if(head != NULL)
               destruct(head);
           return 1;
       default:
           printf("Enter valid choice");
           break;
       }
   }

   return 0;
}

Add a comment
Know the answer?
Add Answer to:
***In C Programming*** TASK 1: Complete itinerary.h Create a struct Destination that represents a node in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • using C++, NOT C language 1. Write a function called insert() to insert a node to...

    using C++, NOT C language 1. Write a function called insert() to insert a node to the beginning of a linked list. The data is passed into the function. For example insert(8) will insert a node at the beginning of the list with the number 8 in the data field. Each node in the linked list is a ListNode struct as discussed in class. 2. Write a function called print() that will traverse the entire linked list and print out...

  • Sometimes it is convenient to maintain references to both the next node and the previous node...

    Sometimes it is convenient to maintain references to both the next node and the previous node in a linked list. This is called a doubly linked list and is illustrated in Figure 13.4 of the text. File DoubleLinked contains definitions for a doubly linked list of integers. This class contains an inner class IntNode that holds information for a single node in the list (its value and references to the next and previous nodes). The DoubleLinked class also contains the...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • * C PROGRAMMING* Outcomes: Demonstrate the ability to create and use linked lists in dynamic memory...

    * C PROGRAMMING* Outcomes: Demonstrate the ability to create and use linked lists in dynamic memory Demonstrate the ability to add nodes and remove nodes from a linked list of structs Program Specifications: Write a program that creates the following struct (you can give it whatever name you want): char name[100]; int age; float weight; Create the following menu system: Add a Record Display All Records Quit When the user selects (1) you will prompt them for a name, age,...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • Please answer in C++ Derive a class called Queue from the linked list described in Assignment...

    Please answer in C++ Derive a class called Queue from the linked list described in Assignment 2 (list of Dates). This means the Queue class will inherit all the properties (data and functions) of the linked list. But, since a queue allows pushing only at the back and popping at the front of the list, you will need to prevent the addition in the front and removal at the back. To do this, you must derive the Queue class in...

  • Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work....

    Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work. You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. Playlist.h - Class declaration Playlist.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1...

  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT