Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed PreCourse1 #1990

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 69 additions & 17 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,98 @@
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file

// Time Complexity : O(1)
// Space Complexity : O(MAX)

static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

boolean isEmpty()
{
//Write your code here
{
//this will check if the stack is empty
return (top < 0);
}

Stack()
{
//Initialize your constructor
{
//When top=-1, it means the stack is empty so initializing it
top = -1;
}

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
/*
Here we check two scenarios:
1. In above constructor, the top starts at -1, but as this is an array based scenario,
while inserting the first element, we need to have top = 0, so in else part we
increment the top first and then insert the element.
2. As mentioned, we need to check Stack Overflow, this is only possible if
array is out of bounds, as in step 1, we are incrementing top before inserting the
elements in it, so when we have already inserted the last element in stack before it
overflows and top will be MAX-1, so now when we try to push the element again, if top
is already MAX-1, it won't insert again as we need to increment top before inserting
the element which will cause Stack Overflow. Hence, we check top >= (MAX - 1).
*/

if (top >= (MAX - 1))
{
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
return true;
}

}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
/*
1. if top is less than 0. means the stack is empty, and we cannot pop an element.
2. if top is more than 0, suppose it is 1, we return the element at a[1] and then
decrement the value of top, top-- so top = 0.
*/
if(top < 0)
{
System.out.println("Stack Underflow");
return 0;
}
else {
return a[top--];
}
}

int peek()
{
//Write your code here
{
/*
Peek method is to get the element at the top of the stack without
removing it, so here we don't decrement top as we are not changing
the index of the top in array, we are just retrieving the element.
*/
if (top < 0)
{
System.out.println("No Element in the Stack");
return 0;
}
else {
return a[top];
}
}
}

// Driver code
class Main {
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");
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
}
}
127 changes: 108 additions & 19 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public class StackAsLinkedList {
// Time Complexity : O(1)
// Space Complexity : O(n) // n represents number of elements stored in the stack
class StackAsLinkedList {

StackNode root;

Expand All @@ -8,45 +10,132 @@ static class StackNode {

StackNode(int data)
{
//Constructor here
//Constructor here
this.data = data;
next = null;
}
}



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 root == null;
}

public void push(int data)
{
//Write code to push data to the stack.
//Write code to push data to the stack.
/*
Here for pushing data in to the stack using linked list, we will have pointer root
which is basically our head, so we will be checking for two scenarios:
1. If the stack is empty then insert the first element, so using linked list if we
want to insert 6
eg: So if stack is empty, root will be pointing to null.
null
root

Now, if want to insert 6, we will add the new node and root will point to the node
6 -> null
root

2. If the stack is not empty and there are elements, we will break it down in to two steps

First, when we create a new node and put element 9, we need to make its next pointer pointing
to the root so it is connected to the stack.
eg: insert 9 in above example

node.next
` ↓
9 -> 6 -> null
root

Second is to update the root to the new node so it wil the at the top of the stack

9 -> 6 -> null
root
*/
StackNode node = new StackNode(data);
if (root == null)
{
root = node;
}
else {
node.next = root;
root = 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
// Write code to pop the topmost element of stack.
//Also return the popped element
/*
To pop the element from a stack we need to check two scenarios:
1. If the stack is empty, meaning root is pointing to null, so return 0.
null
root

2. If the stack has elements,
9 -> 6 -> 1 -> null
root

we will need to get the data of the node to which root is pointing, and then we
need to change the root to point to the next node

9 6 -> 1 -> null
root

one we point to the root to the next node, the connection from the previous node breaks
*/
if (root == null)
{
System.out.println("Stack Underflow");
return 0;
}
else {
int popElement = root.data;
root = root.next;
return popElement;
}
}

public int peek()
{
//Write code to just return the topmost element without removing it.
/*
Here root.data will give the top most element in the stack and as we are not moving
the pointer of root, it will not remove the element
*/
if (root == null)
{
System.out.println("No Element in the Stack");
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());
{

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());
}
}
Loading