From 225ff3422bb18e8ad930285148cc2c0a1e5aa7ce Mon Sep 17 00:00:00 2001 From: Abhinav <101085681+Abhinav-Sriharsha@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:06:02 -0700 Subject: [PATCH 1/2] Done PreCourse-1 --- Exercise_1.java | 22 +++++++++++++++- Exercise_3.java => LinkedList.java | 27 +++++++++++++++++-- Exercise_2.java => StackAsLinkedList.java | 32 ++++++++++++++++++++++- 3 files changed, 77 insertions(+), 4 deletions(-) rename Exercise_3.java => LinkedList.java (70%) rename Exercise_2.java => StackAsLinkedList.java (56%) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..04f90606c 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -7,29 +7,49 @@ class Stack { boolean isEmpty() { - //Write your code here + if(top==-1) + return true; + else + return false; } Stack() { //Initialize your constructor + top=-1; } boolean push(int x) { //Check for stack Overflow + if(top>MAX-1) + return false; + else + { + top=top+1; + a[top]=x; + return true; + } //Write your code here } int pop() { //If empty return 0 and print " Stack Underflow" + if(top==-1) + { + System.out.print("Stack Underflow"); + return 0; + } + else + return a[top--]; //Write your code here } int peek() { //Write your code here + return top; } } diff --git a/Exercise_3.java b/LinkedList.java similarity index 70% rename from Exercise_3.java rename to LinkedList.java index fb66d329d..5a192d405 100644 --- a/Exercise_3.java +++ b/LinkedList.java @@ -18,6 +18,9 @@ static class Node { Node(int d) { //Write your code here + this.data =d; + + } } @@ -25,9 +28,24 @@ static class 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 + // then make the new node as head + if(list.head==null) + { + list.head=newNode; + } + else + { + Node temp =list.head; + while(temp.next!= null) + { + temp=temp.next; + } + temp.next=newNode; + } + return list; // Else traverse till the last node // and insert the new_node there @@ -41,7 +59,12 @@ public static LinkedList insert(LinkedList list, int data) public static void printList(LinkedList list) { // Traverse through the LinkedList - + Node temp=list.head; + while(temp !=null) + { + System.out.println(temp.data+ " "); + temp=temp.next; + } // Print the data at current node // Go to next node diff --git a/Exercise_2.java b/StackAsLinkedList.java similarity index 56% rename from Exercise_2.java rename to StackAsLinkedList.java index 5a9c4868c..9260cf213 100644 --- a/Exercise_2.java +++ b/StackAsLinkedList.java @@ -8,6 +8,8 @@ static class StackNode { StackNode(int data) { + this.data=data; + this.next=null; //Constructor here } } @@ -16,16 +18,42 @@ static class StackNode { public boolean isEmpty() { //Write your code here for the condition if stack is empty. + + if(root==null) + return true; + return false; } public void push(int data) { - //Write code to push data to the stack. + //Write code to push data to the stack. + StackNode newNode=new StackNode(data) ; + if(root==null) + { + root=newNode; + newNode.next=null; + } + else{ + newNode.next=root; + root=newNode; + } } public int pop() { //If Stack Empty Return 0 and print "Stack Underflow" + if(root==null) + { + System.out.println("Stack Underflow"); + return 0; + } + else{ + StackNode temp =root; + int x=temp.data; + root=temp.next; + temp=null; + return x; + } //Write code to pop the topmost element of stack. //Also return the popped element } @@ -33,6 +61,8 @@ public int pop() public int peek() { //Write code to just return the topmost element without removing it. + StackNode temp= root; + return temp.data; } //Driver code From 6248bf0de67125f13b3ade9a9d653ba682d05da0 Mon Sep 17 00:00:00 2001 From: Abhinav <101085681+Abhinav-Sriharsha@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:08:46 -0700 Subject: [PATCH 2/2] Made changes to peek method --- Exercise_1.java | 12 +++++++++++- StackAsLinkedList.java | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Exercise_1.java b/Exercise_1.java index 04f90606c..ace79ccca 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,3 +1,6 @@ +// Time complexity O(1) for each operation +// Problems faced: Unable to calculate the time and spce complexity properly + class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file @@ -49,7 +52,13 @@ int pop() int peek() { //Write your code here - return top; + if(top==-1) + { + System.out.println("Stack is empty"); + return 0; + } + else + return a[top]; } } @@ -62,5 +71,6 @@ public static void main(String args[]) s.push(20); s.push(30); System.out.println(s.pop() + " Popped from stack"); + } } diff --git a/StackAsLinkedList.java b/StackAsLinkedList.java index 9260cf213..50112597b 100644 --- a/StackAsLinkedList.java +++ b/StackAsLinkedList.java @@ -61,8 +61,16 @@ public int pop() public int peek() { //Write code to just return the topmost element without removing it. + if(root==null) + { + System.out.println("Stack is empty"); + return 0; + } + else + { StackNode temp= root; return temp.data; + } } //Driver code