diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..f54a1138c 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -7,29 +7,45 @@ class Stack { boolean isEmpty() { - //Write your code here + return top == -1; } Stack() { - //Initialize your constructor + this.top = -1; } boolean push(int x) { - //Check for stack Overflow - //Write your code here + if(top == MAX -1) { + throw new RuntimeException("Stack Overflow has occurred"); + } + else{ + a[++top]=x; + return true; + } } int pop() { - //If empty return 0 and print " Stack Underflow" - //Write your code here + if(top ==-1) + { + throw new RuntimeException("Stack Underflow has occurred"); + } + else { + return a[top--]; + } } int peek() - { - //Write your code here + { + if(top ==-1) + { + throw new RuntimeException("Stack Underflow has occurred"); + } + else { + return a[top]; + } } } diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..dc94a0597 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,4 @@ -public class StackAsLinkedList { +class StackAsLinkedList { StackNode root; @@ -8,31 +8,46 @@ static class StackNode { StackNode(int data) { - //Constructor here - } + this.data =data; + this.next =next; + } } - + 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 node = new StackNode(data); + node.next = root; + root = 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(root == null) + { + throw new RuntimeException("Stack Underflow has occurred"); + } + else { + int data = root.data; + root = root.next; + return data; + } } public int peek() - { - //Write code to just return the topmost element without removing it. + { + if(root == null) + { + throw new RuntimeException("Stack Underflow has occurred"); + } + else{ + return root.data; + } } //Driver code diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..83c21b330 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -2,7 +2,7 @@ // Java program to implement // a Singly Linked List -public class LinkedList { +class LinkedList { Node head; // head of list @@ -17,34 +17,39 @@ static class Node { // Constructor Node(int d) { - //Write your code here + data =d; } } // 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 + if(list.head == null){ + list.head = newNode; + } + else { + Node lastNode = list.head; + while(lastNode.next !=null){ + lastNode = lastNode.next; + } + lastNode.next = newNode; + } - // 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) { - // Traverse through the LinkedList - - // Print the data at current node - - // Go to next node + Node currentNode = list.head; + + while (currentNode != null){ + System.out.print(currentNode.data + " "); + currentNode = currentNode.next; + } } // Driver code