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
32 changes: 27 additions & 5 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Time Complexity : O(1) for push, pop, peek, and isEmpty
// Space Complexity : O(N) where N is the MAX size of the stack

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

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

Stack()
{
//Initialize your constructor
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 {
a[++top] = x;
System.out.println(x + " pushed into stack");
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;
} else {
int x = a[top--];
return x;
}
}

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

Expand Down
25 changes: 23 additions & 2 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class StackAsLinkedList {
class StackAsLinkedList {

StackNode root;

Expand All @@ -8,31 +8,52 @@ 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.
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;
System.out.println(data + " pushed to 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
if (root == null) {
System.out.println("Stack Underflow");
return 0;
} else {
int popped = root.data;
root = root.next;
return popped;
}
}

public int peek()
{
//Write code to just return the topmost element without removing it.
if (root == null) {
System.out.println("Stack Underflow");
return 0;
} else {
return root.data;
}
}

//Driver code
Expand Down
27 changes: 21 additions & 6 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
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 +16,35 @@ static class Node {
// 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 newNode = new Node(data);

// If the Linked List is empty,
// then make the new node as head

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

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

return list;
}

// Method to print the LinkedList.
Expand All @@ -45,6 +55,11 @@ public static void printList(LinkedList list)
// Print the data at current node

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

// Driver code
Expand Down