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
118 changes: 79 additions & 39 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,86 @@
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

boolean isEmpty()
{
//Write your code here
}

Stack()
{
//Initialize your constructor
}

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
}

int peek()
{
//Write your code here
}
}

// Driver code
class Main {
public static void main(String args[])
// Time Complexity : O(1) for push, pop, peek, isEmpty, isFull
// Space Complexity : O(1) as the size of the stack is fixed

class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
static final int MAX = 5;
int top = 0;
int stackArray[] = new int[MAX]; // Maximum size of Stack

// method to check if stack is empty
boolean isEmpty() {
//Write your code here
if (top == 0) {
return true;
}
return false;
}

// method to check if stack is full
boolean isFull() {
if (top == stackArray.length) {
return true;
}
return false;
}

// method to add an item to stack. It increases top by 1
boolean push(int value) {
//Check for stack Overflow
if (isFull()) {
System.out.println("Stack overflow for value " + value);
return false;
}

//Write your code here
stackArray[top] = value;
top = top + 1;
return true;
}

// method to remove an item from stack. It decreases top by 1
int pop() {
//If empty return 0 and print " Stack Underflow"
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}
//Write your code here
top = top - 1;
int peekValue = stackArray[top];
stackArray[top] = 0;
return peekValue;
}

// method to return top element in stack
int peek() {
//Write your code here
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}
return stackArray[top - 1];
}
}


class Main {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
}
s.push(30);
s.push(40);

for (int i: s.stackArray) {
System.out.println(i);
}
System.out.println(s.peek() + " Peeked from stack");
System.out.println(s.pop() + " Popped from stack");
}
}

115 changes: 67 additions & 48 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,71 @@
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data)
{
//Constructor here
}
}


public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
}

public void push(int data)
{
// Implement a Stack using Linked List
// Time Complexity : O(1) for push, pop, and peek operations
// Space Complexity : O(n) where n is the number of elements in the stack
class StackAsLinkedList {

StackNode head = null;

static class StackNode {
int data;
StackNode next;

// Constructor
StackNode(int value) {
data = value;
next = null;
}
}

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

// Method to push data onto the stack
public void push(int data) {
//Write code to push data to the stack.
}

public int pop()
{
//If Stack Empty Return 0 and print "Stack Underflow"
StackNode node = new StackNode(data);
node.next = head;
head = node;
System.out.println(data + " pushed to stack");
}

// method to pop data off the stack
public int pop() {
//If Stack Empty Return 0 and print "Stack Underflow"
if (isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}

//Write code to pop the topmost element of stack.
//Also return the popped element
}

public int peek()
{
//Also return the popped element
int poppedValue = head.data;
head = head.next;
return poppedValue;
}

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

return head.data;
}
}


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

StackAsLinkedList sll = new StackAsLinkedList();

sll.push(10);
sll.push(20);
sll.push(30);

System.out.println(sll.pop() + " popped from stack");

System.out.println("Top element is " + sll.peek());
}
}
public static void main(String[] args) {
StackAsLinkedList sll = new StackAsLinkedList();
sll.push(10);
sll.push(20);
sll.push(30);
System.out.println(sll.pop() + " popped from stack");
System.out.println("Top element is " + sll.peek());
}
}
67 changes: 38 additions & 29 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import java.io.*;
import java.io.*;

// Java program to implement
// a Singly Linked List
public class LinkedList {
// Java program to implement a Singly Linked List
// Time Complexity : O(n) for insert and printList
// Space Complexity : O(n) where n is the number of elements in the linked list
class LinkedList {

Node head; // head of list

Expand All @@ -15,50 +16,58 @@ static class Node {
Node next;

// Constructor
Node(int d)
{
//Write your code here
Node(int d) {
data = d;
next = null;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data
// Create a new node with given data
Node 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
// then make the new node as head
if (list.head == null) {
node.next = list.head;
list.head = 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;
}
last.next = node;
}
return list;

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

// Return the list by head
}

// 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
// Traverse through the LinkedList
Node last = list.head;
while (last.next != null) {
System.out.println(last.data);
last = last.next;
}
System.out.println(last.data);
}

// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();

//
// ******INSERTION******
//

//Start with the empty list.
LinkedList list = new LinkedList();

// Insert the values
list = insert(list, 1);
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
Expand All @@ -67,4 +76,4 @@ public static void main(String[] args)
// Print the LinkedList
printList(list);
}
}
}