From c745908883dadd767641ad1a9d854e06cb897a3d Mon Sep 17 00:00:00 2001 From: mukul Date: Mon, 22 Dec 2025 10:55:53 +0530 Subject: [PATCH] Done with Precourse 1 --- Exercise_1.java | 19 +++ Exercise_3.java => LinkedList.java | 157 ++++++++++++---------- Exercise_2.java => StackAsLinkedList.java | 124 ++++++++++------- 3 files changed, 179 insertions(+), 121 deletions(-) rename Exercise_3.java => LinkedList.java (72%) rename Exercise_2.java => StackAsLinkedList.java (66%) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..67d2a1500 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -8,28 +8,47 @@ class Stack { boolean isEmpty() { //Write your code here + return (top == -1); } Stack() { //Initialize your constructor + this.top = -1; } boolean push(int x) { //Check for stack Overflow //Write your code here + if (top == MAX - 1) { + System.out.println("Stack Overflow! Cannot push " + x); + return false; + } + a[++top] = x; + System.out.println("Pushed: " + x); + return true; } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if (isEmpty()) { + System.out.println("Stack Underflow! Stack is empty."); + return -1; + } + return a[top--]; } int peek() { //Write your code here + if (isEmpty()) { + System.out.println("Stack is empty."); + return -1; + } + return a[top]; } } diff --git a/Exercise_3.java b/LinkedList.java similarity index 72% rename from Exercise_3.java rename to LinkedList.java index fb66d329d..0653ffadd 100644 --- a/Exercise_3.java +++ b/LinkedList.java @@ -1,70 +1,89 @@ -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); - } +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 new_node = new Node(data); + new_node.next = null; + + // If the Linked List is empty, + // then make the new node as head + if (list.head == null) { + list.head = new_node; + } + else { + // Else traverse till the last node + // and insert the new_node there + Node last = list.head; + while (last.next != null) { + last = last.next; + } + + // Insert the new_node at last node + last.next = new_node; + } + + // Return the list by head + return list; + } + + // Method to print the LinkedList. + public static void printList(LinkedList list) + { + // Traverse through the LinkedList + Node currNode = list.head; + + while (currNode != null) { + // Print the data at current node + System.out.print(currNode.data + " "); + + // Go to next node + currNode = currNode.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/Exercise_2.java b/StackAsLinkedList.java similarity index 66% rename from Exercise_2.java rename to StackAsLinkedList.java index 5a9c4868c..304ace42e 100644 --- a/Exercise_2.java +++ b/StackAsLinkedList.java @@ -1,52 +1,72 @@ -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()); - } -} +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 newNode = new StackNode(data); + newNode.next = root; + root = newNode; + } + + 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 popped = root.data; + root = root.next; + return popped; + } + } + + public int peek() + { + //Write code to just return the topmost element without removing it. + if (isEmpty()) { + System.out.println("Stack is Empty"); + return 0; + } else { + 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()); + } +}