0% found this document useful (0 votes)
3 views

Week 4

Uploaded by

gumgumy 7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Week 4

Uploaded by

gumgumy 7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/////////File: global.

#ifndef GLOBAL_H
#define GLOBAL_H

#define MAX_SIZE 100

typedef struct {
char name[50];
int id;
} customer_t;

#endif

/////////File: stack.h

#ifndef STACK_H
#define STACK_H

#include "global.h"

void push(customer_t customer);


customer_t pop();
int is_empty();

#endif

////////File: stack.c

#include "stack.h"

customer_t stack[MAX_SIZE];
int top = -1;

void push(customer_t customer) {


if (top == MAX_SIZE - 1) {
printf("Stack overflow!\n");
return;
}
stack[++top] = customer;
}

customer_t pop() {
if (top == -1) {
printf("Stack underflow!\n");
customer_t empty_customer = {"", 0};
return empty_customer;
}
return stack[top--];
}

int is_empty() {
return top == -1;
}

/////////File: queue.h
#ifndef QUEUE_H
#define QUEUE_H

#include "global.h"

void enqueue(customer_t customer);


customer_t dequeue();
int is_empty_queue();

#endif

/////////File: queue.c

#include "queue.h"

customer_t queue[MAX_SIZE];
int front = -1, rear = -1;

void enqueue(customer_t customer) {


if (rear == MAX_SIZE - 1) {
printf("Queue overflow!\n");
return;
}
if (front == -1) front = 0;
queue[++rear] = customer;
}

customer_t dequeue() {
if (front == -1 || front > rear) {
printf("Queue underflow!\n");
customer_t empty_customer = {"", 0};
return empty_customer;
}
customer_t customer = queue[front++];
if (front > rear) front = rear = -1;
return customer;
}

int is_empty_queue() {
return front == -1;
}

//////////File: main.c

#include <stdio.h>
#include "stack.h"
#include "queue.h"

void display_customers() {
if (is_empty_queue()) {
printf("No customers waiting.\n");
return;
}
printf("Customers waiting:\n");
for (int i = front; i <= rear; i++) {
printf("Name: %s, ID: %d\n", queue[i].name, queue[i].id);
}
}

void display_recent_order() {
if (is_empty_queue()) {
printf("No customers waiting.\n");
return;
}
printf("Customers in most-recent order:\n");
for (int i = rear; i >= front; i--) {
printf("Name: %s, ID: %d\n", queue[i].name, queue[i].id);
}
}

int main() {
int choice;
customer_t customer;

while (1) {
printf("\nCar Workshop Menu:\n");
printf("1. Add a New Customer.\n");
printf("2. Serve a Customer.\n");
printf("3. Display Customers Information.\n");
printf("4. Display Customers in the most-recent Order.\n");
printf("5. Exit menu\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter customer name: ");
scanf("%s", customer.name);
printf("Enter customer ID: ");
scanf("%d", &customer.id);
enqueue(customer);
break;
case 2:
customer = dequeue();
if (customer.id != 0) {
printf("Served customer: %s (ID: %d)\n", customer.name,
customer.id);
}
break;
case 3:
display_customers();
break;
case 4:
display_recent_order();
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice!\n");
}
}
}

You might also like