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
Binary file added Exercise_1.class
Binary file not shown.
42 changes: 30 additions & 12 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
// Time Complexcity :
// push() - o(1)
// pop() - o(1)
// peek() - o(1)
// isEmpty() - o(1)
// Space Complexity : o(MAX)

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

}

Stack()
{
//Initialize your constructor
top = -1 ;
}

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 (isEmpty()){
System.out.println("Stack Underflow");
return 0;
}
return a[top--];
}



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

Expand Down
Binary file added LinkedList$Node.class
Binary file not shown.
Binary file added LinkedList.class
Binary file not shown.
38 changes: 22 additions & 16 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import java.io.*;

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

Expand All @@ -17,36 +14,45 @@ 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
// and insert the new_node there
if (list.head == null){
list.head = new_node;
return list;
}
Node current = list.head;
while (current.next != null) {
current = current.next;
}
current.next = new_node;
return 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

Node current = list.head;
while (current != null) {
// Print the data at current node

System.out.print(current.data + " ");
// Go to next node
}

current = current.next;
}
}

// Driver code
public static void main(String[] args)
{
Expand Down
Binary file added Main.class
Binary file not shown.
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.
32 changes: 22 additions & 10 deletions Exercise_2.java → StackAsLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,45 @@ static class StackNode {

StackNode(int data)
{
//Constructor here
this.data = data;
this.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 newNode = new StackNode(data);
newNode.next = root;
root = newNode;
}

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 is Empty");
return 0;
}
int popped = root.data;
root = root.next;
return popped;
}

public int peek()
{
//Write code to just return the topmost element without removing it.
}

if (isEmpty()){
System.out.println("Stack is Empty");
return 0;
}
return root.data;
}

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