Skip to content

Commit 8c4651c

Browse files
Merge branch 'SarthakKeshari:master' into MinMaxHeap
2 parents de1976b + 2a49e54 commit 8c4651c

5 files changed

Lines changed: 241 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Statement: Implement a Generic Stack class in Java
3+
*/
4+
5+
import java.util.ArrayList;
6+
7+
class Stack<T> {
8+
private final ArrayList<T> stack;
9+
private final int size;
10+
private int top;
11+
12+
//initialize stack and its fields
13+
public Stack(int size) {
14+
this.size = size;
15+
this.top = -1;
16+
stack = new ArrayList<>(size);
17+
}
18+
19+
//Returns top value in stack
20+
public T top() {
21+
if (top == -1) {
22+
System.out.println("Stack is empty");
23+
return null;
24+
}
25+
26+
return stack.get(top);
27+
}
28+
29+
//Removes top value in stack and returns the popped value
30+
public T pop() {
31+
if (top == -1) {
32+
System.out.println("Stack is empty");
33+
return null;
34+
}
35+
36+
T popped = stack.get(top);
37+
//set value to null
38+
stack.set(top, null);
39+
top--;
40+
return popped;
41+
}
42+
43+
//Push value in stack
44+
public void push(T val) {
45+
//check if stack is full
46+
if (top == size - 1) {
47+
System.out.println("Stack overflow");
48+
return;
49+
}
50+
51+
//push new value in stack
52+
stack.add(val);
53+
top++;
54+
}
55+
56+
//checks if stack is empty
57+
public boolean isEmpty() {
58+
return top == -1;
59+
}
60+
61+
//get stack size
62+
public int size() {
63+
return this.size;
64+
}
65+
66+
//print stack data and meta-data
67+
@Override
68+
public String toString() {
69+
return "Stack{" +
70+
"stack=" + stack +
71+
", size=" + size +
72+
", top=" + top +
73+
'}';
74+
}
75+
}
76+
77+
public class GenericStack {
78+
public static void main(String[] args) {
79+
//stack of integers of size 10
80+
Stack<Integer> integerStack = new Stack<>(10);
81+
82+
//fill the stack completely
83+
for (int i = 0; i < integerStack.size(); i++) {
84+
integerStack.push(i);
85+
}
86+
87+
//print stack information
88+
System.out.println(integerStack);
89+
90+
//print top value in stack
91+
System.out.println(integerStack.top());
92+
93+
//stack is full
94+
integerStack.push(100);
95+
96+
//pop all elements in stack
97+
for (int i = 0; i < integerStack.size(); i++) {
98+
integerStack.pop();
99+
}
100+
101+
//print stack information
102+
System.out.println(integerStack);
103+
104+
//stack underflow
105+
integerStack.pop();
106+
107+
//stack of String of size 5
108+
Stack<String>stringStack = new Stack<>(5);
109+
110+
stringStack.push("Hello");
111+
stringStack.push("Java");
112+
stringStack.push("Programmers");
113+
114+
//get top value in stack
115+
System.out.println(stringStack.top());
116+
117+
//pop the stack and get popped value
118+
String poppedValue = stringStack.pop();
119+
120+
//print popped value
121+
System.out.println(poppedValue);
122+
123+
//print stack information
124+
System.out.println(stringStack.toString());
125+
}
126+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. (Wikipedia)
2+
3+
Java has built-in mechanism to handle exceptions. Using the try statement we can test a block of code for errors. The catch block contains the code that says what to do if exception occurs.
4+
5+
This problem will test your knowledge on try-catch block.
6+
7+
You will be given two integers and as input, you have to compute . If and are not bit signed integers or if is zero, exception will occur and you have to report it. Read sample Input/Output to know what to report in case of exceptions.
8+
*/
9+
10+
import java.io.*;
11+
import java.util.*;
12+
import java.text.*;
13+
import java.math.*;
14+
import java.util.regex.*;
15+
16+
public class Solution {
17+
18+
public static void main(String[] args) {
19+
20+
21+
Scanner scan = new Scanner(System.in);
22+
try {
23+
//taking 2 numbers inputs
24+
int x = scan.nextInt();
25+
int y = scan.nextInt();
26+
//dividing 2 numbers
27+
System.out.println(x / y);
28+
}
29+
catch(ArithmeticException | InputMismatchException e) {
30+
if (e instanceof ArithmeticException) {
31+
//if there is arithmetic exception occured in try block then it will be catched here
32+
System.out.println("java.lang.ArithmeticException: / by zero");
33+
} else if (e instanceof InputMismatchException){
34+
System.out.println("java.util.InputMismatchException");
35+
//if there is inputmismatch exception occured in try block then it will be catched here
36+
}
37+
}
38+
scan.close();
39+
}
40+
41+
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
https://leetcode.com/problems/find-all-duplicates-in-an-array/
3+
*/
4+
5+
class Solution {
6+
public List<Integer> findDuplicates(int[] nums) {
7+
//we are solving this question using cyclic sort
8+
int i=0;
9+
while(i<nums.length){
10+
int correct=nums[i]-1;
11+
if(nums[i]!=nums[correct]){
12+
swap(nums,i,correct);// this line will call the function swap
13+
}
14+
else{
15+
i++;
16+
}
17+
}
18+
List<Integer> ans=new ArrayList<>();
19+
for(int j=0;j<nums.length;j++){
20+
if(nums[j]!=j+1){
21+
ans.add(nums[j]);//here we are adding elements in array
22+
}
23+
}
24+
return ans;
25+
}
26+
void swap(int[] arr,int b,int a){ // by creating 1 temp varible to swap the values of variables
27+
int temp=arr[a];
28+
arr[a]=arr[b];
29+
arr[b]=temp;
30+
}
31+
}

LeetCode/count1s.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
2+
public class Solution {
3+
// you need to treat n as an unsigned value
4+
public int hammingWeight(int n) {
5+
6+
int cnt=0;
7+
int len=0;
8+
while(len<32)
9+
{
10+
if((n&1)==1) //perform bitwise AND and count 1's
11+
{
12+
cnt++;
13+
}
14+
n=n>>1; //right shift digit by one bit
15+
len++;
16+
}
17+
return cnt;
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//Created a program to check whether given string is palindrome or not.
2+
import java.util.*;
3+
public class Main
4+
{
5+
public static void main(String[] args) {
6+
Scanner sc=new Scanner(System.in);
7+
System.out.println("Enter a String");
8+
String s=sc.nextLine();
9+
int l=s.length();
10+
int temp=0;
11+
for(int i=0,j=l-1;i<=j;i++,j--) //Traverse string from start and end
12+
{
13+
if(s.charAt(i)!=s.charAt(j)) //Check Each Character From start and end
14+
{
15+
System.out.println("Not Palindrome"); //if condition fails it displays not palindrome
16+
temp=1;
17+
break;
18+
}
19+
}
20+
if(temp==0) //if abpve condition does not satisfies then print palindrome
21+
System.out.println("Palindrome");
22+
}
23+
}

0 commit comments

Comments
 (0)