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
48 changes: 36 additions & 12 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
class Stack {
// Time Complexity:
// push() - O(1), Directly insert at the top index
// pop() - O(1), Access and remove the top element
// peek() - O(1), Access the top element without removing
// isEmpty() - O(1), Simple comparison check

// Space Complexity:
// O(n), where n = MAX (1000 in this case)

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,31 +16,46 @@ class Stack {

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

Stack()
{
//Initialize your constructor
//Initialize your constructor
top = -1; // Stack is initially 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
} else {
a[++top] = x; // Increment top and add element
return true;
}
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
}

int peek()
{
//Write your code here
}
}
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0; // Stack is empty
} else {
return a[top--]; // Return the top element and decrement top
}
}

int peek() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
} else {
return a[top];
}
}
}

// Driver code
class Main {
Expand Down
48 changes: 39 additions & 9 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
public class StackAsLinkedList {
// Time Complexity:
// push() - O(1), Inserting at the head takes constant time
// pop() - O(1), Removing the top node (head) is also constant time
// peek() - O(1), Accessing the top node without removing it
// isEmpty() - O(1), Simple null check

// Space Complexity:
// O(n), where n is the number of elements in the stack

public class StackAsLinkedList {

StackNode root;

Expand All @@ -8,31 +17,52 @@ static class StackNode {

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



public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
// condition if stack is empty
return root == null;
}

public void push(int data)
{
//Write code to push data to the stack.
// Push data to the stack
StackNode newNode = new StackNode(data);
// Check for stack Overflow
if (newNode == null) {
System.out.println("Stack Overflow");
return; // Stack is full
} else {
newNode.next = root; // Link the new node to the previous root
root = newNode; // Update root to point to the new 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
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0; // Stack is empty
}
// Pop the topmost element of stack
int poppedData = root.data;
root = root.next; // Move the root to the next node
return poppedData;
}

public int peek()
{
//Write code to just return the topmost element without removing it.
// return the topmost element without removing it
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
} else {
return root.data; // Return the data of the top node
}
}

//Driver code
Expand Down
150 changes: 83 additions & 67 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,86 @@
import java.io.*;

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

Node head; // head of list

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

int data;
Node next;

// Constructor
Node(int d)
{
//Write your code here
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given 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
// Time Complexity :
// insert() - O(n), where n is the number of nodes in the list
// because we traverse the entire list to insert at the end
// printList() - O(n), to print each node once

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

}

// 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
}

// Driver code
public static void main(String[] args)
{
// Space Complexity : O(n) where n is the maximum size of the stack.

import java.io.*;

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

Node head; // head of list

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

int data;
Node next;

// Constructor
Node(int d)
{
//Write your code here
data = d;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data
Node new_node = new Node(data);

// If the Linked List is empty,
// then make the new node as head
if (list.head == null) {
list.head = new_node; // Set the head to the new node
} else {
// Else traverse till the last node
Node last = list.head; // Start from head
while (last.next != null) { // Traverse till the last node
last = last.next; // Move to next node
}
// Insert the new_node at last node
last.next = new_node; // Link the new node at the end
}
return list; // Return the list by head
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
// Traverse through the LinkedList
Node currNode = list.head; // Start from head
while (currNode != null) {
System.out.print(currNode.data + " "); // Print the data at current node
currNode = currNode.next; // Move to next node
}
System.out.println();
}

// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();
//
// ******INSERTION******
//
// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
// Print the LinkedList
printList(list);
}
LinkedList list = new LinkedList();

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

// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);

// Print the LinkedList
printList(list);
}
}