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
54 changes: 43 additions & 11 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,75 @@
// Time Complexity : o(1)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode : N/A it is not a leetcode problem
// Any problem you faced while coding this :No
// Your code here along with comments explaining your approach
// steps to implement stack using array
// 1. create a class stack with max size
// 2. create an array to hold stack elements and a variable top to track the top element index
// 3. implement isEmpty method to check if top is -1
// 4. implement push method to add element to stack and check for overflow
// 5. implement pop method to remove and return top element and check for underflow
// 6. implement peek method to return top element without removing it


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
int[] a;
private int capacity =1000;// Maximum size of Stack

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

Stack()
public Stack(int capacity)
{
//Initialize your constructor
a = new int[capacity];
top = -1;
}



boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(top == capacity -1){
return false;
}
a[++top] =x;
System.out.println("Pushed into the stack");
return true;
}

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

int peek()
{
//Write your code here
if(isEmpty()){
System.out.println("Stack is empty");
return -1;
}
return a[top];
}
}

// Driver code
class Main {
public static void main(String args[])
{
Stack s = new Stack();
Stack s = new Stack(10000);
s.push(10);
s.push(20);
s.push(30);
Expand Down
53 changes: 49 additions & 4 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,83 @@
public class StackAsLinkedList {
// Time Complexity : o(1)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode : N/A it is not a leetcode problem
// Any problem you faced while coding this :No
// Your code here along with comments explaining your approach
// steps to implement stack using LinkedList
//1 create node with data and next node. Intialize the constructor node with data.
//2 push : create new node and assign top of next node to new node
//3 pop: keep top node in temporary node and assing top to next node.
//4 peek: check for empty stack. if Not return top node data.
class StackAsLinkedList {

StackNode root;
private StackNode top;

public StackAsLinkedList()
{
//Initialize your constructor
top = null;
}


static class StackNode {
int data;
StackNode next;

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

}
}


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

}

public void push(int data)
{
{
//Write code to push data to the stack.
StackNode newNode = new StackNode(data);
newNode.next = top;
top = newNode;
System.out.println(data +"Pushed into 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(top == null){
System.out.println("Stack underflow");
return 0;
}
StackNode temp = top;
top = top.next;
int val = temp.data;
temp = null;
return val;

}

public int peek()
{
//Write code to just return the topmost element without removing it.
if(top == null){
System.out.println("stack is null");
return -1;
}
return top.data;
}

//Driver code
Expand Down
42 changes: 33 additions & 9 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

// Java program to implement
// a Singly Linked List
public class LinkedList {
// Time Complexity : o(n)
// Space Complexity :o(n)
// Did this code successfully run on Leetcode : N/A it is not a leetcode problem
// Any problem you faced while coding this :No
// Your code here along with comments explaining your approach
// 1.create node with data and next node and constructor
//2. insert new node: create new node with data. If list is empty then make new node as head. Else traverse until last node and insert new node to last node next.

class LinkedList {

Node head; // head of list

Expand All @@ -17,34 +25,50 @@ 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
Node new_node = new Node(data);

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

// Else traverse till the last node
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;

// Insert the new_node at last node
}
// Return the list by head
return list;

}

// 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
// Print the data at current node
// Go to next node
Node currNode = list.head;
System.out.println("LinkedList. ::");
while(currNode!=null){
// Print the data at current node
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
}

// Driver code
Expand Down