From 96019c6d2cf9653b98d6fd66e0cf63f6569e74e0 Mon Sep 17 00:00:00 2001 From: JoseRMarcano Date: Thu, 25 Dec 2025 19:15:38 -0800 Subject: [PATCH] fixing data --- Exercise_1.java | 27 ++++++++- Exercise_2.java | 52 ++++++++++------ Exercise_3.java | 156 +++++++++++++++++++++++++++--------------------- 3 files changed, 148 insertions(+), 87 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..62378757b 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -3,38 +3,59 @@ class 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 + int a[] = new int[MAX]; // Maximum size of Stack + int numElements=0; boolean isEmpty() { - //Write your code here + //Write your code here + return numElements==0; } Stack() { //Initialize your constructor + this.top=0; } boolean push(int x) { //Check for stack Overflow + if (this.top >= this.a.length) { + return false; + } //Write your code here + this.a[this.top++]=x; + this.numElements++; + System.out.println("Pushed " + x + " with top "+ this.top); + return true; } int pop() { //If empty return 0 and print " Stack Underflow" + if (this.numElements==0) { + System.out.println("Stack Underflow"); + return 0; + } //Write your code here + this.top--; + int pop= this.a[this.top]; + this.a[this.top]=Integer.MIN_VALUE; + this.numElements--; + return pop; + } int peek() { //Write your code here + return this.a[this.top]; } } // Driver code -class Main { +class Exercise_1 { public static void main(String args[]) { Stack s = new Stack(); diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..3d85e8818 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,21 @@ -public class StackAsLinkedList { +// StackAsLinkedList +public class Exercise_2 { + //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()); + } +} +class StackAsLinkedList { StackNode root; @@ -9,6 +26,7 @@ static class StackNode { StackNode(int data) { //Constructor here + this.data=data; } } @@ -16,37 +34,37 @@ static class StackNode { 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 newNode= new StackNode(data); + newNode.next=root; + root= newNode; } public int pop() { - //If Stack Empty Return 0 and print "Stack Underflow" + //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 prevNode= root.next; + int poppedValue= root.data; + root = prevNode; + return poppedValue; } public int peek() { //Write code to just return the topmost element without removing it. + int value = root.data; + return value; } - //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 index fb66d329d..b0f34a30b 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,92 @@ -import java.io.*; - +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); - } +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 + this.data = d; + } + } + + // 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 + if (list.head == null) { + list.head = newNode; + + } else { + // Else traverse till the last node + // and insert the new_node there + + // Insert the new_node at last node + Node current = list.head; + while (current.next != null) { + current = current.next; + } + current.next = newNode; + } + + + // Return the list by head + return list; + } + + // 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 + Node current = list.head; + + while (current != null) { + // Print the data at current node + System.out.print(current.data + " "); + + // Go to next node + current = current.next; + } + System.out.println(); + } + +} + +public class Exercise_3 { + // Driver code + public static void main(String[] args) { + /* Start with the empty list. */ + LinkedList list = new LinkedList(); + + // + // ******INSERTION****** + // + + // Insert the values + list = list.insert(list, 1); + list = list.insert(list, 2); + list = list.insert(list, 3); + list = list.insert(list, 4); + list = list.insert(list, 5); + + // Print the LinkedList + list.printList(list); + } } \ No newline at end of file