diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..fbb51546f 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,6 +1,12 @@ class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file + //timecomplexity + //isEmpty() ->O(1) + //push()->O(1) + //pop()->O(1) + //peek()->O(1) + //Spece Complexity: O(MAX) static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack @@ -8,28 +14,49 @@ class Stack { boolean isEmpty() { //Write your code here + return(top < 0); } Stack() { //Initialize your constructor + top = -1; //stack is empty } boolean push(int x) { //Check for stack Overflow //Write your code here + //push : first increment top then store value + if(top >= MAX-1){ + System.out.println("Stack is overflow"); + return false; + }else{ + a[++top] = x; + return true; + } } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if(isEmpty()){ + System.out.println("Stack underflow"); + return 0; + }else{ + return a[top--]; + } } int peek() { - //Write your code here + if(isEmpty()){ + System.out.println("Stack underflow"); + return 0; + }else{ + return a[top]; + } } } diff --git a/Exercise_2$StackNode.class b/Exercise_2$StackNode.class new file mode 100644 index 000000000..0760c5ea3 Binary files /dev/null and b/Exercise_2$StackNode.class differ diff --git a/Exercise_2.class b/Exercise_2.class new file mode 100644 index 000000000..709d9e5d2 Binary files /dev/null and b/Exercise_2.class differ diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..08e88de51 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,52 +1,64 @@ -public class StackAsLinkedList { - - StackNode root; - - static class StackNode { - int data; - StackNode next; - - StackNode(int data) - { - //Constructor here - } - } - - - public boolean isEmpty() - { - //Write your code here for the condition if stack is empty. - } - - public void push(int data) - { - //Write code to push data to the stack. - } - - public int pop() - { - //If Stack Empty Return 0 and print "Stack Underflow" - //Write code to pop the topmost element of stack. - //Also return the popped element - } - - public int peek() - { - //Write code to just return the topmost element without removing it. - } - - //Driver code - public static void main(String[] args) - { - - StackAsLinkedList sll = new StackAsLinkedList(); - - 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()); - } -} +public class Exercise_2 { + + // Time Complexity: + // isEmpty() -> O(1) + // push() -> O(1) + // pop() -> O(1) + // peek() -> O(1) + // + // Space Complexity: + // O(n) + + StackNode root; + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) { + this.data = data; + this.next = null; + } + } + + public boolean isEmpty() { + return root == null; + } + + public void push(int data) { + StackNode newNode = new StackNode(data); + newNode.next = root; + root = newNode; + } + + public int pop() { + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + + int popped = root.data; + root = root.next; + return popped; + } + + public int peek() { + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + + return root.data; + } + + public static void main(String[] args) { + 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()); + } +} \ No newline at end of file diff --git a/Exercise_3$Node.class b/Exercise_3$Node.class new file mode 100644 index 000000000..7b630f685 Binary files /dev/null and b/Exercise_3$Node.class differ diff --git a/Exercise_3.class b/Exercise_3.class new file mode 100644 index 000000000..500bada30 Binary files /dev/null and b/Exercise_3.class differ diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..6fd518b64 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,63 @@ -import java.io.*; - -// Java program to implement -// a Singly Linked List -public class LinkedList { - - Node head; // head of list - - // Linked list Node. - // This inner class is made static - // so that main() can access it - static class Node { - - int data; - Node next; - - // Constructor - Node(int d) - { - //Write your code here - } - } - - // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) - { - // Create a new node with given data - - // If the Linked List is empty, - // then make the new node as head - - // Else traverse till the last node - // and insert the new_node there +import java.io.*; - // Insert the new_node at last node - // Return the list by head - - } - - // Method to print the LinkedList. - public static void printList(LinkedList list) - { - // Traverse through the LinkedList - - // Print the data at current node - - // Go to next node - } - - // Driver code - public static void main(String[] args) - { - /* Start with the empty list. */ - LinkedList list = new LinkedList(); - - // - // ******INSERTION****** - // - - // Insert the values - list = insert(list, 1); - list = insert(list, 2); - list = insert(list, 3); - list = insert(list, 4); - list = insert(list, 5); - - // Print the LinkedList - printList(list); - } +// Time Complexity: +// insert() -> O(n) +// printList() -> O(n) +// +// Space Complexity: +// O(n) + +public class Exercise_3 { + + Node head; // head of list + + static class Node { + int data; + Node next; + + Node(int d) { + this.data = d; + this.next = null; + } + } + + // Method to insert a new node + public static Exercise_3 insert(Exercise_3 list, int data) { + Node new_node = new Node(data); + + if (list.head == null) { + list.head = new_node; + } else { + Node last = list.head; + while (last.next != null) { + last = last.next; + } + last.next = new_node; + } + + return list; + } + + // Method to print the LinkedList + public static void printList(Exercise_3 list) { + Node currNode = list.head; + + while (currNode != null) { + System.out.print(currNode.data + " "); + currNode = currNode.next; + } + } + + // Driver code + public static void main(String[] args) { + Exercise_3 list = new Exercise_3(); + + list = insert(list, 1); + list = insert(list, 2); + list = insert(list, 3); + list = insert(list, 4); + list = insert(list, 5); + + printList(list); + } } \ No newline at end of file diff --git a/Main.class b/Main.class new file mode 100644 index 000000000..838c9b12b Binary files /dev/null and b/Main.class differ diff --git a/Stack.class b/Stack.class new file mode 100644 index 000000000..22ffb777e Binary files /dev/null and b/Stack.class differ