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,4 +1,9 @@
class Stack {
//T.C - O(1) , S.C - O(n)
// Your code here along with comments explaining your approach
/*
* I take a top variable which keeps track of the top element of stack and I increment and decrement it depending
* upon push or pop operation*/
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;
Expand All @@ -7,29 +12,43 @@ class Stack {

boolean isEmpty()
{
//Write your code here
//Write your code here
return top == 0;
}

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

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

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

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

Expand Down
33 changes: 27 additions & 6 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
public class StackAsLinkedList {
//T.C - O(1) , S.C - O(n)
// Your code here along with comments explaining your approach
/*
* I take root node as top node which keeps track of the top element of stack and I append it to next nodes depending
* upon push or pop operation*/
class StackAsLinkedList {

StackNode root;
StackNode root;

static class StackNode {
int data;
StackNode next;

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


public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
//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.
//Write code to push data to the stack.
StackNode node = new StackNode(data);
node.next = root;
root = node;
}

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
//Also return the popped element
if(root == null) {
System.out.println("Stack underflow");
return 0;
}
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)
return 0;
return root.data;
}

//Driver code
Expand Down
43 changes: 34 additions & 9 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import java.io.*;
//T.C - O(1) for insertion at head and O(n) for insertion at the end. For traversal, O(n) as we iterate
// through all the nodes for printing, S.C - O(n)
// Your code here along with comments explaining your approach
/*
* I initialize the constructor with the incoming parameter and insert new node by checking if head is null and making the
* new node as head if null. If not, I safely append it by traversing till the end of the list. Similarly, I print
* the data values of each node by traversing them.
* */
import java.io.*;

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

Node head; // head of list

Expand All @@ -17,24 +25,36 @@ static class Node {
// Constructor
Node(int d)
{
//Write your code here
//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
// 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
// then make the new node as head
if(list.head == null)
list.head = newNode;


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

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

// Return the list by head
return list;
}

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

// Print the data at current node

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

// Driver code
Expand Down