diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..2d340698c 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,5 +1,7 @@ class Stack { //Please read sample.java file before starting. + //Time Complexity: 0(1) + //Space Complexity: 0(n) //Kindly include Time and Space complexity at top of each file static final int MAX = 1000; int top; @@ -8,28 +10,55 @@ class Stack { boolean isEmpty() { //Write your code here + if(top<0){ + return true; + else{ + return false; + } + } } Stack() { //Initialize your constructor + top=-1; } boolean push(int x) { //Check for stack Overflow + if(top>=Max-1) + { + System.out.println("stack Overflow"); + return false; + } //Write your code here + top = top+1; + a[top]=x; + return true; } int pop() { //If empty return 0 and print " Stack Underflow" + if(isEmpty()){ + System.out.println("Stack Underflow") + return 0; + } //Write your code here + int value=a[top]; + top=top-1; + return value; } int peek() { //Write your code here + if(isEmpty()){ + System.out.println("Stack is Empty"); + return 0; + } + return a[top]; } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..06a4446f8 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,3 +1,5 @@ +//Time Complexity: 0(1) +//Space Complexity: 0(n) public class StackAsLinkedList { StackNode root; @@ -9,6 +11,8 @@ static class StackNode { StackNode(int data) { //Constructor here + this.data=data; + this.next=null; } } @@ -16,23 +20,44 @@ static class StackNode { public boolean isEmpty() { //Write your code here for the condition if stack is empty. + if(root==null){ + return true; + }else{ + return false; + } } public void push(int data) { //Write code to push data to the stack. + StackNode newNode =new StackNode(data); + newNode.next=root; + root=newNode; + System.out.println(data+"pushed to stack"); } public int pop() { //If Stack Empty Return 0 and print "Stack Underflow" + if(isEmpty()){ + System.out.println("Stack Underflow"); + return 0; + } //Write code to pop the topmost element of stack. + int poppedValue=root.data; + root=root.next; + return poppedValue; //Also return the popped element } 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 diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..24bce2a92 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -18,6 +18,8 @@ static class Node { Node(int d) { //Write your code here + data=d; + next=null; } } @@ -25,15 +27,26 @@ static class Node { public static LinkedList insert(LinkedList list, int data) { // Create a new node with given data + Node newNode=new Node(data); // If the Linked List is empty, + if(list.head==null){ + list.head=newNode; + } // then make the new node as head - // Else traverse till the last node // and insert the new_node there - + else{ + Node last=list.head; + while (last.next!=null){ + last=last.next; + } + // Insert the new_node at last node + last.next=new_node; + } // Return the list by head + return list; } @@ -41,10 +54,15 @@ public static LinkedList insert(LinkedList list, int data) public static void printList(LinkedList list) { // Traverse through the LinkedList + Node currentNode = list.head; + while(currentNode!=null){ + System.out.print(currentNode.data+" "); + } // Print the data at current node // Go to next node + currentNode=currentNode.next; } // Driver code