DS Programs (1-10)
DS Programs (1-10)
PROGRAM
import java.util.Scanner;
class Stack
Stack(int size)
this.size=size;
this.arr=new int[size];
this.top=-1;
boolean isEmpty()
return(top<0);
if(top==size-1)
System.out.println("Stack Overflow!!");
return false;
else
top++;
arr[top]=val;
return true;
boolean pop()
if(top==-1)
System.out.println("Stack Underflow!!");
return false;
else
int poppedValue=arr[top];
top--;
return true;
}
}
void display()
if(top==-1)
else
for(int i=top;i>=0;i--)
System.out.println(arr[i]);
void peek()
if(top==-1)
System.out.println("Stack is empty");
else
{
System.out.println("Top Element is: "+arr[top]);
int size=sc.nextInt();
int ch=0;
System.out.println("\n----------------------------\n");
while(ch!=5)
ch=sc.nextInt();
switch(ch)
{
case 1:
int value=sc.nextInt();
s.push(value);
break;
case 2:
s.pop();
break;
case 3:
s.peek();
break;
case 4:
s.display();
break;
case 5:
{
System.out.println("EXIT");
break;
default:
sc.close();
}
OUTPUT
RESULT
PROGRAM
import java.util.Scanner;
class ArrayQueue
capacity=size+1;
queue=new int[capacity];
front=rear=0;
if(isFull())
System.out.println("Queue is Full");
return;
queue[rear]=item;
rear=(rear+1)%capacity;
}
public int dequeue()
if(isEmpty())
System.out.println("Queue is empty");
return-1;
int item=queue[front];
front=(front+1)%capacity;
return item;
return front==rear;
return(rear+1)%capacity==front;
if(isEmpty())
System.out.println("Queue is Empty");
return;
for(int i=front;i!=rear;i=(i+1)%capacity)
System.out.println(queue[i]+" ");
System.out.println();
int capacity=scanner.nextInt();
while(true)
int choice=scanner.nextInt();
switch(choice)
case 1:
System.out.println("Enter the element to enqueue: ");
queue.enqueue(scanner.nextInt());
break;
case 2:
int dequeuedElement=queue.dequeue();
if(dequeuedElement!=-1)
break;
case 3:
queue.display();
break;
case 4:
scanner.close();
return;
default:
}
OUTPUT
RESULT
PROGRAM
import java.util.Scanner;
public class LinkedListStack {
private Node top;
private class Node {
int data;
Node next;
}
public LinkedListStack() {
top = null;
}
public void push(int data) {
Node newNode = new Node();
newNode.data = data;
newNode.next = top;
top = newNode;
}
public int pop() {
if (top == null) throw new RuntimeException("Stack is Empty");
int data = top.data;
top = top.next;
return data;
}
public boolean isEmpty() {
return top == null;
}
public int peek() {
if (top == null) throw new RuntimeException("Stack is Empty");
return top.data;
}
public void display() {
if (top == null) {
System.out.println("Stack is Empty");
return;
}
System.out.println("Stack Elements:");
Node current = top;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void displayMenu() {
System.out.println("\nEnter your choice:");
System.out.println("1. PUSH");
System.out.println("2. POP");
System.out.println("3. PEEK");
System.out.println("4. DISPLAY STACK ELEMENTS");
System.out.println("5. EXIT");
}
public static void main(String[] args) {
LinkedListStack stack = new LinkedListStack();
Scanner scanner = new Scanner(System.in);
while (true) {
displayMenu();
String choiceStr = scanner.nextLine();
int choice;
try {
choice = Integer.parseInt(choiceStr);
switch (choice) {
case 1:
System.out.println("Enter a value to push:");
int value = Integer.parseInt(scanner.nextLine());
stack.push(value);
break;
case 2:
try {
System.out.println("Popped: " + stack.pop());
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
break;
case 3:
try {
RESULT
PROGRAM
import java.util.Scanner;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
class LinkedListQueue {
LinkedListQueue() {
if (rear == null) {
front = rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
void dequeue() {
if (front == null) {
System.out.println("Queue is empty.");
} else {
front = front.next;
void peek() {
if (front == null) {
System.out.println("Queue is empty.");
} else {
void display() {
if (front == null) {
System.out.println("Queue is empty.");
} else {
System.out.print("Queue: ");
System.out.println();
while (true) {
case 1:
queue.enqueue(sc.nextInt());
break;
case 2:
queue.dequeue();
break;
case 3:
queue.peek();
break;
case 4:
queue.display();
break;
case 5:
System.out.println("Exiting...");
sc.close();
return;
default:
}
OUTPUT
RESULT
PROGRAM
import java.util.Scanner;
import java.util.Stack;
while (true) {
if (infix.equalsIgnoreCase("exit")) {
break;
if (infix.isEmpty()) {
continue;
}
scanner.close();
if (Character.isLetterOrDigit(c)) {
postfix.append(c);
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
processClosingParenthesis(stack, postfix);
} else if (isOperator(c)) {
} else {
while (!stack.isEmpty()) {
char op = stack.pop();
if (op == '(') {
}
postfix.append(op);
return postfix.toString();
postfix.append(stack.pop());
if (!stack.isEmpty()) {
postfix.append(stack.pop());
stack.push(operator);
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return -1;
}
OUTPUT
RESULT
PROGRAM
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int data;
TreeNode(int data) {
this.data = data;
class BinaryTree {
TreeNode root;
if (elements.length == 0) return;
int i = 1;
if (i < elements.length) {
queue.add(current.left);
if (i < elements.length) {
queue.add(current.right);
// Inorder traversal
if (node != null) {
inorder(node.left);
inorder(node.right);
// Preorder traversal
preorder(node.left);
preorder(node.right);
// Postorder traversal
if (node != null) {
postorder(node.left);
postorder(node.right);
void display() {
if (root == null) {
return;
queue.add(root);
int level = 0;
while (!queue.isEmpty()) {
System.out.println();
level++;
int count = 0;
for (String s : inputStrings) {
if (s.equalsIgnoreCase("done")) {
break;
elements[count++] = Integer.parseInt(s);
tree.insertLevelOrder(elements);
while (true) {
switch (choice) {
case 1:
tree.inorder(tree.root);
System.out.println();
break;
case 2:
tree.preorder(tree.root);
System.out.println();
break;
case 3:
tree.postorder(tree.root);
System.out.println();
break;
case 4:
System.out.println("Tree Structure:");
tree.display();
break;
case 5:
System.out.println("Exiting...");
scanner.close();
return;
default:
}
OUTPUT
RESULT
PROGRAM
import java.util.Scanner;
if (numbers[i] == target) {
// Main method
numbers[i] = sc.nextInt();
}
// Get the target number
sc.close();
}
OUTPUT
RESULT
PROGRAM
import java.util.Scanner;
if (arr[mid] == target) {
} else {
arr[i] = sc.nextInt();
// Display result
if (result == -1) {
} else {
sc.close();
}
OUTPUT
RESULT
PROGRAM
import java.util.*;
public class SimpleGraphSearch {
// Adjacency list representation of the graph
private static Map<Integer, List<Integer>> graph = new HashMap<Integer,
List<Integer>>();
// Function to add an edge to the graph
private static void addEdge(int u, int v) {
if (!graph.containsKey(u)) {
graph.put(u, new ArrayList<Integer>());
}
if (!graph.containsKey(v)) {
graph.put(v, new ArrayList<Integer>());
}
graph.get(u).add(v);
graph.get(v).add(u);
}
// Depth-First Search (DFS)
private static void dfs(int start, Set<Integer> visited) {
if (visited.contains(start)) return;
visited.add(start);
System.out.print(start + " ");
List<Integer> neighbors = graph.get(start);
if (neighbors != null) {
for (int neighbor : neighbors) {
dfs(neighbor, visited);
}
}
}
// Breadth-First Search (BFS)
private static void bfs(int start) {
Set<Integer> visited = new HashSet<Integer>();
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(start);
visited.add(start);
while (!queue.isEmpty()) {
int node = queue.poll();
System.out.print(node + " ");
List<Integer> neighbors = graph.get(node);
if (neighbors != null) {
for (int neighbor : neighbors) {
if (visited.add(neighbor)) {
queue.add(neighbor);
}}
}}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add Edge\n2. DFS\n3. BFS\n4. Exit");
System.out.print("Choose: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter edge (u v): ");
int u = scanner.nextInt();
int v = scanner.nextInt();
addEdge(u, v);
System.out.println("Edge added.");
break;
case 2:
System.out.print("Enter start node for DFS: ");
Set<Integer> visitedDFS = new HashSet<Integer>();
dfs(scanner.nextInt(), visitedDFS);
System.out.println();
break;
case 3:
System.out.print("Enter start node for BFS: ");
bfs(scanner.nextInt());
break;
case 4:
scanner.close();
return;
default:
System.out.println("Invalid choice.");
}
}
}}
OUTPUT
RESULT
PROGRAM
import java.util.*;
this.vertex = vertex;
this.weight = weight;
} }
if (!graph.containsKey(u)) {
if (!graph.containsKey(v)) {
return 0;
});
distances.put(node, Integer.MAX_VALUE);
distances.put(source, 0);
while (!pq.isEmpty()) {
if (neighbors != null) {
} }
} }
}}
int u = scanner.nextInt();
int v = scanner.nextInt();
addEdge(u, v, weight);
dijkstra(source);
}
OUTPUT
RESULT