Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Exercise–1 #2102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
42 changes: 39 additions & 3 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Exercise_1 : Implement Stack using Array.
// Time Complexity : O(1)
// Space Complexity : O(n) where n is the number of items in the stack.


// Your code here along with comments explaining your approach
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
Expand All @@ -7,29 +13,59 @@ class Stack {

boolean isEmpty()
{
//Write your code here
//if stack is empty, then the top will be less than 0,
// where 0 will be the first index of the array.
return (top < 0);
}

Stack()
{
//Initialize your constructor
// since first element will go on the 0th index of the array.
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;
}
else{
a[++top] = x;
System.out.println(x + " pushed onto the stack");
return true;
}
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
if (top < 0){
System.out.println("Stack Underflow!");
return 0;
}
else{
int x = a[top--];
System.out.println(x + " popped put of the stack");
return x;
}
}

int peek()
{
//Write your code here
if (top < 0){
System.out.println("Stack Underflow!");
return 0;
}
else{
int x = a[top];
System.out.println(x + " is top of the stack");
return x;
}

}
}

Expand Down
40 changes: 37 additions & 3 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
public class StackAsLinkedList {
// Exercise_2 : Implement Stack using Linked List.
// Time Complexity : O(1)
// Space Complexity : O(n) where n is the number of items in the stack.
class StackAsLinkedList {

StackNode root;

Expand All @@ -8,31 +11,62 @@ static class StackNode {

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


StackAsLinkedList(){
this.root = 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 new_node = new StackNode(data);
if (new_node == null) {
System.out.println("Stack Overflow!");
return;
}
new_node.next = root;
root = 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 Overflow!");
return 0;
} else{
// current top as temp
StackNode temp = root;

// update top to next node
root = root.next;
return temp.data;
}

}

public int peek()
{
//Write code to just return the topmost element without removing it.
if (!isEmpty())
return root.data;
else {
System.out.println("Stack Is Empty!");
return Integer.MIN_VALUE;
}
}

//Driver code
Expand Down
32 changes: 27 additions & 5 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Exercise_3 : Implement Singly Linked List.
// Time Complexity : O(n)
// Space Complexity : O(n) where n is the number of items in the stack.
import java.io.*;

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

Node head; // head of list

Expand All @@ -18,33 +21,52 @@ static class Node {
Node(int d)
{
//Write your code here
this.data = d;
this.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

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;
// Below causes O(n) time complexity.
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)
{
Node current_node = list.head;
System.out.print("Printing LinkedList: \n");
// Traverse through the LinkedList

while(current_node != null){
// Print the data at current node

System.out.println(current_node.data + " ");
// Go to next node
current_node = current_node.next;
}
}

// Driver code
Expand Down
Binary file added LinkedList$Node.class
Binary file not shown.
Binary file added LinkedList.class
Binary file not shown.
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.