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
33 changes: 33 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// Time Complexity : O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes - implement stack using queues
// Any problem you faced while coding this :


// 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
Expand All @@ -8,28 +16,53 @@ class Stack {
boolean isEmpty()
{
//Write your code here
if(a.length == 0)
{
return true;
}
return false;
}

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

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
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
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 -1;
}
return a[top];
}
}

Expand Down
52 changes: 0 additions & 52 deletions Exercise_2.java

This file was deleted.

47 changes: 45 additions & 2 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// Time Complexity :O(1) for insert and O(n) for iterate
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :


// Your code here along with comments explaining your approach

import java.io.*;

// Java program to implement
// a Singly Linked List
public class LinkedList {

Node head; // head of list
int size;

LinkedList()
{
this.head = null;
this.size = 0;
}

// Linked list Node.
// This inner class is made static
Expand All @@ -18,13 +33,16 @@ static class Node {
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)
{
// Create a new node with given data
Node newNode = new Node(data);

// If the Linked List is empty,
// then make the new node as head
Expand All @@ -34,7 +52,21 @@ public static LinkedList insert(LinkedList list, int data)

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


if(list.size == 0)
{
list.head = newNode;
list.size++;
return list;
}
Node curr = list.head;
while(curr.next != null)
{
curr = curr.next;
}
curr.next = newNode;
list.size++;
return list;
}

// Method to print the LinkedList.
Expand All @@ -44,7 +76,18 @@ public static void printList(LinkedList list)

// Print the data at current node

// Go to next node
// Go to next node
if(list.size == 0)
{
System.out.println("List is empty");
return;
}
Node curr = list.head;
while(curr != null)
{
System.out.println(curr.data);
curr = curr.next;
}
}

// Driver code
Expand Down
102 changes: 102 additions & 0 deletions StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Time Complexity : O(1) for push, isEmpty, and peek and O(n) for pop
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :yes - implement stack using queues
// Any problem you faced while coding this :


// Your code here along with comments explaining your approach
public class StackAsLinkedList {

StackNode root;
StackNode top;

StackAsLinkedList()
{
this.root = null;
this.top = null;
}

static class StackNode {
int data;
StackNode next;

StackNode(int data)
{
//Constructor here
this.data = data;
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.
StackNode newNode = new StackNode(data);
if(null == root)
{
root = newNode;
top = newNode;
}
else
{
top.next = newNode;
top = 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 topElement = top.data;
StackNode curr = root;
while(curr.next != top)
{
curr = curr.next;
}
top = curr;
top.next = null;

return topElement;
}

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

//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());
}
}