Data Structure Codes
Data Structure Codes
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
class SinglyLinkedList
{
private:
Node* head;
public:
SinglyLinkedList()
{
head = NULL;
}
void insertAtBeginning(int x)
{
Node* newNode = new Node();
newNode->data = x;
newNode->next = head;
head = newNode;
}
void insertAtEnd(int x)
{
Node* newNode = new Node();
newNode->data = x;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
void deleteFromBeginning()
{
if (head == NULL)
{
cout << "List is empty!\n";
return;
}
Node* temp = head;
head = head->next;
delete temp;
}
void deleteFromEnd()
{
if (head == NULL)
{
cout << "List is empty!\n";
return;
}
if (head->next == NULL)
{
delete head;
head = NULL;
return;
}
Node* temp = head;
while (temp->next->next != NULL)
{
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
}
void search(int x)
{
Node* temp = head;
int pos = 0;
while (temp != NULL)
{
if (temp->data == x)
{
cout << "Element " << x << " found at position " << pos << endl;
return;
}
temp = temp->next;
pos++;
}
cout << "Element not found\n";
}
void display()
{
if (head == NULL)
{
cout << "List is empty\n";
return;
}
Node* temp = head;
while (temp != NULL)
{
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL\n";
}
};
int main()
{
SinglyLinkedList list;
int choice, value;
do {
cout << "\nMENU:\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Delete from Beginning\n";
cout << "4. Delete from End\n";
cout << "5. Search for an Element\n";
cout << "6. Display List\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value to insert at beginning: ";
cin >> value;
list.insertAtBeginning(value);
break;
case 2:
cout << "Enter value to insert at end: ";
cin >> value;
list.insertAtEnd(value);
break;
case 3:
list.deleteFromBeginning();
break;
case 4:
list.deleteFromEnd();
break;
case 5:
cout << "Enter value to search: ";
cin >> value;
list.search(value);
break;
case 6:
list.display();
break;
case 0:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice! Try again.\n";
}
} while (choice != 0);
return 0;
}
DOUBLY LINKED LIST
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
};
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() {
head = NULL;
tail = NULL;
}
if (head != NULL)
head->prev = newNode;
else
tail = newNode;
head = newNode;
}
if (tail != NULL)
tail->next = newNode;
else
head = newNode;
tail = newNode;
}
void removeFromBeginning() {
if (head == NULL) {
cout << "List is empty.\n";
return;
}
if (head != NULL)
head->prev = NULL;
else
tail = NULL;
delete temp;
cout << "Element removed from beginning.\n";
}
void removeFromEnd() {
if (tail == NULL) {
cout << "List is empty.\n";
return;
}
if (tail != NULL)
tail->next = NULL;
else
head = NULL;
delete temp;
cout << "Element removed from end.\n";
}
void display() {
if (head == NULL) {
cout << "List is empty.\n";
return;
}
int main() {
DoublyLinkedList list;
list.insertAtBeginning(263);
list.insertAtBeginning(442);
list.insertAtEnd(152);
list.insertAtEnd(596);
list.display();
list.removeFromBeginning();
list.display();
list.removeFromEnd();
list.display();
return 0;
}
class Node
{
public:
int data;
Node* next;
};
class CircularLinkedList
{
public:
Node* last;
CircularLinkedList()
{
last = NULL;
}
if (last == NULL)
{
newNode->next = newNode;
last = newNode;
} else
{
newNode->next = last->next;
last->next = newNode;
last = newNode;
}
}
do {
if (current->data == value)
{
if (current == last && current->next == last)
{
delete current;
last = NULL;
} else
{
prev->next = current->next;
if (current == last)
{
last = prev;
}
delete current;
}
cout << "Element " << value << " deleted.\n";
return;
}
prev = current;
current = current->next;
} while (current != last->next);
cout << "Element " << value << " not found.\n";
}
return NULL;
}
void display()
{
if (last == NULL)
{
cout << "List is empty.\n";
return;
}
int main()
{
CircularLinkedList list;
list.insert(164);
list.insert(569);
list.insert(564);
list.display();
list.remove(569);
list.display();
return 0;
}
STACK PREFIX/POSTFIX
#include <iostream>
#include <string>
using namespace std;
class Stack
{
int data[100];
int top;
public:
Stack()
{
top = -1;
}
void push(int x)
{
top++;
data[top] = x;
}
int pop()
{
int x = data[top];
top--;
return x;
}
int peek()
{
return data[top];
}
bool isEmpty()
{
return top == -1;
}
};
class PostfixEvaluator
{
public:
int evaluate(string expr)
{
Stack s;
if (c == '+')
{
result = a + b;
}
else if (c == '-')
{
result = a - b;
}
else if (c == '*')
{
result = a * b;
}
else if (c == '/')
{
result = a / b;
}
s.push(result);
}
}
return s.pop();
}
};
int main()
{
string expr;
cout << "Enter postfix expression (e.g., 23*54*+9-): ";
cin >> expr;
PostfixEvaluator evaluator;
int result = evaluator.evaluate(expr);
return 0;
}
QUEUE
#include <iostream>
using namespace std;
class Queue
{
private:
int arr[100]; // Fixed size array
int front;
int rear;
int size;
public:
// Constructor
Queue(int s)
{
size = s;
front = 0;
rear = -1;
}
// Enqueue operation
void enqueue(int x)
{
if (isFull())
{
cout << "Queue is full. Cannot enqueue " << x << ".\n";
return;
}
rear++;
arr[rear] = x;
}
// Dequeue operation
void dequeue()
{
if (isEmpty())
{
cout << "Queue is empty. Cannot dequeue.\n";
return;
}
front++;
}
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.display(); // Output: 10 20 30
q.dequeue();
q.display(); // Output: 20 30
cout << "Front element is: " << q.peek() << endl;
return 0;
}
BINARY SEARCH TREE
#include <iostream>
using namespace std;
// Node structure
class Node
{
public:
int data;
Node* left;
Node* right;
Node(int value)
{
data = value;
left = nullptr;
right = nullptr;
}
};
return node;
}
// Delete a node
Node* deleteNode(Node* node, int value)
{
if (node == nullptr)
return nullptr;
public:
// Constructor
BST()
{
root = nullptr;
}
// Delete a value
void remove(int value)
{
root = deleteNode(root, value);
}
// Insert elements
tree.insert(50);
tree.insert(30);
tree.insert(70);
tree.insert(20);
tree.insert(40);
tree.insert(60);
tree.insert(80);
tree.displayTraversals();
// Delete an element
tree.remove(30);
cout << "After deleting 30:" << endl;
tree.displayTraversals();
return 0;
}
AVL TREES
#include <iostream>
using namespace std;
Node(int val)
{
data = val;
left = right = nullptr;
height = 1;
}
};
// Right rotate
Node* rotateRight(Node* y)
{
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
updateHeight(y);
updateHeight(x);
return x;
}
// Left rotate
Node* rotateLeft(Node* x)
{
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
updateHeight(x);
updateHeight(y);
return y;
}
updateHeight(node);
return node;
}
public:
AVLTree()
{
root = nullptr;
}
void displayInorder()
{
cout << "Inorder Traversal: ";
inorder(root);
cout << endl;
}
};
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(10); // triggers left-left rotation
tree.insert(25); // triggers left-right rotation
tree.displayInorder(); // Output: 10 20 25 30 40
tree.search(25); // Found
tree.search(100); // Not found
return 0;
}