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
25 changes: 22 additions & 3 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,54 @@
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file

// Time Complexity :O(1) for all operations
// Space Complexity : O(n) for storing elements in array
// Did this code successfully run on Leetcode : NA

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

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

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(top == a.length -1){
return false;
}
else{
a[++top] = x;
return true;
}

}

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

int peek()
{
//Write your code here
if(top == -1) return -1;
return a[top];
}
}

Expand Down
27 changes: 23 additions & 4 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
public class StackAsLinkedList {
//TC - O(1) for all operations
//SC - O(n) for storing elements in linked list

public class Exercise_2 {

StackNode root;

Expand All @@ -8,38 +11,54 @@ static class StackNode {

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


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;
root = newNode;
}

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

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

//Driver code
public static void main(String[] args)
{

StackAsLinkedList sll = new StackAsLinkedList();
Exercise_2 sll = new Exercise_2();

sll.push(10);
sll.push(20);
Expand Down
Binary file added Exercise_3.class
Binary file not shown.
44 changes: 33 additions & 11 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Node head; // head of list

Expand All @@ -12,17 +12,21 @@ public class LinkedList {
static class Node {

int data;
Node next;
Node next;


// Constructor
Node(int d)
Node(int data)
{
//Write your code here
this.data = data;
this.next = null;

}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
public static Exercise_3 insert(Exercise_3 list, int data)
{
// Create a new node with given data

Expand All @@ -34,29 +38,47 @@ public static LinkedList insert(LinkedList list, int data)

// Insert the new_node at last node
// Return the list by head
Node newNode = new Node(data);
//LL is empty
if(list.head == null){
list.head = newNode;
return list;
}

// LL is not empty so traverse till last and then insert the element
Node temp = list.head;

while(temp.next != null){
temp = temp.next;
}
temp.next = newNode;
return list;

}

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

// Print the data at current node

// Go to next node
// Go to next node
Node temp = list.head;
while(temp != null){
System.out.print(+temp.data +" -> ");
temp = temp.next;
}
System.out.println("null");
}

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

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


// Insert the values
list = insert(list, 1);
list = insert(list, 2);
Expand Down