DSA Project – Traffic Light Simulation

Learn DSA & Get Ready for MAANG Companies Start Now!!

Program 1

// Project: Traffic Light Simulation Using Queue Linked List 
//----------------------------------------------------------
// Features:
// Add vehicles to queue (simulating arrival)
// Process vehicles when the signal turns green
// Show vehicles in the queue
// Count waiting vehicles
// Clear All Vehicles (Emergency Clear Signal)
//Search for a Vehicle by Number
//Display First and Last Vehicle in Queue
//Log of Passed Vehicles

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

#define SIZE 100

typedef struct Vehicle   // Node 
{
    char number[20];  
    struct Vehicle* next;
} Vehicle;

Vehicle* front = NULL;
Vehicle* rear = NULL;
Vehicle* passedLog = NULL;  // Passed vehicle log

// Enqueue vehicle
void arriveVehicle(const char* number) 
{
    Vehicle* temp = (Vehicle*)malloc(sizeof(Vehicle));
    strcpy(temp->number, number);
    temp->next = NULL;

    if (rear == NULL) 
    {
        front = rear = temp;
    } else 
    {
        rear->next = temp;
        rear = temp;
    }
    printf("\n Vehicle %s arrived at the signal.\n", number);
}

// Add vehicle to log
void logPassedVehicle(const char* number) 
{
    Vehicle* temp = (Vehicle*)malloc(sizeof(Vehicle));
    strcpy(temp->number, number);
    temp->next = NULL;

    if (passedLog == NULL) 
    {
        passedLog = temp;
    } else 
    {
        Vehicle* ptr = passedLog;
        while (ptr->next != NULL)
            ptr = ptr->next;
        ptr->next = temp;
    }
}

// Dequeue vehicle
void passVehicle()
 {
    if (front == NULL) 
    {
        printf("\n No vehicles waiting at the signal.\n");
        return;
    }

    Vehicle* temp = front;
    printf("\n Vehicle %s passed the signal.\n", temp->number);
    logPassedVehicle(temp->number);
    front = front->next;

    if (front == NULL)
       front=rear = NULL;

    free(temp);
}

// View all vehicles waiting
void displayQueue() 
{
    if (front == NULL) 
    {
        printf("\nNo vehicles in the queue.\n");
        return;
    }

    printf("\n Vehicles at signal:\n");
    Vehicle* temp = front;
    while (temp != NULL) 
    {
        printf("  -> %s\n", temp->number);
        temp = temp->next;
    }
}

// Count waiting vehicles
void countQueue() 
{
    int count = 0;
    Vehicle* temp = front;
    while (temp != NULL) 
    {
        count++;
        temp = temp->next;
    }
    printf("\nTotal vehicles waiting: %d\n", count);
}

// Emergency clear
void clearQueue() 
{
    while (front != NULL) 
    {
        Vehicle* temp = front;
        front = front->next;
        free(temp);
    }
    rear = NULL;
    printf("\nAll vehicles cleared from the signal!\n");
}

// Search for a vehicle
void searchVehicle(const char* number)
 {

    Vehicle* temp = front;
      while (temp != NULL) 
     {
        if (strcmp(temp->number, number) == 0) 
        {
            printf("\n Vehicle %s is currently in the queue.\n", number);
            return;
        }
        temp = temp->next;
     }
     printf("\n Vehicle %s not found in queue.\n", number);
}

// Show first and last vehicle
void showFirstAndLast() 
{
    if (front == NULL) {
        printf("\n No vehicles in the queue.\n");
        return;
    }
    printf("\nFirst Vehicle: %s\n", front->number);
    printf("Last Vehicle: %s\n", rear->number);
}

// Show passed vehicle log
void showPassedLog() 
{
    if (passedLog == NULL) 
    {
        printf("\nNo vehicles have passed the signal yet.\n");
        return;
    }
    printf("\nPassed Vehicles Log:\n");
    Vehicle* temp = passedLog;
    while (temp != NULL) 
    {
        printf("  -> %s\n", temp->number);
        temp = temp->next;
    }
}

// Menu
void menu() {
    int choice;
    char number[20];

    do {
        printf("\n========== Traffic Light Simulation ==========");
        printf("\n1. Vehicle Arrives");
        printf("\n2. Signal Turns Green (Vehicle Passes)");
        printf("\n3. Show Vehicles in Queue");
        printf("\n4. Count Waiting Vehicles");
        printf("\n5. Emergency Clear All Vehicles");
        printf("\n6. Search Vehicle by Number");
        printf("\n7. Show First and Last Vehicle");
        printf("\n8. Show Passed Vehicle Log");
        printf("\n9. Exit\n");
       printf("\n=====================================\n");  
        printf("Choose an option: ");
        scanf("%d", &choice);
        getchar(); // clear newline

        switch (choice)
         {
            case 1:
                printf("Enter vehicle number: ");
                fgets(number, sizeof(number), stdin);
                number[strcspn(number, "\n")] = 0;
                arriveVehicle(number);
                break;
            case 2:
                passVehicle();
                break;
            case 3:
                displayQueue();
                break;
            case 4:
                countQueue();
                break;
            case 5:
                clearQueue();
                break;
            case 6:
              if(front==NULL) 
               printf("No vehicle in queue........");
               else
               {
                 printf("Enter vehicle number to search: ");
                 fgets(number, sizeof(number), stdin);
                 number[strcspn(number, "\n")] = 0;
                 searchVehicle(number);
               }  
                break;
            case 7:
                showFirstAndLast();
                break;
            case 8:
                showPassedLog();
                break;
            case 9:
                printf("\nExiting simulation. Goodbye!\n");
                break;
            default:
                printf("\nInvalid option. Try again.\n");
        }
    } while (choice != 9);
}

int main() {
    menu();
    return 0;
}

 

Your opinion matters
Please write your valuable feedback about DataFlair on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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