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
54 changes: 0 additions & 54 deletions Exercise_1.cpp

This file was deleted.

35 changes: 34 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,62 @@ class Stack {
int top;
int a[] = new int[MAX]; // Maximum size of Stack

// Time Complexity: O(1)
// Space Complexity: O(1)
boolean isEmpty()
{
//Write your code here
return top ==0;
}

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

// Time Complexity: O(1)
// Space Complexity: O(1)
boolean push(int x)
{
//Check for stack Overflow
//Write your code here

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

}

// Time Complexity: O(1)
// Space Complexity: O(1)
int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
if(isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}

return a[--top];
}

// Time Complexity: O(1)
// Space Complexity: O(1)
int peek()
{
//Write your code here
if (isEmpty()) {
System.out.println("Stack is Empty");
return 0;
}
return a[top-1];
}
}

Expand All @@ -39,8 +70,10 @@ public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(20);
s.pop();
s.push(30);
s.pop();
System.out.println(s.pop() + " Popped from stack");
}
}
35 changes: 0 additions & 35 deletions Exercise_1.js

This file was deleted.

24 changes: 0 additions & 24 deletions Exercise_1.py

This file was deleted.

52 changes: 0 additions & 52 deletions Exercise_2.cpp

This file was deleted.

34 changes: 31 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class StackAsLinkedList {
public class Exercise_2 {

StackNode root;

Expand All @@ -9,37 +9,65 @@ static class StackNode {
StackNode(int data)
{
//Constructor here
this.data = data;
this.next = null;
}
}


// Time Complexity: O(1)
// Space Complexity: O(1)
public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
return root == null;

}

// Time Complexity: O(1)
// Space Complexity: O(1)
public void push(int data)
{
//Write code to push data to the stack.
StackNode newNode = new StackNode(data);
newNode.next = root;
root = newNode;

}

// Time Complexity: O(1)
// Space Complexity: O(1)
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;
}
StackNode temp = root;
root = root.next;
temp.next = null;
return temp.data;

}

// Time Complexity: O(1)
// Space Complexity: O(1)
public int peek()
{
//Write code to just return the topmost element without removing it.
if (isEmpty()) {
return 0;
}
return root.data;
}

//Driver code
public static void main(String[] args)
{

StackAsLinkedList sll = new StackAsLinkedList();
Exercise_2 sll = new Exercise_2();

sll.push(10);
sll.push(20);
Expand Down
36 changes: 0 additions & 36 deletions Exercise_2.js

This file was deleted.

32 changes: 0 additions & 32 deletions Exercise_2.py

This file was deleted.

Loading