diff --git a/Exercise_1.class b/Exercise_1.class new file mode 100644 index 00000000..adb44505 Binary files /dev/null and b/Exercise_1.class differ diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb4..6ab17598 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,46 +1,74 @@ +//Space complexity O(N) +//Time complexity O(1) class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file static final int MAX = 1000; - int top; + int top =-1; int a[] = new int[MAX]; // Maximum size of Stack boolean isEmpty() { //Write your code here + if(top == -1) { + return true; + } + return false; } Stack() { //Initialize your constructor } - boolean push(int x) { //Check for stack Overflow + if(top == MAX) { + System.out.println("Stack Overflow"); + return false; + } //Write your code here + else { + a[++top] = x; + return true; + } + } int pop() { //If empty return 0 and print " Stack Underflow" + if(top == -1) { + System.out.println( "Stack Underflow"); + return 0; + } //Write your code here + else { + int temp = a[top]; + top--; + return temp; + } + } int peek() { //Write your code here + return a[top]; } + } - + + + // Driver code -class Main { +class Exercise_1 { public static void main(String args[]) { Stack s = new Stack(); s.push(10); s.push(20); - s.push(30); + s.push(30); System.out.println(s.pop() + " Popped from stack"); } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868..80e8f855 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,14 +1,19 @@ -public class StackAsLinkedList { +//TimeCOmplexity -> 0(1) +//Space Complexity -> O(1) +public class Exercise_2 { - StackNode root; + + StackNode root = null; static class StackNode { int data; StackNode next; - StackNode(int data) + StackNode(int d) { //Constructor here + data = d; + next = null; } } @@ -16,35 +21,58 @@ static class StackNode { public boolean isEmpty() { //Write your code here for the condition if stack is empty. + if(root == null) { + return true; + } + return false; } public void push(int data) { //Write code to push data to the stack. + StackNode node = new StackNode(data); + node.next = null; + if(root == null) { + root = node; + }else { + node.next = root; + root = node; + } + } public int pop() { //If Stack Empty Return 0 and print "Stack Underflow" - //Write code to pop the topmost element of stack. + if(root == null) { + System.out.println("Stack Underflow"); + return 0; + } + //Write code to pop the topmost element of stack. //Also return the popped element + int element = root.data; + root = root.next; + return element; + } public int peek() { //Write code to just return the topmost element without removing it. + return root.data; } + //Driver code public static void main(String[] args) { - StackAsLinkedList sll = new StackAsLinkedList(); + Exercise_2 sll = new Exercise_2(); sll.push(10); sll.push(20); sll.push(30); - + System.out.println(sll.pop() + " popped from stack"); System.out.println("Top element is " + sll.peek()); diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329..fe337d34 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,8 +1,9 @@ import java.io.*; - + //Time Complexity - > O(N) + //Space Complexity - > O(N) // Java program to implement // a Singly Linked List -public class LinkedList { +public class Exercise_3 { Node head; // head of list @@ -18,40 +19,62 @@ static class Node { Node(int d) { //Write your code here + data = d; + next = null; } } // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) + public static Exercise_3 insert(Exercise_3 list, int data) { + // Create a new node with given data - - // If the Linked List is empty, - // then make the new node as head - + Node n = new Node(data); + n.next = null; + // If the Linked List is empty, + if(list.head == null) { + // then make the new node as head + list.head = n; + }else { // Else traverse till the last node // and insert the new_node there + // Insert the new_node at last node + Node temp = list.head; + while (temp.next != null) { + temp = temp.next; + } + temp.next = n; + } + + + + - // Insert the new_node at last node + // Return the list by head - + return list; } // Method to print the LinkedList. - public static void printList(LinkedList list) + public static void printList(Exercise_3 list) { + Node temp = list.head; // Traverse through the LinkedList - + while (temp.next != null) { // Print the data at current node - - // Go to next node + System.out.println(temp.data); + // Go to next node + temp = temp.next; + } + System.out.println(temp.data); + } // Driver code public static void main(String[] args) { /* Start with the empty list. */ - LinkedList list = new LinkedList(); + Exercise_3 list = new Exercise_3(); // // ******INSERTION******