Skip to content
Open
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
50 changes: 37 additions & 13 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
class Stack {

// Time Complexity:
// push() - O(1)
// pop() - O(1)
// peek() - O(1)
// isEmpty() - 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
{
return top == -1;
}

Stack()
{
//Initialize your constructor
top = -1;
}

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(top >=MAX-1) {
return false;
}
else{
a[++top] = x;
return true;
}


}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
{
if(isEmpty())
{
System.out.println("Stack Underflow");
return 0;
}
else
{
return a[top--];
}
}

int peek()
{
//Write your code here
if(isEmpty())
return 0;
else
return a[top];
}
}

Expand All @@ -40,7 +64,7 @@ public static void main(String args[])
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
}
}