Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- // Vector Class
- template <typename T>
- class Vector {
- private:
- T* data;
- size_t capacity;
- size_t size;
- void resize(size_t new_capacity) {
- T* new_data = new T[new_capacity];
- for (size_t i = 0; i < size; ++i) {
- new_data[i] = data[i];
- }
- delete[] data;
- data = new_data;
- capacity = new_capacity;
- }
- public:
- Vector() : data(nullptr), capacity(0), size(0) {}
- ~Vector() {
- delete[] data;
- }
- void push_back(const T& value) {
- if (size == capacity) {
- resize(capacity == 0 ? 1 : 2 * capacity);
- }
- data[size++] = value;
- }
- void pop_back() {
- if (size > 0) {
- --size;
- }
- }
- T& operator[](size_t index) {
- return data[index];
- }
- const T& operator[](size_t index) const {
- return data[index];
- }
- size_t getSize() const {
- return size;
- }
- };
- // Queue Class
- template <typename T>
- class Queue {
- private:
- struct Node {
- T data;
- Node* next;
- Node(const T& data) : data(data), next(nullptr) {}
- };
- Node* front;
- Node* back;
- size_t size;
- public:
- Queue() : front(nullptr), back(nullptr), size(0) {}
- ~Queue() {
- while (!isEmpty()) {
- dequeue();
- }
- }
- void enqueue(const T& value) {
- Node* newNode = new Node(value);
- if (isEmpty()) {
- front = back = newNode;
- } else {
- back->next = newNode;
- back = newNode;
- }
- ++size;
- }
- void dequeue() {
- if (!isEmpty()) {
- Node* temp = front;
- front = front->next;
- delete temp;
- --size;
- }
- }
- T& getFront() {
- return front->data;
- }
- const T& getFront() const {
- return front->data;
- }
- bool isEmpty() const {
- return size == 0;
- }
- size_t getSize() const {
- return size;
- }
- };
- // Stack Class
- template <typename T>
- class Stack {
- private:
- struct Node {
- T data;
- Node* next;
- Node(const T& data) : data(data), next(nullptr) {}
- };
- Node* top;
- size_t size;
- public:
- Stack() : top(nullptr), size(0) {}
- ~Stack() {
- while (!isEmpty()) {
- pop();
- }
- }
- void push(const T& value) {
- Node* newNode = new Node(value);
- newNode->next = top;
- top = newNode;
- ++size;
- }
- void pop() {
- if (!isEmpty()) {
- Node* temp = top;
- top = top->next;
- delete temp;
- --size;
- }
- }
- T& peek() {
- return top->data;
- }
- const T& peek() const {
- return top->data;
- }
- bool isEmpty() const {
- return size == 0;
- }
- size_t getSize() const {
- return size;
- }
- };
- int main() {
- // Vector example
- Vector<int> vec;
- vec.push_back(1);
- vec.push_back(2);
- vec.push_back(3);
- std::cout << "Vector: ";
- for (size_t i = 0; i < vec.getSize(); ++i) {
- std::cout << vec[i] << " ";
- }
- std::cout << std::endl;
- // Queue example
- Queue<int> q;
- q.enqueue(1);
- q.enqueue(2);
- q.enqueue(3);
- std::cout << "Queue: ";
- while (!q.isEmpty()) {
- std::cout << q.getFront() << " ";
- q.dequeue();
- }
- std::cout << std::endl;
- // Stack example
- Stack<int> stack;
- stack.push(1);
- stack.push(2);
- stack.push(3);
- std::cout << "Stack: ";
- while (!stack.isEmpty()) {
- std::cout << stack.peek() << " ";
- stack.pop();
- }
- std::cout << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment