Skip to content
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
Binary file added Exercise_1.class
Binary file not shown.
126 changes: 81 additions & 45 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,82 @@
class Stack {
//Please read sample.java file before starting.
//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

boolean isEmpty()
{
//Write your code here
}

Stack()
{
//Initialize your constructor
}

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
}

int peek()
{
//Write your code here
}
}

// 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");
}

// Time Complexity : isEmpty - O(1), push - O(1), pop - O(1), peek - O(1)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :not really, but i had to think for a bit on how i will implement it as this was the first time i implement the stack, once i could visualise the stack operations in my mind it was easy to code

class Stack {
// Please read sample.java file before starting.
// Kindly include Time and Space complexity at top of each file
static final int MAX = 5;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

boolean isEmpty() {
// simplified the logic
return top == -1;
}

Stack() {
this.top = -1;
}

boolean push(int x) {
boolean pushed;
if (top == (MAX - 1)) {
System.out.println("Stack overflow");
pushed = false;
} else {
top++;
a[top] = x;
pushed = true;
}

return pushed;
}

int pop() {
int value;
if (isEmpty()) {
System.out.println("Stack underflow");
return Integer.MIN_VALUE;
} else {
value = a[top];
top--;
}
return value;
}

int peek() {
int value;
if (isEmpty()) {
System.out.println("Stack underflow");
return Integer.MIN_VALUE;
} else {

value = a[top];
}
return value;
}
}

// Driver code
class Exercise_1 {
public static void main(String args[]) {
Stack s = new Stack();
System.out.println(s.peek());
s.push(10);
System.out.println(s.peek());
s.push(20);
System.out.println(s.peek());
s.push(30);
System.out.println(s.peek());
s.push(40);
System.out.println(s.peek());
s.push(50);
System.out.println(s.pop() + " Popped from stack");
System.out.println(s.peek());
s.push(60);
System.out.println(s.peek());

}
}
131 changes: 79 additions & 52 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,79 @@
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());
}
}

// Time Complexity : isEmpty - O(1), push - O(1), pop - O(1), peek - O(1)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :I have to put a more effort for this one because i am not i a habit of working with linked lists to first i have to figure out the linked list part firt, after that it was a bit easy as i had already implement stack for the previous exericise.
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data) {
this.data = data;
next = null;

}
}

public boolean isEmpty() {
return root == null;
// Write your code here for the condition if stack is empty.
}

public void push(int data) {
// Null check added
StackNode temp = new StackNode(data);
if (isEmpty()) {
root = temp;

} else {
temp.next = root;
root = temp;
}
// Write code to push data to the stack.
}

public int pop() {
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
} else {
int num;
num = root.data;
root = root.next;
return num;
}
// 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.
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
} else {
int num;
num = root.data;
return num;
}
}

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