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
36 changes: 26 additions & 10 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
class Stack {
/*
Time complexity isEmpty => O(1), push => O(1), pop => O(1)
*/


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

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

Stack()
{
//Initialize your constructor
top = 0 ;
size = 0;
}

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

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
if(top == 0) {
print(" Stack Underflow");
return 0;
}
return a[top--];
}

int peek()
{
//Write your code here
{
return a[top];
}
}

// Driver code
class Main {
public class Exercise_1 {
public static void main(String args[])
{
Stack s = new Stack();
Expand Down
35 changes: 30 additions & 5 deletions Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,56 @@ static class StackNode {

StackNode(int data)
{
//Constructor here
StackNode node = new StackNode();
node.data = data;
node.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 node = new StackNode(data);

if (root == null) {
root = node;
}
else {
node.next = root;
root = 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
//Also return the popped element
if( root == null) {
System.out.println("Stack Underflow");
return 0;
}
else {
int val = root.data;
root = root.next;
return val;
}
}

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

//Driver code
Expand Down
32 changes: 22 additions & 10 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.io.*;

// Java program to implement
// a Singly Linked List
// Time complexity: insert => O(n) where n is the size of the elements
// Space complexity: insert => O(1)
public class LinkedList {

Node head; // head of list
Expand All @@ -17,24 +17,36 @@ static class Node {
// Constructor
Node(int d)
{
//Write your code here
//Write your code here
Node a = new Node();
a.data = d;
a.next = null;

}
}

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

Node a = 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
if(list == null) {
head = a;
}
else {
Node curr = head;

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

while (curr.next != null) {
curr = curr.next;
}
curr.next = a;
return head;
}
}

// Method to print the LinkedList.
Expand Down