Followers

Showing posts with label Array Implementation. Show all posts
Showing posts with label Array Implementation. Show all posts

Wednesday, July 17, 2024

An implementation of queue operations using an array in C++


 

#include <iostream>

using namespace std;

class Queue {

private:

    int front, rear, size;

    int* queueArray;

    int capacity;

public:

    Queue(int capacity) {

        this->capacity = capacity;

        queueArray = new int[capacity];

        front = size = 0;

        rear = capacity - 1;

    }

    ~Queue() {

        delete[] queueArray;

    }

    bool isFull() {

        return (size == capacity);

    }

    bool isEmpty() {

        return (size == 0);

    }

    void enqueue(int item) {

        if (isFull()) {

            cout << "Queue overflow! Cannot enqueue " << item << endl;

            return;

        }

        rear = (rear + 1) % capacity;

        queueArray[rear] = item;

        size++;

        cout << item << " enqueued to queue" << endl;

    }

    int dequeue() {

        if (isEmpty()) {

            cout << "Queue underflow! Cannot dequeue" << endl;

            return -1; // Assuming -1 as error value for queue underflow

        }

        int item = queueArray[front];

        front = (front + 1) % capacity;

        size--;

        return item;

    }


    int frontElement() {

        if (isEmpty()) {

            cout << "Queue is empty! Cannot retrieve front element" << endl;

            return -1; // Assuming -1 as error value for empty queue

        }

        return queueArray[front];

    }

    int rearElement() {

        if (isEmpty()) {

            cout << "Queue is empty! Cannot retrieve rear element" << endl;

            return -1; // Assuming -1 as error value for empty queue

        }

        return queueArray[rear];

    }

};

int main() {

    int capacity;

    cout << "Enter the capacity of the queue: ";

    cin >> capacity;

    Queue queue(capacity);

    queue.enqueue(10);

    queue.enqueue(20);

    queue.enqueue(30);

    queue.enqueue(40);

    cout << "Front element is: " << queue.frontElement() << endl;

    cout << "Rear element is: " << queue.rearElement() << endl;

    cout << "Dequeued element: " << queue.dequeue() << endl;

    cout << "Front element after dequeue: " << queue.frontElement() << endl;

    // Attempt to dequeue from empty queue

    while (!queue.isEmpty()) {

        queue.dequeue();

    }

    queue.dequeue();

    return 0;

}


Explanation:

  1. Queue Class:

    • Contains a constructor to initialize the queue with a given capacity.
    • Contains a destructor to deallocate the memory used by the queue.
    • Implements isFull to check if the queue is full.
    • Implements isEmpty to check if the queue is empty.
    • Implements enqueue to add an element to the queue.
    • Implements dequeue to remove and return the front element from the queue.
    • Implements frontElement to return the front element without removing it.
    • Implements rearElement to return the rear element without removing it.
  2. Main Function:

    • Takes the capacity of the queue from the user.
    • Demonstrates basic queue operations (enqueue, dequeue, frontElement, rearElement).

An implementation of stack operations using an array in C++.


 

    # include <iostream>

    using namespace std;

    class Stack {

        private:

        int top;

        int maxSize;

        int* stackArray;

        public:

        Stack(int size) {

        maxSize = size;

        stackArray = new int[maxSize];

        top = -1;

        }

        ~Stack() {

        delete[] stackArray;

        }

        void push(int value) {

            if (isFull()) {

                cout << "Stack overflow! Cannot push " << value << endl;

            } else {

                stackArray[++top] = value;

            }

        }

    int pop() {

        if (isEmpty()) {

            cout << "Stack underflow! Cannot pop" << endl;

            return -1; // Assuming -1 as error value for stack underflow

        } else {

            return stackArray[top--];

        }

    }

    int peek() {

        if (isEmpty()) {

            cout << "Stack is empty! Cannot peek" << endl;

            return -1; // Assuming -1 as error value for empty stack

            } else {

            return stackArray[top];

            }

        }

        bool isEmpty() {

            return top == -1;

        }

        bool isFull() {

        return top == maxSize - 1;

        }

    };


    int main() {

        int size;

        cout << "Enter the size of the stack: ";

        cin >> size;

        Stack stack(size);

        stack.push(10);

        stack.push(20);

        stack.push(30);

        cout << "Top element is: " << stack.peek() << endl;

        cout << "Elements popped from stack: " << stack.pop() << ", " << stack.pop() << ", " <<  stack.pop() << endl;

    // Attempt to pop from empty stack

        stack.pop();

        return 0;

    }


  • 1. Stack Class:

    • Contains a constructor to initialise the stack with a given size.
    • Contains a destructor to deallocate the memory used by the stack.
    • Implements push to add an element to the stack.
    • Implements pop to remove and return the top element from the stack.
    • Implements peek to return the top element without removing it.
    • Implements isEmpty to check if the stack is empty.
    • Implements isFull to check if the stack is full.
  • 2. Main Function:

    • Takes the size of the stack from the user.
    • Demonstrates basic stack operations (push, pop, peek).