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
35 changes: 26 additions & 9 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
// Time Complexity : push, pop and peek operations --> O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :

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
return top == -1; //return true if stack is empty
}

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

boolean push(int x)
boolean push(int x) //time: O(1), space: O(1)
{
//Check for stack Overflow
//Write your code here
if(top == MAX-1) //top reached last index indicating stack overflow
return false;
a[++top] = x; //increment the top and then store x
return true;
}

int pop()
int pop() //time: O(1), space: O(1)
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
if(top == -1)
{
System.out.println("Stack Underflow"); //if stack empty, returns 0
return 0;
}

return a[top--]; //returns the top of the stack then decrements top
}

int peek()
int peek() //time: O(1), space: O(1)
{
//Write your code here
if(top == -1)
return -1;
return a[top]; //same as pop but top isn't decremented
}
}

Expand Down
36 changes: 29 additions & 7 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
// Time Complexity : push, pop and peek operations --> O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :


public class StackAsLinkedList {

StackNode root;
StackNode root; //points to the top of the stack

static class StackNode {
int data;
StackNode next;

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


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

public void push(int data)
public void push(int data) //time: O(1), space: O(1)
{
//Write code to push data to the stack.
StackNode new_node = new StackNode(data); //creating a new node
new_node.next = root; //new node points to the top of the list
root = new_node; //new node is reassigned as the top
}

public int pop()
public int pop() //time: O(1), space: O(1)
{
//If Stack Empty Return 0 and print "Stack Underflow"
//Write code to pop the topmost element of stack.
//Also return the popped element
if(root == null) //checks if list is empty
{
System.out.println("Stack Underflow");
return 0;
}

int temp = root.data; //saves root data before we unlink it from the list
root = root.next; //moving root to the current top element in stack
return temp; //returning saved value

}

public int peek()
public int peek() //time: O(1), space: O(1)
{
//Write code to just return the topmost element without removing it.
if(root == null) //checking if there's any elements in the list
return -1;
return root.data; //returning the root node data which is top of the stack
}

//Driver code
Expand Down
45 changes: 27 additions & 18 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Time Complexity : O(n)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :

import java.io.*;

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

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
public static LinkedList insert(LinkedList list, int data) //time:O(n), space:O(1)
{
// Create a new node with given data

// If the Linked List is empty,
// then make the new node as head

// Else traverse till the last node
// and insert the new_node there
Node new_node = new Node(data); //create a new node
if(list.head == null) //checks if list empty. if yes, assigns new node as head
list.head = new_node;
else
{
Node tail = list.head; //else creates a tail node and traverse through the list
while(tail.next != null)
tail = tail.next;

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

tail.next = new_node; //adds node at the end of the lisr
}

return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
public static void printList(LinkedList list) //time:O(n), space:O(1)
{
// Traverse through the LinkedList

// Print the data at current node

// Go to next node
Node current = list.head;
while(current != null) //current node traverses through the end of the list prints each value
{
System.out.println(current.data + " ");
current = current.next;
}
}

// Driver code
Expand Down