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

#define MAX_SIZE 100

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;
}

// Clear the Bag
void clear(Bag *bag) {
    bag->size = 0;
}

// Remove the last item from the Bag
void removeLast(Bag *bag) {
    if (!isEmpty(bag)) {
        bag->size--;
    } else {
        printf("Bag is empty, cannot remove item.\n");
    }
}

// Remove a specific item from the Bag
void removeItem(Bag *bag, int item) {
    for (int i = 0; i < bag->size; i++) {
        if (bag->data[i] == item) {
            for (int j = i; j < bag->size - 1; j++) {
                bag->data[j] = bag->data[j + 1];
            }
            bag->size--;
            return;
        }
    }
    printf("Item not found in the Bag.\n");
}

// 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);

    // Remove an item and print again
    removeItem(&bag, 10);
    printBag(&bag);

    // Remove the last item and print again
    removeLast(&bag);
    printBag(&bag);

    // Clear the bag and print again
    clear(&bag);
    printBag(&bag);

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: