├── .DS_Store ├── README.md └── Stack ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── StackAL.java ├── StackJCF.java └── StackLL.java /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apna-college/Java-B/cf2968404004abc0c4a08ef5e5bc921dfce60569/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-B -------------------------------------------------------------------------------- /Stack/Question1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | //To push an element at the bottom of a stack 3 | public class Question1 { 4 | public static void pushAtBottom(Stack s, int data) { 5 | if(s.isEmpty()) { 6 | s.push(data); 7 | return; 8 | } 9 | 10 | int temp = s.pop(); 11 | pushAtBottom(s, data); 12 | s.push(temp); 13 | } 14 | 15 | public static void main(String args[]) { 16 | Stack stack = new Stack<>(); 17 | stack.push(1); 18 | stack.push(2); 19 | stack.push(3); 20 | pushAtBottom(stack, 4); 21 | 22 | while(!stack.isEmpty()) { 23 | System.out.println(stack.pop()); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Stack/Question2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | //Reverse a String using a Stack 3 | 4 | public class Question2 { 5 | public static void main(String args[]) { 6 | String str = "HelloWorld"; 7 | 8 | Stack s = new Stack<>(); 9 | 10 | int i=0; 11 | while(i s, int data) { 6 | if(s.isEmpty()) { 7 | s.push(data); 8 | return; 9 | } 10 | 11 | int temp = s.pop(); 12 | pushAtBottom(s, data); 13 | s.push(temp); 14 | } 15 | 16 | public static void reverse(Stack s) { 17 | if(s.isEmpty()) { 18 | return; 19 | } 20 | 21 | int top = s.pop(); 22 | reverse(s); 23 | pushAtBottom(s, top); 24 | } 25 | 26 | public static void main(String args[]) { 27 | Stack stack = new Stack<>(); 28 | stack.push(1); 29 | stack.push(2); 30 | stack.push(3); 31 | 32 | while(!stack.isEmpty()) { 33 | System.out.println(stack.pop()); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Stack/Question4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | //Stock Span Problem 3 | 4 | public class Question4 { 5 | public static void stockSpan(int stocks[]) { 6 | Stack s = new Stack<>(); 7 | int span[] = new int[stocks.length]; 8 | span[0] = 1; 9 | s.push(0); 10 | 11 | for(int i=1; i= stocks[s.peek()]) { 14 | s.pop(); 15 | } 16 | if(s.isEmpty()) { 17 | span[i] = i+1; 18 | } else { 19 | int prevHigh = s.peek(); 20 | span[i] = i - prevHigh; 21 | } 22 | 23 | s.push(i); 24 | } 25 | 26 | for(int i=0; i list = new ArrayList<>(); 6 | 7 | public void push(int data) { 8 | list.add(data); 9 | } 10 | 11 | public boolean isEmpty() { 12 | return list.size() == 0; 13 | } 14 | 15 | public int pop() { 16 | if(isEmpty()) { 17 | return -1; 18 | } 19 | int top = list.remove(list.size()-1); 20 | return top; 21 | } 22 | 23 | public int peek() { 24 | if(isEmpty()) { 25 | return -1; 26 | } 27 | return list.get(list.size()-1); 28 | } 29 | } 30 | public static void main(String args[]) { 31 | Stack stack = new Stack(); 32 | stack.push(1); 33 | stack.push(2); 34 | stack.push(3); 35 | stack.push(4); 36 | 37 | while(!stack.isEmpty()) { 38 | System.out.println(stack.peek()); 39 | stack.pop(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Stack/StackJCF.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class StackJCF { 4 | public static void main(String args[]) { 5 | Stack stack = new Stack<>(); 6 | stack.push(1); 7 | stack.push(2); 8 | stack.push(3); 9 | stack.push(4); 10 | 11 | while(!stack.isEmpty()) { 12 | System.out.println(stack.peek()); 13 | stack.pop(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Stack/StackLL.java: -------------------------------------------------------------------------------- 1 | //stack using Linked List 2 | public class StackDS { 3 | private static class Node { 4 | int data; 5 | Node next; 6 | 7 | Node(int data) { 8 | this.data = data; 9 | next = null; 10 | } 11 | } 12 | 13 | static class Stack { 14 | public static Node head = null; 15 | public static void push(int data) { 16 | Node newNode = new Node(data); 17 | 18 | if(head == null) { 19 | head = newNode; 20 | return; 21 | } 22 | newNode.next = head; 23 | head = newNode; 24 | } 25 | 26 | public static boolean isEmpty() { 27 | return head == null; 28 | } 29 | 30 | public static int pop() { 31 | if(isEmpty()) { 32 | return -1; 33 | } 34 | Node top = head; 35 | head = head.next; 36 | return top.data; 37 | } 38 | 39 | public static int peek() { 40 | if(isEmpty()) { 41 | return -1; 42 | } 43 | Node top = head; 44 | return top.data; 45 | } 46 | } 47 | 48 | public static void main(String args[]) { 49 | Stack stack = new Stack(); 50 | stack.push(1); 51 | stack.push(2); 52 | stack.push(3); 53 | stack.push(4); 54 | 55 | while(!stack.isEmpty()) { 56 | System.out.println(stack.peek()); 57 | stack.pop(); 58 | } 59 | } 60 | } --------------------------------------------------------------------------------