diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..bfd8e5749 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,35 +1,67 @@ +// Time Complexity : o(1) +// Space Complexity :o(1) +// Did this code successfully run on Leetcode : N/A it is not a leetcode problem +// Any problem you faced while coding this :No +// Your code here along with comments explaining your approach +// steps to implement stack using array +// 1. create a class stack with max size +// 2. create an array to hold stack elements and a variable top to track the top element index +// 3. implement isEmpty method to check if top is -1 +// 4. implement push method to add element to stack and check for overflow +// 5. implement pop method to remove and return top element and check for underflow +// 6. implement peek method to return top element without removing it + + 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 a[] = new int[MAX]; // Maximum size of Stack + int[] a; + private int capacity =1000;// Maximum size of Stack boolean isEmpty() { - //Write your code here + if(top == -1){ + return true; + } + return false; } - Stack() + public Stack(int capacity) { //Initialize your constructor + a = new int[capacity]; + top = -1; } + + boolean push(int x) { //Check for stack Overflow - //Write your code here + if(top == capacity -1){ + return false; + } + a[++top] =x; + System.out.println("Pushed into the stack"); + return true; } int pop() { - //If empty return 0 and print " Stack Underflow" - //Write your code here + if(isEmpty()){ + System.out.println("Stack Underflow"); + return -1; + } + return a[top--]; } int peek() { - //Write your code here + if(isEmpty()){ + System.out.println("Stack is empty"); + return -1; + } + return a[top]; } } @@ -37,7 +69,7 @@ int peek() class Main { public static void main(String args[]) { - Stack s = new Stack(); + Stack s = new Stack(10000); s.push(10); s.push(20); s.push(30); diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..c24065955 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,6 +1,23 @@ -public class StackAsLinkedList { +// Time Complexity : o(1) +// Space Complexity :o(1) +// Did this code successfully run on Leetcode : N/A it is not a leetcode problem +// Any problem you faced while coding this :No +// Your code here along with comments explaining your approach +// steps to implement stack using LinkedList +//1 create node with data and next node. Intialize the constructor node with data. +//2 push : create new node and assign top of next node to new node +//3 pop: keep top node in temporary node and assing top to next node. +//4 peek: check for empty stack. if Not return top node data. +class StackAsLinkedList { - StackNode root; + private StackNode top; + + public StackAsLinkedList() + { + //Initialize your constructor + top = null; + } + static class StackNode { int data; @@ -8,7 +25,9 @@ static class StackNode { StackNode(int data) { - //Constructor here + this.data = data; + next = null; + } } @@ -16,11 +35,22 @@ static class StackNode { public boolean isEmpty() { //Write your code here for the condition if stack is empty. + if(top == 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 = top; + top = newNode; + System.out.println(data +"Pushed into stack"); + } public int pop() @@ -28,11 +58,26 @@ 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(top == null){ + System.out.println("Stack underflow"); + return 0; + } + StackNode temp = top; + top = top.next; + int val = temp.data; + temp = null; + return val; + } public int peek() { //Write code to just return the topmost element without removing it. + if(top == null){ + System.out.println("stack is null"); + return -1; + } + return top.data; } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..4af21e919 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -2,7 +2,15 @@ // Java program to implement // a Singly Linked List -public class LinkedList { +// Time Complexity : o(n) +// Space Complexity :o(n) +// Did this code successfully run on Leetcode : N/A it is not a leetcode problem +// Any problem you faced while coding this :No +// Your code here along with comments explaining your approach +// 1.create node with data and next node and constructor +//2. insert new node: create new node with data. If list is empty then make new node as head. Else traverse until last node and insert new node to last node next. + +class LinkedList { Node head; // head of list @@ -17,7 +25,8 @@ static class Node { // Constructor Node(int d) { - //Write your code here + data = d; + next = null; } } @@ -25,15 +34,25 @@ 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 + if(list.head == null){ + list.head = new_node; + }else{ + // Else traverse till the last node // and insert the new_node there + Node last = list.head; + while(last.next!=null){ + last = last.next; + } + // Insert the new_node at last node + last.next = new_node; - // Insert the new_node at last node + } // Return the list by head + return list; } @@ -41,10 +60,15 @@ public static LinkedList insert(LinkedList list, int data) public static void printList(LinkedList list) { // Traverse through the LinkedList - - // Print the data at current node - - // Go to next node + // Print the data at current node + // Go to next node + Node currNode = list.head; + System.out.println("LinkedList. ::"); + while(currNode!=null){ + // Print the data at current node + System.out.print(currNode.data + " "); + currNode = currNode.next; + } } // Driver code