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
120 changes: 81 additions & 39 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,88 @@
class Stack {
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)
{
//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()
{
// Time Complexity : O(1)
// Space Complexity : O(1)

//Write your code here
if(a.length == 0 || a == null){
return true;
}else{
return false;
}
}

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


boolean push(int x)
{
// Time Complexity : O(1)
// Space Complexity : O(1)

//Check for stack Overflow
if(top < MAX - 1){
top += 1;
a[top]= x;
return true;
}else{
System.out.println("***** Stack Overflow *****");
return false;
}
//Write your code here
}

int pop()
{
}

int pop()
{
// Time Complexity : O(1)
// Space Complexity : O(1)

//If empty return 0 and print " Stack Underflow"
if(top < 0){
System.out.print("***** Stack Underflow : ");
return 0;
}else{
top -= 1;
return a[top+1];
}
//Write your code here
}

int peek()
{
}

int peek()
{
// Time Complexity : O(1)
// Space Complexity : O(1)

//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");
}
if(top == -1){
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");
System.out.println(s.pop() + " Popped from stack");
System.out.println(s.pop() + " Popped from stack");
System.out.println(s.pop() + " Popped from stack");
}
}
135 changes: 85 additions & 50 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,87 @@
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"
class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

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


public boolean isEmpty() {
// Time Complexity : O(1)
// Space Complexity : O(1)

if (root == null) {
return true;
} else {
return false;
}
//Write your code here for the condition if stack is empty.
}

public void push(int data) {
// Time Complexity : O(1)
// Space Complexity : O(1)

//Write code to push data to the stack.
if (root == null) {
root = new StackNode(data);
return;
}
StackNode node = new StackNode(data);
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
}

public int peek()
{
//Also return the popped element

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

if (root == null) {
System.out.print("***** Stack Underflow : ");
return 0;
}
int popped = root.data;
root = root.next;
return popped;
}

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());
}
}
if (root == null) {
return 0;
}
return root.data;
}
}
class Main {
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(sll.pop() + " popped from stack");
System.out.println(sll.pop() + " popped from stack");
System.out.println(sll.pop() + " popped from stack");


System.out.println("Top element is " + sll.peek());
}
}
Loading