From dff8907818ff7ce00addfce76fbeca225da01741 Mon Sep 17 00:00:00 2001
From: Revathi Mukkamala <rmukkamala@twilio.com>
Date: Sat, 18 Jan 2025 17:37:48 -0800
Subject: [PATCH] Precourse 1

---
 Exercise_1.java | 36 +++++++++++++++++++++++++++++-------
 Exercise_2.java | 47 ++++++++++++++++++++++++++++++++++++++++-------
 Exercise_3.java | 39 ++++++++++++++++++++++++++++-----------
 3 files changed, 97 insertions(+), 25 deletions(-)

diff --git a/Exercise_1.java b/Exercise_1.java
index 314a3cb45..cb9fcc8e1 100644
--- a/Exercise_1.java
+++ b/Exercise_1.java
@@ -1,4 +1,6 @@
-class Stack { 
+import java.util.Arrays;
+
+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; 
@@ -6,31 +8,51 @@ class Stack {
     int a[] = new int[MAX]; // Maximum size of Stack 
   
     boolean isEmpty() 
-    { 
-        //Write your code here 
+    {
+        return 0 == top;
     } 
 
     Stack() 
     { 
-        //Initialize your constructor 
+        //Initialize your constructor
+        top = 0;
     } 
   
     boolean push(int x) 
     { 
         //Check for stack Overflow
         //Write your code here
+        if( top+1 > MAX) {
+            return false;
+        }
+        a[top] = x;
+        top = top + 1;
+        return true;
     } 
   
     int pop() 
     { 
         //If empty return 0 and print " Stack Underflow"
         //Write your code here
+        if(top == 0) {
+            return 0;
+        }
+        int new_array[] = new int[MAX];
+        int value = a[top-1];
+        for(int i=0; i<top-1; i++) {
+            new_array[i] = a[i];
+        }
+        a=new_array;
+        top = top -1;
+        return value;
     } 
   
     int peek() 
     { 
         //Write your code here
-    } 
+        return a[top- 1];
+    }
+
 } 
   
 // Driver code 
@@ -40,7 +62,7 @@ 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"); 
+        s.push(30);
+        System.out.println(s.pop() + " Popped from stack");
     } 
 }
diff --git a/Exercise_2.java b/Exercise_2.java
index 5a9c4868c..9e6e1b593 100644
--- a/Exercise_2.java
+++ b/Exercise_2.java
@@ -1,4 +1,4 @@
-public class StackAsLinkedList { 
+public class StackAsLinkedList {
   
     StackNode root; 
   
@@ -8,38 +8,71 @@ static class StackNode {
   
         StackNode(int data) 
         { 
-            //Constructor here 
+            //Constructor here
+            this.data = data;
         } 
     } 
     
 	
     public boolean isEmpty() 
     { 
-        //Write your code here for the condition if stack is empty. 
+        //Write your code here for the condition if stack is empty.
+        return null == root;
     } 
   
     public void push(int data) 
-    { 
-        //Write code to push data to the stack. 
+    {
+        //Write code to push data to the stack.
+        StackNode new_node = new StackNode(data);
+        if(null == root){
+            root = new_node;
+            return;
+        }
+        StackNode temp = root;
+        while(temp.next != null) {
+            temp = temp.next;
+        }
+        temp.next = new_node;
     } 
   
     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 
+	//Also return the popped element
+        if(isEmpty()){
+            System.out.println("Stack Underflow");
+            return 0;
+        }
+        StackNode temp = root;
+        while(temp.next != null && temp.next.next != null) {
+            temp = temp.next;
+        }
+        StackNode pop_node = temp.next;
+        temp.next = null;
+        return pop_node.data;
     } 
   
     public int peek() 
     { 
         //Write code to just return the topmost element without removing it.
+        if(isEmpty()){
+            System.out.println("Stack Underflow");
+            return 0;
+        }
+        StackNode temp = root;
+        while(temp.next != null) {
+            temp = temp.next;
+        }
+        StackNode pop_node = temp;
+        return pop_node.data;
     } 
   
 	//Driver code
     public static void main(String[] args) 
     { 
   
-        StackAsLinkedList sll = new StackAsLinkedList(); 
+        StackAsLinkedList sll = new StackAsLinkedList();
   
         sll.push(10); 
         sll.push(20); 
diff --git a/Exercise_3.java b/Exercise_3.java
index fb66d329d..b504d0dd7 100644
--- a/Exercise_3.java
+++ b/Exercise_3.java
@@ -2,7 +2,7 @@
   
 // Java program to implement 
 // a Singly Linked List 
-public class LinkedList { 
+public class LinkedList {
   
     Node head; // head of list 
   
@@ -17,41 +17,58 @@ static class Node {
         // Constructor 
         Node(int d) 
         { 
-            //Write your code here 
+            //Write your code here
+            this.data = d;
         } 
     } 
   
     // Method to insert a new node 
-    public static LinkedList insert(LinkedList list, int data) 
+    public static LinkedList insert(LinkedList list, int data)
     { 
-        // Create a new node with given 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 
+        // 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 
-        
+        // Return the list by head
+        if (null == list.head){
+            list.head = new_node;
+            return list;
+        }
+
+        Node temp = list.head;
+        while(temp.next != null) {
+            temp = temp.next;
+        }
+        temp.next = new_node;
+        return list;
     } 
   
     // Method to print the LinkedList. 
-    public static void printList(LinkedList list) 
+    public static void printList(LinkedList list)
     {  
         // Traverse through the LinkedList 
    
             // Print the data at current node 
        
-            // Go to next node 
+            // Go to next node
+        Node temp = list.head;
+        while(temp != null){
+            System.out.println(temp.data);
+            temp = temp.next;
+        }
     } 
    
     // Driver code 
     public static void main(String[] args) 
     { 
         /* Start with the empty list. */
-        LinkedList list = new LinkedList(); 
+        LinkedList list = new LinkedList();
   
         // 
         // ******INSERTION****** 
@@ -67,4 +84,4 @@ public static void main(String[] args)
         // Print the LinkedList 
         printList(list); 
     } 
-}
\ No newline at end of file
+}