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

C++ Program to do Recursion in a Singly Linked List. Using a singly linked list,

ID: 3680759 • Letter: C

Question

C++ Program to do Recursion in a Singly Linked List. Using a singly linked list, support the ability to add the following information about an airport: airport code, distance from the last entered airport code to the current one. Provide a sentinel header which represents an element whose value is 'LGA' and whose distance is 0. Allow the user to provide 3 types of input: 1. Enter new airport information 2. Specify an existing airport code and calculate the distance from LGA to the selected airport using recursion. 3. Specify an existing airport code and calculate the distance from LGA to the selected airport by looping thru the nodes in standard fashion.

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

  

/* Link list node */

struct node

{

    int data;

    struct node* next;

};

  

/* Function to reverse the linked list */

void printReverse(struct node* head)

{

    // Base case

    if (head == NULL)

       return;

    // print the list after head node

    printReverse(head->next);

    // After everything else is printed, print head

    printf("%d ", head->data);

}

  

/*UTILITY FUNCTIONS*/

/* Push a node to linked list. Note that this function

  changes the head */

void push(struct node** head_ref, char new_data)

{

    /* allocate node */

    struct node* new_node =

            (struct node*) malloc(sizeof(struct node));

  

    /* put in the data */

    new_node->data = new_data;

  

    /* link the old list off the new node */

    new_node->next = (*head_ref);  

  

    /* move the head to pochar to the new node */

    (*head_ref)    = new_node;

}

  

/* Drier program to test above function*/

int main()

{

    // Let us create linked list 1->2->3->4

    struct node* head = NULL;   

    push(&head, 4);

    push(&head, 3);

    push(&head, 2);

    push(&head, 1);

   

    printReverse(head);

    return 0;

}

//hope this helps

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote