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
46 changes: 34 additions & 12 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@

//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file

// Time Complexity :O(1)
// Space Complexity :O(MAX) (MAX is size of stack)
// Did this code successfully run on Leetcode : ran code locally
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach
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

//check if array is empty by checking top value
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
//top shouldnot be >MAX
if (top >= MAX - 1) {
System.out.println("Stack Overflow");
return false;
}
a[++top] = x;
return true;
}

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

int peek()
{
//Write your code here
if (isEmpty()) {
System.out.println("Stack is Empty");
return 0;
}
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,3 +1,12 @@
// Time Complexity :O(1)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :ran code locally
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach


public class StackAsLinkedList {

StackNode root;
Expand All @@ -8,31 +17,44 @@ static class StackNode {

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


//check if stack empty
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.
StackNode newnode = new StackNode(data);

newnode.next = root; //point node to top
root = newnode; //update top to newnode
}

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;
}

int popped = root.data;
root = root.next; //move top to next node
return popped;
}

public int peek()
{
//Write code to just return the topmost element without removing it.
if (isEmpty()) {
System.out.println("Stack is Empty");
return 0;
}
return root.data;
}

//Driver code
Expand Down
56 changes: 39 additions & 17 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import java.io.*;

// Java program to implement
// Time Complexity :O(n) for insert and printList
// Space Complexity :O(n)
// Did this code successfully run on Leetcode :ran code locally
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach

// a Singly Linked List
public class LinkedList {

Expand All @@ -17,34 +25,48 @@ static class Node {
// Constructor
Node(int d)
{
//Write your code here
data = d;
next = null;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// 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

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

Node newnode = new Node(data);
// if linkedlist is empty then make newnode as head
if (list.head == null) {
list.head = newnode;
}
// else traverse till the last node and insert there
else {
Node currnode = list.head;

// traverse till the last node
while (currnode.next != null) {
currnode = currnode.next;
}

// Insert the newnode at last node
currnode.next = newnode;
}

return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
// Traverse through the LinkedList

// Print the data at current node

// Go to next node
Node currnode = list.head;

// traverse the LinkedList
while (currnode != null) {
System.out.print(currnode.data + " ");

// go to next node
currnode = currnode.next;
}
System.out.println();
}

// Driver code
Expand Down