Ds Programs All
Ds Programs All
Stack in array
#include <iostream>
//using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
2.Stack in linked list
#include <iostream.h>
//using namespace std;
struct Node {
int data;
Node* next;
};
void pop() {
if (top == NULL)
cout << "Stack Underflow" << endl;
else {
cout << "The popped element is " << top->data << endl;
Node* temp = top; // Store the current top node
top = top->next;
delete temp; // Free the memory of the popped node
}
}
void display() {
if (top == NULL)
cout << "Stack is empty" << endl;
else {
Node* ptr = top;
cout << "Stack elements are: ";
while (ptr != NULL) {
cout << ptr->data << " ";
ptr = ptr->next;
}
cout << endl;
}
}
int main() {
int ch, val;
cout << "1) Push in stack" << endl;
cout << "2) Pop from stack" << endl;
cout << "3) Display stack" << endl;
cout << "4) Exit" << endl;
do {
cout << "Enter choice: " << endl;
cin >> ch;
switch (ch) {
case 1: {
cout << "Enter value to be pushed:" << endl;
cin >> val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout << "Exit" << endl;
break;
}
default: {
cout << "Invalid Choice" << endl;
}
}
} while (ch != 4);
return 0;
}
3.Infix to postfix
#include <iostream.h>
#include <string.h>
class Stack {
private:
char arr[MAX];
int top;
public:
Stack();
int push(char x);
char pop();
char peek();
int isEmpty();
};
Stack::Stack() {
top = -1;
}
int Stack::push(char x) {
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return 0;
} else {
arr[++top] = x;
return 1;
}
}
char Stack::pop() {
if (top < 0) {
cout << "Stack Underflow";
return 0;
} else {
char x = arr[top--];
return x;
}
}
char Stack::peek() {
if (top < 0) {
cout << "Stack is Empty";
return 0;
} else {
char x = arr[top];
return x;
}
}
int Stack::isEmpty() {
return (top < 0);
}
void dequeue() {
if (front == NULL) {
cout << "Queue Underflow" << endl;
} else {
cout << "The dequeued element is " << front->data << endl;
Node* temp = front; // Store the current front node
front = front->next;
if (front == NULL) { // If the queue becomes empty
rear = NULL;
}
delete temp; // Free the memory of the dequeued node
currentsize--; // Decrement the size of the queue
}
}
void display() {
if (front == NULL) {
cout << "Queue is empty" << endl;
} else {
Node* ptr = front;
cout << "Queue elements are: ";
while (ptr != NULL) {
cout << ptr->data << " ";
ptr = ptr->next;
}
cout << endl;
}
}
int main() {
int ch, val;
cout << "1) Enqueue in queue" << endl;
cout << "2) Dequeue from queue" << endl;
cout << "3) Display queue" << endl;
cout << "4) Exit" << endl;
do {
cout << "Enter choice: " << endl;
cin >> ch;
switch (ch) {
case 1: {
cout << "Enter value to be enqueued:" << endl;
cin >> val;
enqueue(val);
break;
}
case 2: {
dequeue();
break;
}
case 3: {
display();
break;
}
case 4: {
cout << "Exit" << endl;
break;
}
default: {
cout << "Invalid Choice" << endl;
}
}
} while (ch != 4);
return 0;
}
6. linear search and binary search
Linear search
#include <iostream.h>
#include <conio.h>
const int MAX_SIZE = 100; // Define a constant size for the array
void main() {
clrscr(); // Clear the screen
cout << "Enter the number of elements in the array (max " << MAX_SIZE
<< "): ";
cin >> n;
if (n > MAX_SIZE) {
cout << "Number of elements exceeds maximum size!" << endl;
getch();
return;
}
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
binary search
#include <iostream.h>
#include <conio.h>
if (arr[mid] == key) {
return mid; // Element found, return the index
}
if (arr[mid] < key) {
low = mid + 1; // Search in the right half
} else {
high = mid - 1; // Search in the left half
}
}
return -1; // Element not found
}
void main() {
clrscr(); // Clear the screen
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
struct Node {
int data;
Node* left;
Node* right;
Node(int x) : data(x), left(NULL), right(NULL) {}
};
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
int main() {
int V = 5; // Number of vertices
int adj[MAX][MAX] = {0}; // Adjacency matrix initialized to 0
// Add edges
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 4);
return 0;
}
Dfs
#include <iostream.h>
#include <conio.h> // Include for getch()
//using namespace std;
int main() {
int V = 5; // Number of vertices
int adj[MAX][MAX] = {0}; // Adjacency matrix initialized to 0
int visited[MAX] = {0}; // Visited array initialized to 0
// Add edges
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 4);
return 0;
}
sp