From 99ced19f668378949f206ef6977f049638b8466e Mon Sep 17 00:00:00 2001 From: Tejaswini Date: Sat, 18 Jan 2025 01:04:25 -0800 Subject: [PATCH] Done precourse-1 --- Exercise_1.java | 117 +++++++++++++++++++++++++++-------------- Exercise_2.java | 52 ------------------ Exercise_3.java | 70 ------------------------ LinkedList.java | 84 +++++++++++++++++++++++++++++ StackAsLinkedList.java | 73 +++++++++++++++++++++++++ 5 files changed, 235 insertions(+), 161 deletions(-) delete mode 100644 Exercise_2.java delete mode 100644 Exercise_3.java create mode 100644 LinkedList.java create mode 100644 StackAsLinkedList.java diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..dbc7c105e 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,46 +1,85 @@ -class Stack { +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) - { + //Kindly include Time and Space complexity at top of each file + // Time Complexity :O(1) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this :No + + +// Your code here along with comments explaining your approach + + + 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 = 0; + } + + boolean push(int x) + { //Check for stack Overflow //Write your code here - } - - int pop() - { + if(top == MAX){ + System.out.println("Stack Overflow"); + return false; + } else { + a[top] = x; + top++; + System.out.println("Pushed into stack " +x); + return true; + } + } + + int pop() + { //If empty return 0 and print " Stack Underflow" //Write your code here - } - - int peek() - { + if(isEmpty()){ + System.out.println("Stack Underflow"); + return 0; + } else { + top--; + int x = a[top]; + return x; + } + } + + 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"); - } + if (isEmpty()) { + System.out.println("Stack Empty"); + return 0; + } else { + int index = a[top]; +// System.out.println("Top element is " +index); + return index; + } + + } +} + +// 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"); + System.out.println(s.peek() + "Top element from stack"); + } } diff --git a/Exercise_2.java b/Exercise_2.java deleted file mode 100644 index 5a9c4868c..000000000 --- a/Exercise_2.java +++ /dev/null @@ -1,52 +0,0 @@ -public class StackAsLinkedList { - - StackNode root; - - static class StackNode { - int data; - StackNode next; - - StackNode(int data) - { - //Constructor here - } - } - - - public boolean isEmpty() - { - //Write your code here for the condition if stack is empty. - } - - public void push(int data) - { - //Write code to push data to the stack. - } - - 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 - } - - public int peek() - { - //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()); - } -} diff --git a/Exercise_3.java b/Exercise_3.java deleted file mode 100644 index fb66d329d..000000000 --- a/Exercise_3.java +++ /dev/null @@ -1,70 +0,0 @@ -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 - - // 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/LinkedList.java b/LinkedList.java new file mode 100644 index 000000000..fcb2ff645 --- /dev/null +++ b/LinkedList.java @@ -0,0 +1,84 @@ +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 + data = d; + next = null; + } + } + + // Method to insert a new 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, + // then make the new node as head + // Else traverse till the last node + // and insert the new_node there + + // Insert the new_node at last node + // Return the list by head + if(list.head == null){ + list.head = newNode; + } else { + Node last = list.head; + while(last.next != null){ + last = last.next; + } + last.next = newNode; + } + return list; + } + + // Method to print the LinkedList. + public static void printList(LinkedList list) + { + Node currentNode = list.head; + // Traverse through the LinkedList + while(currentNode!=null) { + // Print the data at current node + System.out.println("Current Node " + currentNode.data + " "); + // Go to next node + currentNode = currentNode.next; + } + } + + // 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/StackAsLinkedList.java b/StackAsLinkedList.java new file mode 100644 index 000000000..d5f336b9f --- /dev/null +++ b/StackAsLinkedList.java @@ -0,0 +1,73 @@ +public class StackAsLinkedList { + + StackNode root; + + static class StackNode { + int data; + StackNode next; + 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 newData = new StackNode(data); + if(newData == null){ + System.out.println("Stack overflow"); + } else { + newData.next = root; + root = newData; + } + } + + 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 Underflow"); + return 0; + } else { + int temp = root.data; + root = root.next; + return temp; + } + } + + public int peek() + { + //Write code to just return the topmost element without removing it. + if(isEmpty()){ + System.out.println("Stack empty"); + } + return root.data; + } + + //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()); + } +}