Practical 3. Program based on linked list: Maintain a record of coffee shop database implement following: i. Display the total sale of a day ii. Maximum and minimum bill of a day
#include <stdio.h>
#include <stdlib.h>
// Define a node structure
struct Node {
int data; // Store the bill amount
struct Node* next; // Pointer to the next node
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to append a node to the linked list
void append(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// Function to calculate the total sales of the day
int calculateTotalSales(struct Node* head) {
int total = 0;
struct Node* current = head;
while (current != NULL) {
total += current->data;
current = current->next;
}
return total;
}
// Function to find the maximum and minimum bill of the day
void findMaxMinBill(struct Node* head, int* maxBill, int* minBill) {
if (head == NULL) {
*maxBill = *minBill = 0;
return;
}
*maxBill = head->data;
*minBill = head->data;
struct Node* current = head->next;
while (current != NULL) {
if (current->data > *maxBill) {
*maxBill = current->data;
}
if (current->data < *minBill) {
*minBill = current->data;
}
current = current->next;
}
}
int main() {
struct Node* sales = NULL;
int choice, amount;
int maxBill, minBill;
do {
printf("\nMenu:\n");
printf("1. Add a transaction\n");
printf("2. Calculate total sales\n");
printf("3. Find maximum and minimum bill\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the bill amount: ");
scanf("%d", &amount);
append(&sales, amount);
printf("Transaction added.\n");
break;
case 2:
printf("Total sales of the day: %d\n", calculateTotalSales(sales));
break;
case 3:
findMaxMinBill(sales, &maxBill, &minBill);
if (maxBill == 0 && minBill == 0) {
printf("No transactions recorded yet.\n");
} else {
printf("Maximum bill of the day: %d\n", maxBill);
printf("Minimum bill of the day: %d\n", minBill);
}
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 4);
return 0;
}
Practical 3. Program based on linked list:
Maintain a record of coffee shop database implement following:
i. Display the total sale of a day
ii. Maximum and minimum bill of a day
Reviewed by RISHI and ARYAN
on
21:21
Rating:

No comments:
May I help you ?