diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..a99a1ed6a 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -4,32 +4,56 @@ class Stack { static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack - + // Time Complexity :O(1) +// Space Complexity :O(1) boolean isEmpty() { //Write your code here + if(top==-1) + return true; + + return false; } Stack() { //Initialize your constructor + top = -1; } - + // Time Complexity :O(1) +// Space Complexity :O(N) boolean push(int x) { //Check for stack Overflow + if(top==MAX-1) + return false; //Write your code here + top++; + a[top] = x; + return true; } - + // Time Complexity :O(1) +// Space Complexity :O(1) int pop() { //If empty return 0 and print " Stack Underflow" + if(top==-1){ + System.out.println("Stack Underflow"); + } //Write your code here + int val = a[top]; + top--; + return val; } - + // Time Complexity :O(N) +// Space Complexity :O(1) int peek() { //Write your code here + if(top==-1) + return -1; + + return a[top]; } } diff --git a/Exercise_3.java b/LinkedList.java similarity index 69% rename from Exercise_3.java rename to LinkedList.java index fb66d329d..aa6d26e4b 100644 --- a/Exercise_3.java +++ b/LinkedList.java @@ -18,30 +18,49 @@ static class Node { Node(int d) { //Write your code here + data = d; + this.next = null; } } - + // Time Complexity :O(1) +// Space Complexity :O(N) // Method to insert a new node public static LinkedList insert(LinkedList list, int data) { // Create a new node with given data - + Node temp = new Node(data); // If the Linked List is empty, // then make the new node as head - + if(list.head==null) + { + list.head = temp; + } // Else traverse till the last node + else{ + Node travel = list.head; + while(travel.next!=null){ + travel = travel.next; + } + travel.next = temp; + } // and insert the new_node there // Insert the new_node at last node // Return the list by head - + return list; } // Method to print the LinkedList. + // Time Complexity :O(N) +// Space Complexity :O(N) public static void printList(LinkedList list) { // Traverse through the LinkedList - + Node temp = list.head; + while(temp!=null){ + System.out.println(temp.data); + temp = temp.next; + } // Print the data at current node // Go to next node diff --git a/Exercise_2.java b/StackAsLinkedList.java similarity index 55% rename from Exercise_2.java rename to StackAsLinkedList.java index 5a9c4868c..0293b6f51 100644 --- a/Exercise_2.java +++ b/StackAsLinkedList.java @@ -9,30 +9,58 @@ static class StackNode { StackNode(int data) { //Constructor here + this.data = data; + this.next = null; } } - + // Time Complexity :O(1) +// Space Complexity :O(1) public boolean isEmpty() { //Write your code here for the condition if stack is empty. + if(root==null) + return true; + + return false; } - + // Time Complexity :O(1) +// Space Complexity :O(1) public void push(int data) { //Write code to push data to the stack. + StackNode temp = new StackNode(data); + temp.next = root; + root = temp; } - + // Time Complexity :O(1) +// Space Complexity :O(1) public int pop() { //If Stack Empty Return 0 and print "Stack Underflow" + if(root == null){ + System.out.println("Stack Underflow"); + return 0; + } + //Write code to pop the topmost element of stack. + int val = root.data; + root = root.next; + return val; + //Also return the popped element } - + // Time Complexity :O(1) +// Space Complexity :O(1) public int peek() { //Write code to just return the topmost element without removing it. + if(root == null){ + System.out.println("Stack Underflow"); + return 0; + } + return root.data; + } //Driver code