[Solved] Train Compartment with C

Train Compartment: As per the railway schedule, train has to dispatch within 10 minutes, as some of the compartments had just been from the hub, they have to be joined accordingly before it starts to the railway station.

The repaired compartments from the hub and the non-repaired compartments are in different tracks. So help the train operator to join the compartment according to the coach number as soon as possible.

When track 1 and track 2 are empty it’s not possible to merge the compartments. so, display error message as “No compartments were found.File complaint to railway enquiry”.


Write a C program to join the compartments according to the coach number.

Define a structure
struct compartment
{
    int number;
    struct compartment * link;
};

Include functions
    insert — to insert the new compartment in ascending order.
    display — to display all the compartments in a track.
    merge — to merge the compartments in the two tracks.

   void insert (struct compartment **q,int n);

    void display(struct compartment *q);

    void merge(struct compartment **p,struct compartment **q);


Refer function specifications for further details.

[All text in bold corresponds to input and the rest corresponds to output.]
Input and Output Format:
Refer sample input and output for formatting specifications.

Sample Input and Output 1:
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
1
Enter the compartment number
1
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
1
Enter the compartment number
2
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
1
Enter the compartment number
3
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
3
Compartments are:1 2 3
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
4
Track is Empty
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
2
Enter the compartment number
5
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
2
Enter the compartment number
6
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
4
Compartments are:5 6
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
5
Compartments are:1 2 3 5 6
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
6
Exit

Sample Input and Output 2:
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
3
Track is Empty
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
4
Track is Empty
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
Enter your choice
5
No compartments were found.File complaint to railway enquiry
1.Insert compartment in the first track
2.Insert compartment in the second track
3.Display track 1
4.Display track 2
5.Merge compartments
6.Exit
6
Enter your choice
Exit

Solution


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

struct compartment
{
    int number;
    struct compartment *link;
};

void insert(struct compartment **q, int n);
void display(struct compartment *q);
void merge(struct compartment **p, struct compartment **q);

void insert(struct compartment **q, int n)
{
    struct compartment *temp;
    temp = (struct compartment *)malloc(sizeof(struct compartment));
    temp->number = n;
    temp->link = NULL;
    if (*q == NULL)
    {
        *q = temp;
    }
    else
    {
        struct compartment *p;
        p = *q;
        while (p->link != NULL)
        {
            p = p->link;
        }
        p->link = temp;
    }
}

void display(struct compartment *q)
{
    if (q == NULL)
    {
        printf("Track is Empty\n");
    }
    else
    {
        printf("Compartments are:");
        while (q != NULL)
        {
            printf("%d ", q->number);
            q = q->link;
        }
        printf("\n");
    }
}

void merge(struct compartment **p, struct compartment **q)
{
    struct compartment *temp;
    temp = (struct compartment *)malloc(sizeof(struct compartment));
    temp->number = 0;
    temp->link = NULL;
    // When track 1 and track 2 are empty it's not possible to merge the compartments. so, display error message as "No compartments were found.File complaint to railway enquiry".

    if (*p == NULL && *q == NULL)
    {
        printf("No compartments were found.File complaint to railway enquiry\n");
    }
    else
    {
        printf("Compartments are:");
        while (*p != NULL && *q != NULL)
        {
            if ((*p)->number < (*q)->number)
            {
                temp->link = *p;
                *p = (*p)->link;
            }
            else
            {
                temp->link = *q;
                *q = (*q)->link;
            }
            temp = temp->link;
        }
        if (*p == NULL)
        {
            temp->link = *q;
        }
        else
        {
            temp->link = *p;
        }
        *p = temp->link;
        temp->link = NULL;

        while (temp->link != NULL)
        {
            printf("%d ", temp->number);
            temp = temp->link;
        }
        printf("%d\n", temp->number);
    }
}



int main()
{
    int choice;
    struct compartment *p, *q;
    p = q = NULL;
    do
    {
        printf("1.Insert compartment in the first track\n");
        printf("2.Insert compartment in the second track\n");
        printf("3.Display track 1\n");
        printf("4.Display track 2\n");
        printf("5.Merge compartments\n");
        printf("6.Exit\n");
        printf("Enter your choice\n");
        scanf("%d", &choice);
        // When track 1 and track 2 are empty it's not possible to merge the compartments. so, display error message as "No compartments were found.File complaint to railway enquiry". return 0;

        if (choice == 1)
        {
            int n;
            printf("Enter the compartment number\n");
            scanf("%d", &n);
            insert(&p, n);
        }
        else if (choice == 2)
        {
            int n;
            printf("Enter the compartment number\n");
            scanf("%d", &n);
            insert(&q, n);
        }
        else if (choice == 3)
        {
            display(p);
        }
        else if (choice == 4)
        {
            display(q);
        }
        else if (choice == 5)
        {
            merge(&p, &q);
        }
        else if (choice == 6)
        {
            printf("Exit\n");
        }
        else
        {
            printf("Invalid choice\n");
        }
    } while (choice != 6);
    return 0;
    
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *