#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 50
// Define a structure for the Bag
typedef struct {
int data[MAX_SIZE]; // Array to store the elements
int size; // Current number of elements in the Bag
} Bag;
// Initialize the Bag
void initBag(Bag *bag) {
bag->size = 0; // Initialize size to 0
}
// Add an item to the Bag
void addToBag(Bag *bag, int item) {
if (bag->size < MAX_SIZE) { // Check if Bag is full
bag->data[bag->size++] = item; // Add item to the Bag and increment size
} else {
printf("Bag is full, cannot add more items.\n"); // Print message if Bag is full
}
}
// Get the current size of the Bag
int getCurrentSize(Bag *bag) {
return bag->size;
}
// Check if the Bag is full
int isFull(Bag *bag) {
return bag->size == MAX_SIZE;
}
// Check if the Bag is empty
int isEmpty(Bag *bag) {
return bag->size == 0;
}
// Print the contents of the Bag
void printBag(Bag *bag) {
printf("Bag contents: ");
for (int i = 0; i < bag->size; i++) {
printf("%d ", bag->data[i]); // Print each element of the Bag
}
printf("\n");
}
int main() {
Bag bag;
initBag(&bag); // Initialize the Bag
// Add items to the Bag
addToBag(&bag, 5);
addToBag(&bag, 10);
addToBag(&bag, 5);
addToBag(&bag, 20);
// Print the contents of the Bag
printBag(&bag);
// Print the current size, and check if the Bag is full or empty
printf("Current size: %d\n", getCurrentSize(&bag));
printf("Is full: %s\n", isFull(&bag) ? "Yes" : "No");
printf("Is empty: %s\n", isEmpty(&bag) ? "Yes" : "No");
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: