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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# Ignore compiled Java files
*.class

# Ignore C++ executables
a.out
*.out
*.exe
Exercise_2

# Ignore temp files
*.o

Binary file added Exercise_1.class
Binary file not shown.
111 changes: 67 additions & 44 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,69 @@
class Stack {

// Time Complexity:
// push() -> O(1)
// pop() -> O(1)
// peek() -> O(1)
// isEmpty() -> O(1)
//
// Space Complexity: O(MAX)

import java.util.*;

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
}

Stack()
{
//Initialize your constructor
}

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
}

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

int peek()
{
//Write your code here
}
}

// Driver code
class Main {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
}
//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

Stack() { // Constructor here
top = -1;
}

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

int pop() {
//Your code here
//Check Stack Underflow as well
if (top < 0) {
System.out.println("Stack Underflow");
return -1;
}
return a[top--];
}

int peek() {
//Your code here
//Check empty condition too
if (top < 0) {
System.out.println("Stack is Empty");
return -1;
}
return a[top];
}

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

public class Exercise_1 {
public static void main(String[] args) {
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
}
}

Binary file added Exercise_2
Binary file not shown.
38 changes: 34 additions & 4 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
// Time Complexity:
// push() -> O(1)
// pop() -> O(1)
// peek() -> O(1)
// isEmpty() -> O(1)
//
// Space Complexity:
// O(n) — where n is the number of elements pushed onto the stack,
// because each push creates a new node in the linked list.
//
// Did this code successfully run on Leetcode?
// This was not a LeetCode problem, but the code runs successfully on terminal.
//
// Any problems faced while coding this:
// Understanding pointer manipulation and using StackNode** for push/pop.
#include <bits/stdc++.h>
using namespace std;

Expand All @@ -18,23 +33,38 @@ StackNode* newNode(int data)

int isEmpty(StackNode* root)
{
//Your code here
return !root; //Your code here
}

void push(StackNode** root, int data)
{
StackNode* stackNode = newNode(data);
stackNode->next = *root;
*root = stackNode;
//Your code here
}

int pop(StackNode** root)
{
//Your code here
if (isEmpty(*root)){
cout << "Stack Underflow\n";
return INT_MIN;
}
StackNode* temp = *root;
*root = (*root)->next;
int popped = temp -> data;
delete temp;
return popped;//Your code here
}

int peek(StackNode* root)
{
//Your code here
if (isEmpty(root)){
cout << "Stack is empty\n";
return INT_MIN;//Your code here
}
return root -> data;
}

int main()
{
Expand All @@ -49,4 +79,4 @@ int main()
cout << "Top element is " << peek(root) << endl;

return 0;
}
}
Binary file added Exercise_3$Node.class
Binary file not shown.
Binary file added Exercise_3.class
Binary file not shown.
141 changes: 72 additions & 69 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,73 @@
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:O(n)
//Space: O(n)
//On terminal it runs successfully
//Problems Faced: Understanding pointers
import java.io.*;

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

Node head; // head of list

// Linked list Node.
static class Node {
int data;
Node next;

// Constructor
Node(int d) {
this.data = d;
this.next = null;
}
}

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

// If list is empty
if (list.head == null) {
list.head = new_node;
}
else {
Node last = list.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}

return list; // <-- FIXED
}

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

while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}

System.out.println();
}

// Driver code
public static void main(String[] args)
{
Exercise_3 list = new Exercise_3();

list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);

printList(list);
}
}

// 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)
{
/* 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);
}
}
Binary file added Stack.class
Binary file not shown.
Binary file added StackAsLinkedList$StackNode.class
Binary file not shown.
Binary file added StackAsLinkedList.class
Binary file not shown.
Loading