Dsa for exams
import java.util.Stack;
import java.util.*;
class Main{
public static void main(String[]args){
System.out.print("Enter size of stack:");
Scanner sd=new Scanner(System.in);
int n=sd.nextInt();
int N[]=new int[n];
Stack <Integer> sa=new Stack<>();
System.out.print("Enter elements in stack:");
for (int i=0;i<n;i++){
N[i]=sd.nextInt();
sa.push(N[i]);
}
for(int j=0;j<3;j++){
int s =sa.pop();
System.out.println("popped elements: "+s);
}
System.out.println("remaining in stack :"+sa);
}
}
//o/p is:
Enter size of stack:5
Enter elements in stack:1
2
3
4
5
popped elements: 5
popped elements: 4
popped elements: 3
remaining in stack :[1, 2]
//queue :
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sd = new Scanner(System.in);
System.out.print("Enter size: ");
int a = sd.nextInt();
int A[] = new int[a];
System.out.print("Enter elements:\n");
Queue <Integer> qu = new LinkedList<>();
// Push elements onto the stack (LILO behavior)
for (int i = 0; i < a; i++) {
A[i] = sd.nextInt();
qu.add(A[i]);
}
System.out.println("Elements are:");
for (int el : qu) {
System.out.println(el);
}
System.out.println("Removed:\n");
// Pop elements from the stack (LILO behavior)
for (int j = 0; j < 2; j++) {
int de = qu.remove();
System.out.println(de);
}
System.out.println("queue: " + qu);
}
}
//o/p is:
Enter size: 4
Enter elements:
1
2
3
4
Elements are:
1
2
3
4
Removed:
1
2
queue: [3, 4]
//lilo condition in queue:
import java.util.*;
class af {
public static void main(String[] args) {
Scanner sd = new Scanner(System.in);
System.out.print("Enter size: ");
int a = sd.nextInt();
int A[] = new int[a];
System.out.print("Enter elements:\n");
Stack<Integer> stack = new Stack<>();
// Push elements onto the stack (LILO behavior)
for (int i = 0; i < a; i++) {
A[i] = sd.nextInt();
stack.push(A[i]);
}
System.out.println("Elements are:");
for (int el : stack) {
System.out.println(el);
}
System.out.println("Removed:\n");
// Pop elements from the stack (LILO behavior)
for (int j = 0; j < 2; j++) {
int de = stack.pop();
System.out.println(de);
}
System.out.println("Stack: " + stack);
}
}
//o/p is:
Enter size: 3
Enter elements:
12
11
23
Elements are:
1
11
23
Removed:
23
11
Stack: [1]
//queue all :
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sd = new Scanner(System.in);
System.out.print("Enter size: ");
int count =0;
int count1=0;
int a = sd.nextInt();
int A[] = new int[a];
System.out.print("Enter elements:\n");
Queue <Integer> qu = new LinkedList<>();
// Push elements onto the stack (LILO behavior)
for (int i = 0; i < a; i++) {
if (count ==a){
System.out.println("queue is full");
}
else{
A[i] = sd.nextInt();
qu.add(A[i]);
count++;
}
}
System.out.println("Elements are:");//display
for (int el : qu) {
System.out.println(el);
}
System.out.println("Enter number for removing items :");
int b =sd.nextInt();
System.out.println("Removed:\n");
// Pop elements from the stack (LILO behavior)
for (int j = 0; j < b; j++) {
count1++;
int de = qu.remove();
System.out.println(de);
}
if (b==a){
System.out.println("queue is empty");
}
System.out.println("queue: " + qu);
}
}
//o/p is:
Enter size: 3
Enter elements:
33
63
43
Elements are:
33
63
43
Enter number for removing items :
3
Removed:
33
63
43
queue is empty
queue: []
public class LinkedList {
private Node head;
private Node tail;
private int size;
// Node class
private class Node {
int value;
Node next;
public Node(int value) {
this.value = value;
this.next = null;
}
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
// LinkedList constructor
public LinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
// Insert at the first position
public void insertFirst(int value) {
Node node = new Node(value);
node.next = head;
head = node;
if (tail == null) {
tail = head;
}
size++;
}
// Insert at the last position
public void insertLast(int value) {
if (tail == null) {
insertFirst(value);
return;
}
Node node = new Node(value);
tail.next = node;
tail = node;
size++;
}
// Insert at a given index
public void insert(int value, int index) {
if (index == 0) {
insertFirst(value);
return;
}
if (index == size) {
insertLast(value);
return;
}
Node temp = head;
for (int i = 1; i < index; i++) {
temp = temp.next;
}
Node node = new Node(value, temp.next);
temp.next = node;
size++;
}
// Delete from the first position
public int deleteFirst() {
if (head == null) {
return -1;
}
int value = head.value;
head = head.next;
if (head == null) {
tail = null;
}
size--;
return value;
}
// Delete from the last position
public int deleteLast() {
if (size <= 1) {
return deleteFirst();
}
Node temp = head;
while (temp.next != tail) {
temp = temp.next;
}
int value = tail.value;
tail = temp;
tail.next = null;
size--;
return value;
}
// Display the linked list
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.value + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
// Main method for testing
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insertFirst(5);
list.insertFirst(4);
list.insertFirst(3);
list.insertFirst(2);
list.insertFirst(1);
list.display();
list.deleteFirst();
list.display();
list.deleteLast();
list.display();
}
}
/*o/p is:
1 -> 2 -> 3 -> 4 -> 5 -> NULL
2 -> 3 -> 4 -> 5 -> NULL
2 -> 3 -> 4 -> NULL
*/
#meritcurve
import java.util.Scanner;
public class NumberRepresentation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Reading input values
int a = scanner.nextInt();
int b = scanner.nextInt();
// Looping through the range [a, b]
for (int n = a; n <= b; n++) {
if (n >= 1 && n <= 9) {
// Printing English representation of numbers from 1 to 9
switch (n) {
case 1 -> System.out.println("one");
case 2 -> System.out.println("two");
case 3 -> System.out.println("three");
case 4 -> System.out.println("four");
case 5 -> System.out.println("five");
case 6 -> System.out.println("six");
case 7 -> System.out.println("seven");
case 8 -> System.out.println("eight");
case 9 -> System.out.println("nine");
}
} else if (n > 9) {
// Printing "even" or "odd" for numbers greater than 9
if (n % 2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
}
}
scanner.close();
}
}
//Another method
import java.util.Scanner;
public class NumberRepresentationWithArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Storing English representations of numbers 1 to 9 in an array
String[] numberWords = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
// Reading input values
int a = scanner.nextInt();
int b = scanner.nextInt();
// Looping through the range [a, b]
for (int n = a; n <= b; n++) {
if (n >= 1 && n <= 9) {
// Accessing the corresponding English representation from the array
System.out.println(numberWords[n - 1]);
} else if (n > 9) {
// Printing "even" or "odd" for numbers greater than 9
if (n % 2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
}
}
scanner.close();
}
}
Comments
Post a Comment