diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..c2084442f --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ + +# Ignore compiled Java files +*.class + +# Ignore C++ executables +a.out +*.out +*.exe +Exercise_2 + +# Ignore temp files +*.o + diff --git a/Exercise_1.class b/Exercise_1.class new file mode 100644 index 000000000..cb41d1048 Binary files /dev/null and b/Exercise_1.class differ diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..aeeebc2cf 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,46 +1,69 @@ -class Stack { + +// Time Complexity: +// push() -> O(1) +// pop() -> O(1) +// peek() -> O(1) +// isEmpty() -> O(1) +// +// Space Complexity: O(MAX) + +import java.util.*; + +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 - - boolean isEmpty() - { - //Write your code here - } - - Stack() - { - //Initialize your constructor - } - - boolean push(int x) - { - //Check for stack Overflow - //Write your code here - } - - int pop() - { - //If empty return 0 and print " Stack Underflow" - //Write your code here - } - - int peek() - { - //Write your code here - } -} - -// Driver code -class Main { - public static void main(String args[]) - { - Stack s = new Stack(); - s.push(10); - s.push(20); - s.push(30); - System.out.println(s.pop() + " Popped from stack"); - } + //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 + + Stack() { // Constructor here + top = -1; + } + + boolean push(int x) { + //Your code here + //Check Stack overflow as well + if (top >= MAX - 1) { + System.out.println("Stack Overflow"); + return false; + } + a[++top] = x; + return true; + } + + int pop() { + //Your code here + //Check Stack Underflow as well + if (top < 0) { + System.out.println("Stack Underflow"); + return -1; + } + return a[top--]; + } + + int peek() { + //Your code here + //Check empty condition too + if (top < 0) { + System.out.println("Stack is Empty"); + return -1; + } + return a[top]; + } + + boolean isEmpty() { + //Your code here + return (top < 0); + } } + +public class Exercise_1 { + public static void main(String[] args) { + Stack s = new Stack(); + s.push(10); + s.push(20); + s.push(30); + System.out.println(s.pop() + " Popped from stack"); + } +} + diff --git a/Exercise_2 b/Exercise_2 new file mode 100755 index 000000000..7cffba06c Binary files /dev/null and b/Exercise_2 differ diff --git a/Exercise_2.cpp b/Exercise_2.cpp index 1eb3de9b9..f456fa936 100644 --- a/Exercise_2.cpp +++ b/Exercise_2.cpp @@ -1,3 +1,18 @@ +// Time Complexity: +// push() -> O(1) +// pop() -> O(1) +// peek() -> O(1) +// isEmpty() -> O(1) +// +// Space Complexity: +// O(n) — where n is the number of elements pushed onto the stack, +// because each push creates a new node in the linked list. +// +// Did this code successfully run on Leetcode? +// This was not a LeetCode problem, but the code runs successfully on terminal. +// +// Any problems faced while coding this: +// Understanding pointer manipulation and using StackNode** for push/pop. #include using namespace std; @@ -18,23 +33,38 @@ StackNode* newNode(int data) int isEmpty(StackNode* root) { - //Your code here +return !root; //Your code here } void push(StackNode** root, int data) { + StackNode* stackNode = newNode(data); + stackNode->next = *root; + *root = stackNode; //Your code here } int pop(StackNode** root) { - //Your code here + if (isEmpty(*root)){ + cout << "Stack Underflow\n"; + return INT_MIN; + } + StackNode* temp = *root; + *root = (*root)->next; + int popped = temp -> data; + delete temp; + return popped;//Your code here } int peek(StackNode* root) { - //Your code here + if (isEmpty(root)){ + cout << "Stack is empty\n"; + return INT_MIN;//Your code here } +return root -> data; +} int main() { @@ -49,4 +79,4 @@ int main() cout << "Top element is " << peek(root) << endl; return 0; -} \ No newline at end of file +} diff --git a/Exercise_3$Node.class b/Exercise_3$Node.class new file mode 100644 index 000000000..068a7a5b9 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..4e5d11efc Binary files /dev/null and b/Exercise_3.class differ diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..d29f1cc9f 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,73 @@ -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 +//Time Complexity:O(n) +//Space: O(n) +//On terminal it runs successfully +//Problems Faced: Understanding pointers +import java.io.*; + +// Java program to implement +// a Singly Linked List +public class Exercise_3 { + + Node head; // head of list + + // Linked list Node. + static class Node { + int data; + Node next; + + // Constructor + 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) + { + // Create a new node with given data + Node new_node = new Node(data); + + // If list is empty + 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; // <-- FIXED + } + + // Method to print the LinkedList. + public static void printList(Exercise_3 list) + { + Node current = list.head; + + while (current != null) { + System.out.print(current.data + " "); + current = current.next; + } + + System.out.println(); + } + + // 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); + } +} - // 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); - } -} \ No newline at end of file diff --git a/Stack.class b/Stack.class new file mode 100644 index 000000000..806bdbea7 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..3cba84ec3 Binary files /dev/null and b/StackAsLinkedList$StackNode.class differ diff --git a/StackAsLinkedList.class b/StackAsLinkedList.class new file mode 100644 index 000000000..3c25d8455 Binary files /dev/null and b/StackAsLinkedList.class differ diff --git a/StackAsLinkedList.java b/StackAsLinkedList.java new file mode 100644 index 000000000..8e9de5eb7 --- /dev/null +++ b/StackAsLinkedList.java @@ -0,0 +1,73 @@ +//Time Complexity : +//push-O(1), pop-O(1), peek-O(1) +//Space Complexity:O(n) +//The code runs successfully in terminal +//Problems Faced: Understanding the pointer updates while pushing and popping elements +public class StackAsLinkedList { + + StackNode root; + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) + { + this.data = data; + this.next = null; + //Constructor here + } + } + + + public boolean isEmpty() + { + //Write your code here for the condition if stack is empty. +return root == null; + } + + public void push(int data) + { + StackNode newNode = new StackNode(data); + newNode.next = root; + root = newNode; + //Write code to push data to the stack. + } + + public int pop() + { + if (isEmpty()){ + System.out.println("Stack Underflow"); + return 0; + }//If Stack Empty Return 0 and print "Stack Underflow" + int popped = root.data; + root = root.next; + return popped;//Write code to pop the topmost element of stack. + //Also return the popped element + } + + public int peek() + { + if (isEmpty()){ + System.out.println("Stack is Empty"); + return 0; + } + return root.data; + //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()); + } +}