diff --git a/Exercise_1.class b/Exercise_1.class new file mode 100644 index 000000000..ead77032d Binary files /dev/null and b/Exercise_1.class differ diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..e6d953445 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,35 +1,53 @@ class Stack { - //Please read sample.java file before starting. - //Kindly include Time and Space complexity at top of each file + // Time Complexcity : + // push() - o(1) + // pop() - o(1) + // peek() - o(1) + // isEmpty() - o(1) + // Space Complexity : o(MAX) + static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack boolean isEmpty() - { - //Write your code here + { + return (top < 0); + } Stack() { - //Initialize your constructor + top = -1 ; } boolean push(int x) { - //Check for stack Overflow - //Write your code here - } + if (top >= (MAX - 1)){ + System.out.println("Stack Overflow"); + return false; + } + 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; + } + return a[top--]; } - + + int peek() { - //Write your code here + if (isEmpty()){ + System.out.println("Stack is Empty"); + return 0; + } + return a[top]; } } diff --git a/LinkedList$Node.class b/LinkedList$Node.class new file mode 100644 index 000000000..e2ba79de8 Binary files /dev/null and b/LinkedList$Node.class differ diff --git a/LinkedList.class b/LinkedList.class new file mode 100644 index 000000000..cfb6cd685 Binary files /dev/null and b/LinkedList.class differ diff --git a/Exercise_3.java b/LinkedList.java similarity index 67% rename from Exercise_3.java rename to LinkedList.java index fb66d329d..f4018cac2 100644 --- a/Exercise_3.java +++ b/LinkedList.java @@ -1,6 +1,3 @@ -import java.io.*; - -// Java program to implement // a Singly Linked List public class LinkedList { @@ -17,7 +14,9 @@ static class Node { // Constructor Node(int d) { - //Write your code here + data = d; + next = null; + } } @@ -25,28 +24,35 @@ static class Node { public static LinkedList insert(LinkedList list, int data) { // Create a new node with given data + Node new_node = new Node(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 + if (list.head == null){ + list.head = new_node; + return list; + } + Node current = list.head; + while (current.next != null) { + current = current.next; + } + current.next = new_node; + return list; + } - // 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 - + Node current = list.head; + while (current != null) { // Print the data at current node - + System.out.print(current.data + " "); // Go to next node - } - + current = current.next; + } + } + // Driver code public static void main(String[] args) { diff --git a/Main.class b/Main.class new file mode 100644 index 000000000..1cef80aa1 Binary files /dev/null and b/Main.class differ diff --git a/Stack.class b/Stack.class new file mode 100644 index 000000000..ab0ea69bb Binary files /dev/null and b/Stack.class differ diff --git a/StackAsLinkedList$StackNode.class b/StackAsLinkedList$StackNode.class new file mode 100644 index 000000000..b996e395f Binary files /dev/null and b/StackAsLinkedList$StackNode.class differ diff --git a/StackAsLinkedList.class b/StackAsLinkedList.class new file mode 100644 index 000000000..9a7a51d2a Binary files /dev/null and b/StackAsLinkedList.class differ diff --git a/Exercise_2.java b/StackAsLinkedList.java similarity index 56% rename from Exercise_2.java rename to StackAsLinkedList.java index 5a9c4868c..cbc09e0dd 100644 --- a/Exercise_2.java +++ b/StackAsLinkedList.java @@ -8,33 +8,45 @@ static class StackNode { StackNode(int data) { - //Constructor here + this.data = data; + this.next = null; } } public boolean isEmpty() { - //Write your code here for the condition if stack is empty. + return (root == null); } public void push(int data) { - //Write code to push data to the stack. + StackNode newNode = new StackNode(data); + newNode.next = root; + root = newNode; } 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 - } - + if (isEmpty()){ + System.out.println("Stack is Empty"); + return 0; + } + int popped = root.data; + root = root.next; + return popped; + } + public int peek() { - //Write code to just return the topmost element without removing it. - } - + if (isEmpty()){ + System.out.println("Stack is Empty"); + return 0; + } + return root.data; + } + //Driver code public static void main(String[] args) {