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

C++ Sda

The document defines Stack and Queue template classes to store and manipulate generic data types. Both classes use a fixed-size array to store elements. Methods are provided to add/remove elements, check status, and access elements. Exceptions are defined for invalid operations like adding to a full or empty container.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

C++ Sda

The document defines Stack and Queue template classes to store and manipulate generic data types. Both classes use a fixed-size array to store elements. Methods are provided to add/remove elements, check status, and access elements. Exceptions are defined for invalid operations like adding to a full or empty container.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <stdexcept>

using namespace std;

template <typename T>


class Stack {
private:
T* data;
int size;
int capacity;

public:
Stack(int capacity = 10) : size(0), capacity(capacity) {
data = new T[capacity];
}

~Stack() {
delete[] data;
}

void push(const T& value) {


if (size == capacity) {
throw overflow_error("Stack is full");
}
data[size++] = value;
}

void pop() {
if (size == 0) {
throw underflow_error("Stack is empty");
}
size--;
}

T& top() const {


if (size == 0) {
throw underflow_error("Stack is empty");
}
return data[size - 1];
}

bool isEmpty() const {


return size == 0;
}

bool isFull() const {


return size == capacity;
}

int getSize() const {


return size;
}

int getCapacity() const {


return capacity;
}

T& operator[](int index) const {


if (index < 0 || index >= size) {
throw out_of_range("Invalid index");
}
return data[index];
}
};

template <typename T>


class Queue {
private:
T* data;
int head;
int tail;
int size;
int capacity;

public:
Queue(int capacity = 10) : head(0), tail(0), size(0), capacity(capacity) {
data = new T[capacity];
}

~Queue() {
delete[] data;
}

void enqueue(const T& value) {


if (size == capacity) {
throw overflow_error("Queue is full");
}
data[tail] = value;
tail = (tail + 1) % capacity;
size++;
}

void dequeue() {
if (size == 0) {
throw underflow_error("Queue is empty");
}
head = (head + 1) % capacity;
size--;
}

T& front() const {


if (size == 0) {
throw underflow_error("Queue is empty");
}
return data[head];
}

bool isEmpty() const {


return size == 0;
}

bool isFull() const {


return size == capacity;
}

int getSize() const {


return size;
}

int getCapacity() const {


return capacity;
}

T& operator[](int index) const {


if (index < 0 || index >= size) {
throw out_of_range("Invalid index");
}
return data[(head + index) % capacity];
}
};

class NegativeSizeException : public exception {


public:
virtual const char* what() const throw() {
return "Stack/Queue size cannot be negative";
}
};

class ElementNotFoundException : public exception {


public:
virtual const char* what() const throw() {
return "Element not found in Stack/Queue";
}
};

class FullCapacityException : public exception {


public:
virtual const char* what() const throw() {
return "Capacity of Stack/Queue is already full";
}
};

template <typename T>


class Stack {
private:
T* data;
int size;
int capacity;

public:
Stack(int capacity = 10) : size(0), capacity(capacity) {
if (capacity < 0) {
throw NegativeSizeException();
}
data = new T[capacity];
}

~Stack() {
delete[] data;
}

void push(const T& value) {


if (size == capacity) {
throw FullCapacityException();
}
data[size++] = value;
}

void pop() {
if (size == 0) {
throw underflow_error("Stack is empty");
}
size--;
}

T& top() const {


if (size == 0) {
throw underflow_error("Stack is empty");
}
return data[size - 1];
}

bool isEmpty() const {


return size == 0;
}

bool isFull() const {


return size == capacity;
}

int getSize() const {


return size;
}

int getCapacity() const {


return capacity;
}

T& operator[](int index) const {


if (index < 0 || index >= size) {
throw out_of_range("Invalid index");
}
return data[index];
}
};

template <typename T>


class Queue {
private:
T* data;
int head;
int tail;
int size;
int capacity;

public:
Queue(int capacity = 10) : head(0), tail(0), size(0), capacity(capacity) {
if (capacity < 0) {
throw NegativeSizeException();
}
data = new T[capacity];
}

~Queue() {
delete[] data;
}

void enqueue(const T& value) {


if (size == capacity) {
throw FullCapacityException();
}
data[tail] = value;
tail = (tail + 1) % capacity;
size++;
}

void dequeue() {
if (size == 0) {
throw underflow_error("Queue is empty");
}
head = (head + 1) % capacity;
size--;
}

T& front() const {


if (size == 0) {
throw underflow_error("Queue is empty");
}
return data[head];
}

bool isEmpty() const {


return size == 0;
}

bool isFull() const {


return size == capacity;
}

int getSize() const {


return size;
}

int getCapacity() const {


return capacity;
}

T& operator[](int index) const {


if (index < 0 || index >= size) {
throw ElementNotFoundException();
}
return data[(head + index) % capacity];

You might also like