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
36 changes: 33 additions & 3 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
class Stack {
/*
Time Complexity:
push: O(1)
pop: O(1)
peak: O(1)

Space Complexity:
push: O(1)
pop: O(1)
peak: O(1)
*/
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 +18,48 @@ class Stack {

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

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

boolean push(int x)
{
//Check for stack Overflow
if(top >= MAX-1){
System.out.println("Stack Overflow");
return false;
}
//Write your code here
a[top+1] = x;
top +=1;
return true;
}

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

int peek()
{
//Write your code here
if(top <0){
System.out.println("Empty Stack");
return 0;
}
return a[top];
}
}

Expand Down
41 changes: 35 additions & 6 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
public class StackAsLinkedList {
/*
Time Complexity:
push: O(1)
pop: O(1)
peak: O(1)

Space Complexity:
push: O(1)
pop: O(1)
peak: O(1)
*/
public class StackAsLinkedList {

StackNode root;

Expand All @@ -8,31 +19,49 @@ static class StackNode {

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 newNode = new StackNode(data);
newNode.next = root;
root = newNode;
System.out.println("Pushed to stack");
}

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

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
64 changes: 46 additions & 18 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import java.io.*;

/*
Time Complexity:
insert: O(n) {Con be reduced to O(1) if we had tail node}
print: O(n)

Space Complexity:
insert: O(1)
print: O(1)
*/
// Java program to implement
// a Singly Linked List
public class LinkedList {

Node head; // head of list

Node head; // head of list
// Linked list Node.
// This inner class is made static
// so that main() can access it
Expand All @@ -17,42 +23,64 @@ 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
// If the Linked List is empty,
// 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

// Insert the new_node at last node
// Return the list by head
// and insert the new_node there
else{
Node currentNode = list.head;
while(currentNode.next != null){
currentNode = currentNode.next;
}
// Insert the new_node at last node
currentNode.next = newNode;
}
// Return the list by head
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
// Traverse through the LinkedList
if(list.head == null){
System.out.println("List is Empty");
}
// Print the data at current node

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

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

//
// ******INSERTION******
//
Expand Down