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: 35 additions & 11 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
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;
class Stack {

// Time Complexity : O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : 12/18 test cases passed, the test with inputs ["MyStack","empty"] failed
// Any problem you faced while coding this : No but I did not understand why the Leetcode test case failed
static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

boolean isEmpty()
{
//Write your code here
{
// If a is empty, return true
return a.length == 0;
}

Stack()
{
//Initialize your constructor
{
// Initialize the top of the stack to be -1
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;
}
else {
top++;
a[top] = x;
return true;
}
}

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

int peek()
{
//Write your code here
if(isEmpty()){
return 0;
}
else{
return a[top];
}
}
}

Expand Down
70 changes: 0 additions & 70 deletions Exercise_3.java

This file was deleted.

94 changes: 94 additions & 0 deletions LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Java program to implement
// a Singly Linked List

// Time Complexity : O(n) to add element to the end of the LL
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this: No

public class LinkedList {

Node head; // head of list

// Linked list Node.
// This inner class is made static
// so that main() can access it
static class Node {

int data;
Node next;

// 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)
{
// Create a new node with given data
Node temp = new Node(data);

// If the Linked List is empty,
// then make the new node as head
if(list.head == null){
list.head = temp;
return list;
}

// Else traverse till the last node
// and insert the new_node there
Node pointer = list.head;
while(pointer.next != null){
pointer = pointer.next;
}

// Insert the new_node at last node
pointer.next = temp;
// Return the list by head
return list;

}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
// Traverse through the LinkedList
// if the list is empty
if(list.head == null){
return;
}
Node pointer = list.head;
while(pointer != null){
// Print the data at current node
System.out.println(pointer.data);
// Go to next node
pointer = pointer.next;
}
}

// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();

//
// ******INSERTION******
//

// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);

// Print the LinkedList
printList(list);
}
}
130 changes: 78 additions & 52 deletions Exercise_2.java → StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,78 @@
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"
//Write code to pop the topmost element of stack.
//Also return the popped element
}

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());
}
}
// Time Complexity : O(1) for all operations
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this: No

public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

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


public boolean isEmpty()
{
return root == null;
}

public void push(int data)
{
StackNode temp = new StackNode(data);
if (root == null){
root = temp;
}
else{
temp.next = root;
root = temp;
}
}

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(root == null){
System.out.println("Stack Underflow");
return 0;
}
else{
int poppedData = root.data;
root = root.next;
return poppedData;
}
}

public int peek()
{
//Write code to just return the topmost element without removing it.
if(root == null){
return 0;
}
else return root.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());
}
}