From 67678dfef5a8cc70a28c55dc38469ea4e60f31f3 Mon Sep 17 00:00:00 2001 From: Anirudh Venkateshwaran Date: Fri, 29 May 2026 15:13:28 -0700 Subject: [PATCH] Completed Precourse-1 --- Exercise_1.java | 28 ++++++++++++++++++++++++++ Exercise_2.java | 37 ++++++++++++++++++++++++++++++++++- Exercise_3.java | 52 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 108 insertions(+), 9 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..6ec5f4885 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -8,28 +8,56 @@ class 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 + if(top == MAX) + { + System.out.println("Stack Overflow"); + return false; + } + + top += 1; + a[top] = x; + return true; } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if(top == 0) + { + System.out.println("Stack Underflow"); + return 0; + } + + int ele = a[top]; + top -= 1; + return ele; + } int peek() { //Write your code here + if(top == 0) + { + System.out.println("Stack Underflow"); + return 0; + } + + return a[top]; } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..3ad0e0173 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -9,18 +9,34 @@ static class StackNode { StackNode(int data) { //Constructor here + this.data = data; + this.next = null; } } + + public StackAsLinkedList() + { + this.root = new StackNode(Integer.MIN_VALUE); + } public boolean isEmpty() { - //Write your code here for the condition if stack is empty. + //Write your code here for the conditi on if stack is empty. + if(root.data == Integer.MIN_VALUE) + { + return true; + } + + return false; } public void push(int data) { //Write code to push data to the stack. + StackNode temp = new StackNode(data); + temp.next = root; + root = temp; } public int pop() @@ -28,11 +44,30 @@ 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(root.data == Integer.MIN_VALUE) + { + System.out.println("Stack Underflow"); + return 0; + } + + StackNode temp = root; + int ele = temp.data; + root = root.next; + temp.next = null; + return ele; + } public int peek() { //Write code to just return the topmost element without removing it. + if(root.data == Integer.MIN_VALUE) + { + System.out.println("Stack Underflow"); + return 0; + } + + return root.data; } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..fce0f53b6 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -18,22 +18,43 @@ static class Node { Node(int d) { //Write your code here + this.data = d; + this.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 + if(list.head == null) + { + list.head = newNode; + } - // Else traverse till the last node - // and insert the new_node there - - // Insert the new_node at last node + // Else traverse till the last node + // and insert the new_node there + // Insert the new_node at last node // Return the list by head + else + { + Node temp = list.head; + + while(temp.next != null) + { + temp = temp.next; + } + + temp.next = newNode; + + } + + return list; + } @@ -42,9 +63,24 @@ 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 + + if(list == null || list.head == null) + { + System.out.println("The linked list is empty"); + return; + } + + Node temp = list.head; + + while(temp!=null) + { + System.out.print(temp.data + " "); + temp = temp.next; + } + } // Driver code