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
49 changes: 38 additions & 11 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// Time Complexity :
// push(): O(1)
// pop(): O(1)
// peek(): O(1)
// isEmpty(): O(1)

// Space Complexity : O(MAX)

import java.io.IOException;

class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
Expand All @@ -7,40 +17,57 @@ class Stack {

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

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

boolean push(int x)
boolean push(int x) throws Exception
{
//Check for stack Overflow
//Write your code here
if(top>MAX){
throw new Exception("Stack Overflow");
}
else{
a[++top] = x;
return true;
}
}

int pop()
int pop() throws Exception
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
if(top<0){
throw new Exception("Stack Underflow");
}else{
return a[top--];
}
}

int peek()
int peek() throws Exception
{
//Write your code here
if(top<0){
throw new Exception("Stack is empty");
}else{
return a[top];
}
}
}

// Driver code
class Main {
public static void main(String args[])
public static void main(String args[]) throws Exception
{
try{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
System.out.println(s.pop() + " Popped from stack");
}catch(Exception e){
throw e;
}
}
}
41 changes: 32 additions & 9 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
public class StackAsLinkedList {
// Time Complexity :
// push(): O(1)
// pop(): O(1)
// peek(): O(1)
// isEmpty(): O(1)

// Space Complexity : O(n)
public class Exercise_2 {

StackNode root;

Expand All @@ -8,45 +15,61 @@ static class StackNode {

StackNode(int data)
{
//Constructor here
this.data = data;
this.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);
newNode.next = root;
root = newNode;
}

public int pop()
public int pop() throws Exception
{
//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()) {
throw new Exception("Stack Underflow");
}
int popped = root.data;
root = root.next;
return popped;
}

public int peek()
public int peek() throws Exception
{
//Write code to just return the topmost element without removing it.
if (isEmpty()) {
throw new Exception("Stack is Empty");
}
return root.data;
}

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

StackAsLinkedList sll = new StackAsLinkedList();

Exercise_2 sll = new Exercise_2();
try{
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());
}catch(Exception e){
throw e;
}
}
}
85 changes: 72 additions & 13 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import java.io.*;
// Time Complexity :
// insert(): O(n)
// printlist(): O(n)
// delete(): O(n)

// Space Complexity : O(n)
import java.io.*;

// Java program to implement
// a Singly Linked List
Expand All @@ -8,7 +14,7 @@ public class LinkedList {

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

int data;
Expand All @@ -18,45 +24,95 @@ static class Node {
Node(int d)
{
//Write your code here
data = d;
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 new_node = new Node(data);
new_node.next = null;

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

// Else traverse till the last node
// and insert the new_node there

Node last = list.head;
while (last.next != null) {
last = last.next;
}
// Insert the new_node at last node
last.next = new_node;
}
// Return the list by head

return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
Node currNode = list.head;

System.out.print("LinkedList: ");

// Traverse through the LinkedList

// Print the data at current node

// Go to next node
}
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");

// Go to next node
currNode = currNode.next;
}
System.out.println();
}

public static LinkedList delete(LinkedList list, int key) throws Exception {
Node currNode = list.head, prev = null;

// CASE 1: If head node has the key (data)
if (currNode != null && currNode.data == key) {
list.head = currNode.next; // Changed head
System.out.println(key + " found and deleted");
return list;
}

// CASE 2: Search for the key to be deleted
while (currNode != null && currNode.data != key) {
prev = currNode;
currNode = currNode.next;
}

// If key was present, unlink it
if (currNode != null) {
prev.next = currNode.next;
System.out.println(key + " found and deleted");
}

// CASE 3: The key was not present
if (currNode == null) {
throw new Exception(key + " not found");
}

return list;
}

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

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

try{
// Insert the values
list = insert(list, 1);
list = insert(list, 2);
Expand All @@ -65,6 +121,9 @@ public static void main(String[] args)
list = insert(list, 5);

// Print the LinkedList
printList(list);
printList(list);
}catch(Exception e){
throw new Exception(e);
}
}
}