Data Structures Implementation
Data Structures Implementation
Data Structures Implementation
#include <iostream>
using namespace std;
public:
ArrayStack(int size = 100) {
arr = new int[size];
capacity = size;
top = -1;
}
~ArrayStack() {
delete[] arr;
}
void push(int x) {
if (top >= capacity - 1) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = x;
}
int pop() {
if (top < 0) {
cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}
int peek() {
if (top < 0) {
cout << "Stack is Empty\n";
return -1;
}
return arr[top];
}
bool isEmpty() {
return top < 0;
}
};
public:
ArrayQueue(int size = 100) {
arr = new int[size];
capacity = size;
front = rear = -1;
}
~ArrayQueue() {
delete[] arr;
}
void enqueue(int x) {
if (rear >= capacity - 1) {
cout << "Queue Overflow\n";
return;
}
if (front == -1) front = 0;
arr[++rear] = x;
}
int dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow\n";
return -1;
}
return arr[front++];
}
int peek() {
if (front == -1 || front > rear) {
cout << "Queue is Empty\n";
return -1;
}
return arr[front];
}
bool isEmpty() {
return front == -1 || front > rear;
}
};
Node(int value) {
data = value;
next = nullptr;
}
};
public:
LinkedStack() {
top = nullptr;
}
void push(int x) {
Node* newNode = new Node(x);
newNode->next = top;
top = newNode;
}
int pop() {
if (isEmpty()) {
cout << "Stack Underflow\n";
return -1;
}
Node* temp = top;
int value = temp->data;
top = top->next;
delete temp;
return value;
}
bool isEmpty() {
return top == nullptr;
}
};
public:
LinkedQueue() {
front = rear = nullptr;
}
void enqueue(int x) {
Node* newNode = new Node(x);
if (rear == nullptr) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
int dequeue() {
if (isEmpty()) {
cout << "Queue Underflow\n";
return -1;
}
Node* temp = front;
int value = temp->data;
front = front->next;
if (front == nullptr) rear = nullptr;
delete temp;
return value;
}
bool isEmpty() {
return front == nullptr;
}
};
public:
LinkedList() {
head = nullptr;
}
// Insert at beginning
void insertAtBeginning(int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
}
// Insert at end
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
// Insert at position
void insertAtPosition(int value, int position) {
if (position < 0) {
cout << "Invalid position\n";
return;
}
if (position == 0) {
insertAtBeginning(value);
return;
}
Node* newNode = new Node(value);
Node* temp = head;
for (int i = 0; i < position - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) {
cout << "Position out of range\n";
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Delete a node with given value
void deleteNode(int value) {
if (head == nullptr) return;
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
// Search element
bool search(int value) {
Node* temp = head;
while (temp != nullptr) {
if (temp->data == value) return true;
temp = temp->next;
}
return false;
}
// Display list
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Reverse list
void reverse() {
Node *prev = nullptr, *current = head, *next = nullptr;
while (current != nullptr) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
bool swapped;
Node* ptr1;
Node* lptr = nullptr;
do {
swapped = false;
ptr1 = head;
BSTNode(int value) {
data = value;
left = right = nullptr;
}
};
class BST {
private:
BSTNode* root;
public:
BST() {
root = nullptr;
}
int findMax() {
if (root == nullptr) return -1;
BSTNode* current = root;
while (current->right != nullptr)
current = current->right;
return current->data;
}
int findMin() {
if (root == nullptr) return -1;
BSTNode* current = root;
while (current->left != nullptr)
current = current->left;
return current->data;
}
void inorder() {
inorderRec(root);
cout << endl;
}
void preorder() {
preorderRec(root);
cout << endl;
}
void postorder() {
postorderRec(root);
cout << endl;
}
};