Data Structures Implementation

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

// Implementation of Stacks and Queues using Arrays

#include <iostream>
using namespace std;

// Stack implementation using Array


class ArrayStack {
private:
int* arr;
int top;
int capacity;

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

// Queue implementation using Array


class ArrayQueue {
private:
int* arr;
int front, rear;
int capacity;

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

// Linked List Node


class Node {
public:
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

// Stack implementation using Linked List


class LinkedStack {
private:
Node* top;

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

// Queue implementation using Linked List


class LinkedQueue {
private:
Node *front, *rear;

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

// Singly Linked List with all operations


class LinkedList {
private:
Node* head;

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

Node* temp = head;


while (temp->next != nullptr && temp->next->data != value) {
temp = temp->next;
}

if (temp->next == nullptr) return;

Node* toDelete = temp->next;


temp->next = temp->next->next;
delete toDelete;
}

// Update node value


void updateNode(int oldValue, int newValue) {
Node* temp = head;
while (temp != nullptr) {
if (temp->data == oldValue) {
temp->data = newValue;
return;
}
temp = temp->next;
}
cout << "Value not found\n";
}

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

// Sort list (using bubble sort)


void sort() {
if (head == nullptr || head->next == nullptr) return;

bool swapped;
Node* ptr1;
Node* lptr = nullptr;

do {
swapped = false;
ptr1 = head;

while (ptr1->next != lptr) {


if (ptr1->data > ptr1->next->data) {
int temp = ptr1->data;
ptr1->data = ptr1->next->data;
ptr1->next->data = temp;
swapped = true;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
};

// Binary Search Tree Implementation


class BSTNode {
public:
int data;
BSTNode *left, *right;

BSTNode(int value) {
data = value;
left = right = nullptr;
}
};

class BST {
private:
BSTNode* root;

BSTNode* insertRec(BSTNode* node, int value) {


if (node == nullptr) return new BSTNode(value);

if (value < node->data)


node->left = insertRec(node->left, value);
else if (value > node->data)
node->right = insertRec(node->right, value);
return node;
}

BSTNode* minValueNode(BSTNode* node) {


BSTNode* current = node;
while (current && current->left != nullptr)
current = current->left;
return current;
}

BSTNode* deleteRec(BSTNode* root, int value) {


if (root == nullptr) return root;

if (value < root->data)


root->left = deleteRec(root->left, value);
else if (value > root->data)
root->right = deleteRec(root->right, value);
else {
if (root->left == nullptr) {
BSTNode* temp = root->right;
delete root;
return temp;
}
else if (root->right == nullptr) {
BSTNode* temp = root->left;
delete root;
return temp;
}

BSTNode* temp = minValueNode(root->right);


root->data = temp->data;
root->right = deleteRec(root->right, temp->data);
}
return root;
}

bool searchRec(BSTNode* root, int value) {


if (root == nullptr || root->data == value)
return root != nullptr;

if (value < root->data)


return searchRec(root->left, value);

return searchRec(root->right, value);


}

void inorderRec(BSTNode* root) {


if (root != nullptr) {
inorderRec(root->left);
cout << root->data << " ";
inorderRec(root->right);
}
}

void preorderRec(BSTNode* root) {


if (root != nullptr) {
cout << root->data << " ";
preorderRec(root->left);
preorderRec(root->right);
}
}

void postorderRec(BSTNode* root) {


if (root != nullptr) {
postorderRec(root->left);
postorderRec(root->right);
cout << root->data << " ";
}
}

public:
BST() {
root = nullptr;
}

void insert(int value) {


root = insertRec(root, value);
}

void remove(int value) {


root = deleteRec(root, value);
}

bool search(int value) {


return searchRec(root, value);
}

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

int predecessor(int value) {


BSTNode* current = root;
BSTNode* pred = nullptr;

while (current != nullptr) {


if (value < current->data)
current = current->left;
else if (value > current->data) {
pred = current;
current = current->right;
}
else {
if (current->left != nullptr) {
pred = current->left;
while (pred->right != nullptr)
pred = pred->right;
}
break;
}
}

return pred ? pred->data : -1;


}

int successor(int value) {


BSTNode* current = root;
BSTNode* succ = nullptr;

while (current != nullptr) {


if (value < current->data) {
succ = current;
current = current->left;
}
else if (value > current->data)
current = current->right;
else {
if (current->right != nullptr) {
succ = current->right;
while (succ->left != nullptr)
succ = succ->left;
}
break;
}
}

return succ ? succ->data : -1;


}

void inorder() {
inorderRec(root);
cout << endl;
}

void preorder() {
preorderRec(root);
cout << endl;
}

void postorder() {
postorderRec(root);
cout << endl;
}
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy