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
39 changes: 31 additions & 8 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
class Stack {
// Time Complexity: O(1) for push, pop, and peek operations
// Space Complexity: O(n) where n is the number of elements in the stack

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;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

boolean isEmpty()
{
//Write your code here
{
return top < 0; // If top is less than 0, stack is empty
}

Stack()
{
//Initialize your constructor
top = -1; // Initialize top to -1 indicating stack is empty
}

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


top ++;
a[top] = x;
return true;
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}

int poppedValue = a[top];
top--; // Decrease the top index
return poppedValue; // Return the popped value
}

int peek()
{
//Write your code here
//If empty return 0 and print " Stack Underflow"
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}

return a[top]; // Return the top element without removing it
}
}

Expand Down
52 changes: 0 additions & 52 deletions Exercise_2.java

This file was deleted.

35 changes: 27 additions & 8 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import java.io.*;

// Time Complexity: O(n) for insertion and printing
// Space Complexity: O(n) where n is the number of nodes in the linked list
// Java program to implement
// a Singly Linked List
public class LinkedList {
Expand All @@ -17,15 +18,17 @@ static class Node {
// Constructor
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
// If the Linked List is empty,
// then make the new node as head

Expand All @@ -34,7 +37,18 @@ public static LinkedList insert(LinkedList list, int data)

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


Node new_node = new Node(data);
if (list.head == null) {
list.head = new_node; // If list is empty, set head to new node
} else {
Node last = list.head; // Start from head
while (last.next != null) { // Traverse to the last node
last = last.next;
}
last.next = new_node; // Insert new node at the end
}
return list; // Return the updated list
}

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

// Print the data at current node

// Go to next node
// Go to next node
Node currNode = list.head; // Start from head
while (currNode != null) { // Traverse until the end of the list
System.out.print(currNode.data + " "); // Print current node's data
currNode = currNode.next; // Move to the next node
}
}

// Driver code
Expand Down
79 changes: 79 additions & 0 deletions StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Time Complexity: O(1) for push, pop, and peek operations
// Space Complexity: O(n) where n is the number of elements in the stack
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

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


public boolean isEmpty()
{
//If root is null then stack is empty
return root == null;
}

public void push(int data)
{
StackNode newNode = new StackNode(data);
//If stack is empty, set root to newNode
if (root == null) {
root = newNode;
} else {
//If stack is not empty, set newNode's next to current root and update root
newNode.next = root;
root = newNode;
}
}

public int pop()
{
//If Stack Empty Return 0 and print "Stack Underflow"
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0; // Return 0 if stack is empty
}

//Store the topmost element in a variable
int poppedValue = root.data;

//Update root to the next element
root = root.next;

//Return the popped element
return poppedValue;
}

public int peek()
{
if(isEmpty()) {
System.out.println("Stack Underflow");
}

return root.data; // Get the top element
}

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

StackAsLinkedList sll = new StackAsLinkedList();

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