0% found this document useful (0 votes)
31 views26 pages

Ds Programs All

madras university based new syllabus ds program

Uploaded by

madhanaksubbu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views26 pages

Ds Programs All

madras university based new syllabus ds program

Uploaded by

madhanaksubbu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

1.

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

Node* top = NULL;

void push(int val) {


Node* newnode = new Node(); // Use new instead of malloc
newnode->data = val;
newnode->next = top;
top = newnode;
}

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>

#define MAX 1000

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

int precedence(char op) {


if (op == '^') return 3;
else if (op == '*' || op == '/') return 2;
else if (op == '+' || op == '-') return 1;
return -1;
}

void infixToPostfix(char infix[], char postfix[]) {


Stack s;
int k = 0;

for (int i = 0; i < strlen(infix); i++) {


char c = infix[i];

// If the character is an operand, add it to the postfix expression


if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
postfix[k++] = c;
}
// If the character is '(', push it to the stack
else if (c == '(') {
s.push(c);
}
// If the character is ')', pop and output from the stack until '(' is found
else if (c == ')') {
while (!s.isEmpty() && s.peek() != '(') {
postfix[k++] = s.pop();
}
if (!s.isEmpty()) s.pop(); // Pop '('
}
// If an operator is encountered
else {
while (!s.isEmpty() && precedence(s.peek()) >= precedence(c)) {
postfix[k++] = s.pop();
}
s.push(c);
}
}

// Pop all the operators from the stack


while (!s.isEmpty()) {
postfix[k++] = s.pop();
}

postfix[k] = '\0'; // Null-terminate the postfix expression


}
int main() {
char infix[100], postfix[100];
cout << "Enter infix: ";
cin >> infix; // Input handling with cin for simplicity
infixToPostfix(infix, postfix);
cout << "Postfix is: " << postfix << endl;
return 0;
}
4.queue in array
#include <iostream.h>
//using namespace std;
int queue[4], n = 4, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
5.Queue in linkeslist
#include <iostream.h>
//using namespace std;
struct Node {
int data;
Node* next;
};

Node* front = NULL;


Node* rear = NULL;
int currentsize = 0;
const int max_size = 3;

void enqueue(int val) {


if (currentsize >= max_size) {
cout << "Queue Overflow" << endl;
return;
}

Node* newnode = new Node(); // Use new instead of malloc


newnode->data = val;
newnode->next = NULL;
if (rear == NULL) {
front = rear = newnode;
} else {
rear->next = newnode;
rear = newnode;
}
currentsize++; // Increment the size of the queue
}

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

int linearSearch(int arr[], int n, int key) {


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index of the found element
}
}
return -1; // Return -1 if the element is not found
}

void main() {
clrscr(); // Clear the screen

int n, key, result;

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

int arr[MAX_SIZE]; // Declare the array with a constant size

cout << "Enter the elements of the array:\n";


for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Enter the element to search for: ";


cin >> key;

result = linearSearch(arr, n, key);

if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}

getch(); // Wait for a key press


}

binary search
#include <iostream.h>
#include <conio.h>

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1;

while (low <= high) {


int mid = (low + high) / 2;

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

int n, key, result;

cout << "Enter the number of elements in the array: ";


cin >> n;

int arr[100]; // Assume maximum size of the array is 100


cout << "Enter " << n << " elements in sorted order:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Enter the element to search for: ";


cin >> key;

result = binarySearch(arr, n, key);

if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}

getch(); // Wait for a key press


}

7. in order ,pre order and post order


#include <iostream.h>
#include <conio.h> // For getch()
//using namespace std;

struct Node {
int data;
Node* left;
Node* right;
Node(int x) : data(x), left(NULL), right(NULL) {}
};

// Function to perform inorder traversal


void inorderTraversal(Node* root) {
if (root == NULL)
return;
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}

// Function to perform preorder traversal


void preorderTraversal(Node* root) {
if (root == NULL)
return;
cout << root->data << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Function to perform postorder traversal


void postorderTraversal(Node* root) {
if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
cout << root->data << " ";
}

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

cout << "Inorder traversal: ";


inorderTraversal(root);
cout << endl;
cout.flush();

cout << "Preorder traversal: ";


preorderTraversal(root);
cout << endl;
cout.flush();

cout << "Postorder traversal: ";


postorderTraversal(root);
cout << endl;
cout.flush();

getch(); // Wait for a key press before closing


return 0;
}
8.bfs and dfs
bfs
#include <iostream.h>
#include <conio.h> // Include for getch()
//using namespace std;

#define MAX 100 // Maximum number of vertices

// Function to add an edge to an undirected graph


void addEdge(int adj[MAX][MAX], int u, int v) {
adj[u][v] = 1;
adj[v][u] = 1; // Since the graph is undirected
}

// BFS function without queue


void bfs(int adj[MAX][MAX], int V, int start) {
int visited[MAX] = { 0 }; // Use int instead of bool (0 for false, 1 for true)
int bfsArray[MAX]; // Array to store the order of BFS traversal
int front = 0, rear = 0;

// Start with the source vertex


visited[start] = 1; // Mark as visited (1 for true)
bfsArray[rear++] = start; // Enqueue the starting vertex

while (front < rear) {


// Dequeue vertex and print it
int current = bfsArray[front++];
cout << current << " ";

// Visit all adjacent vertices


for (int i = 0; i < V; i++) {
if (adj[current][i] == 1 && visited[i] == 0) {
visited[i] = 1; // Mark as visited
bfsArray[rear++] = i; // Enqueue
}
}
}
}

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

// Perform BFS traversal starting from vertex 0


cout << "BFS starting from vertex 0:\n";
bfs(adj, V, 0);
// Pause the program before exiting
cout << "\nPress any key to exit...\n";
getch(); // Wait for a key press

return 0;
}
Dfs
#include <iostream.h>
#include <conio.h> // Include for getch()
//using namespace std;

#define MAX 100 // Maximum number of vertices

// Function to add an edge to an undirected graph


void addEdge(int adj[MAX][MAX], int u, int v) {
adj[u][v] = 1;
adj[v][u] = 1; // Since the graph is undirected
}

// DFS function using recursion


void dfs(int adj[MAX][MAX], int V, int start, int visited[MAX]) {
// Mark the current node as visited and print it
visited[start] = 1;
cout << start << " ";

// Recursively visit all adjacent vertices


for (int i = 0; i < V; i++) {
if (adj[start][i] == 1 && visited[i] == 0) {
dfs(adj, V, i, visited); // Recur for the adjacent vertex
}
}
}

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

// Perform DFS traversal starting from vertex 0


cout << "DFS starting from vertex 0:\n";
dfs(adj, V, 0, visited);

// Pause the program before exiting


cout << "\nPress any key to exit...\n";
getch(); // Wait for a key press

return 0;
}
sp

You might also like