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
32 changes: 28 additions & 4 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,55 @@ class Stack {
int top;
int a[] = new int[MAX]; // Maximum size of Stack

//Time complexxity : O(1) for push, pop and peek operations
//Space complexity : O(n) where n is the number of elements in the stack

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

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

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;
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;
}
else {
return a[top--];
}
}

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

Expand Down
27 changes: 23 additions & 4 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public class StackAsLinkedList {
//Time Complexity : O(1) for push, pop and peek operations
//Space Complexity : O(n)
public class Exercise_2 {

StackNode root;

Expand All @@ -8,38 +10,55 @@ 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"
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}
int poppedData = root.data;
root = root.next;
return poppedData;
//Write code to pop the topmost element of stack.
//Also return the popped element
}

public int peek()
{
//Write code to just return the topmost element without removing it.
//Write code to just return the topmost element without removing it.
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}
return root.data;
}

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

StackAsLinkedList sll = new StackAsLinkedList();
Exercise_2 sll = new Exercise_2();

sll.push(10);
sll.push(20);
Expand Down
30 changes: 24 additions & 6 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//Time Complexity : O(n)
//Space Complexity : O(1)
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
Expand All @@ -18,34 +20,50 @@ 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 newNode = new Node(data);

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

if (list.head == null) {
list.head = newNode;
} else {
// Else traverse till the last node
// and insert the new_node there

Node current = list.head;
while (current.next != null) {
current = current.next;
}
// Insert the new_node at last node
current.next = newNode;

}
// Return the list by head

return list;
}

// 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.println(current.data);
// Go to next node
current = current.next;
}
System.out.println();
}
Comment on lines +57 to +66

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