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
27 changes: 27 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* Time Complexity : O(1) for all operations (push, pop, peek, isEmpty)
* Space Complexity : O(N) where N is maximum size of the array (MAX)
*/
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
Expand All @@ -8,28 +12,51 @@ class Stack {
boolean isEmpty()
{
//Write your code here
// A stack is empty if top is at its intial value
return top<0;
}

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

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(top>=MAX){
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
if(top==-1) {
System.out.print("Stack Underflow");
return 0;
}
int element = a[top--];
return element;
}

int peek()
{
//Write your code here
if(top==-1){
System.out.println("Stack is Empty");
return 0;
}
else
return a[top];
}
}

Expand Down
38 changes: 30 additions & 8 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
/**
* Time Complexity: O(1) for all operations (push, pop, peek, isEmpty) as we only manipulate the head pointer.
* Space Complexity: O(N) where N is the number of elements in the stack.
*/
public 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()
{
//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.
StackNode newNode = new StackNode(data);
if(root==null){
root = newNode;
}else{
newNode.next = root;
root = newNode;
}
}

public int pop()
{
//If Stack Empty Return 0 and print "Stack Underflow"
//If Stack Empty Return 0 and print "Stack Underflow"
//Write code to pop the topmost element of stack.
//Also return the popped element
//Also return the popped element
if(root==null){
System.out.print("Stack Underflow");
return 0;
}
int poppedValue = root.data;
root = root.next;
return poppedValue;
}

public int peek()
{
//Write code to just return the topmost element without removing it.
if(root!=null)
return root.data;
else
return -1;
}

//Driver code
Expand Down
30 changes: 28 additions & 2 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Insert: Time Complexity: O(n) because we must traverse the entire list to find the end.
* Space Complexity: O(1)
*
* Print:Time Complexity: O(n) where n is the number of nodes.
* Space Complexity: O(1).
*/
import java.io.*;

// Java program to implement
Expand All @@ -17,7 +24,8 @@ static class Node {
// Constructor
Node(int d)
{
//Write your code here
this.data = d;
this.next = null;
}
}

Expand All @@ -34,7 +42,17 @@ public static LinkedList insert(LinkedList list, int data)

// Insert the new_node at last node
// Return the list by head

Node node = new Node(data);
if(list.head==null){
list.head = node;
}else{
Node last = list.head;
while(last.next!=null){
last = last.next;
}
last.next = node;
}
return list;
}

// Method to print the LinkedList.
Expand All @@ -45,6 +63,14 @@ public static void printList(LinkedList list)
// Print the data at current node

// Go to next node
Node currNode = list.head;
while(currNode!=null){
Node curr = list.head;

System.out.println(head.data);
head = head.next;

}
}

// Driver code
Expand Down