diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..7d019aef3 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,35 +1,61 @@ class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file + + //Space complexity: O(MAX) -- array size + //Time complexity: O(1) for push, pop and peek operations + 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 Underflow"); + return 0; + } + return a[top]; } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..d12dd8419 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,5 +1,7 @@ -public class StackAsLinkedList { - +class StackAsLinkedList { + //Space complexity: O(n) -- number of nodes in the linked list + //Time complexity: O(1) for push, pop and peek operations + StackNode root; static class StackNode { @@ -8,35 +10,53 @@ 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. + //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. + //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 + //Also return the popped element + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + int poppedData = root.data; + root = root.next; + return poppedData; } public int peek() { //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) + public static void main(String[] args) { StackAsLinkedList sll = new StackAsLinkedList(); diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..fb21a5da5 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,88 @@ -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 - static class Node { - - int data; - Node next; - - // Constructor - Node(int d) - { - //Write your code here - } - } - - // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) - { - // Create a new node with given 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 - - // 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 - - // Print the data at current node - - // Go to next node - } - - // Driver code - public static void main(String[] args) - { +import java.io.*; + +// Java program to implement +// a Singly Linked List +class LinkedList { + +//Space and Time complexity: O(n) -- number of nodes in the linked list + Node head; // head of list + + // Linked list Node. + // This inner class is made static + // so that main() can access it + static class Node { + + int data; + Node next; + + // 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 newNode = 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 + + // Insert the new_node at last node + // Return the list by head + if (list.head == null) { + list.head = newNode; + return list; + } + + Node temp = list.head; + while (temp.next != null) { + temp = temp.next; + } + temp.next = newNode; + return list; + } + + // 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 + Node current = list.head; + while (current != null) { + System.out.print(current.data + " "); + current = current.next; + } + } + + // Driver code + public static void main(String[] args) + { /* Start with the empty list. */ - LinkedList list = new LinkedList(); - - // - // ******INSERTION****** - // - - // Insert the values - list = insert(list, 1); - list = insert(list, 2); - list = insert(list, 3); - list = insert(list, 4); - list = insert(list, 5); - - // Print the LinkedList - printList(list); - } + LinkedList list = new LinkedList(); + + // + // ******INSERTION****** + // + + // Insert the values + list = insert(list, 1); + list = insert(list, 2); + list = insert(list, 3); + list = insert(list, 4); + list = insert(list, 5); + + // Print the LinkedList + printList(list); + } } \ No newline at end of file