diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..54392cb8c 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,3 +1,5 @@ +// Time Complexity: O(1) for all operations +// Space Complexity: O(n) //n=size of Stack class Stack { //Please read sample.java file before starting. //Kindly include Time and Space complexity at top of each file @@ -8,28 +10,53 @@ class Stack { boolean isEmpty() { //Write your code here + if(top==-1) + { return true;} + + return false; + } 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; + }else + {top++; + a[top]=x; + return true;} } int pop() { //If empty return 0 and print " Stack Underflow" - //Write your code here + if(top==-1){ + System.out.println("Stack Underflow"); + return 0; + + }else{ + int m=a[top]; + top--; + return m; + } } int peek() { //Write your code here + if(top==-1){ + return -1; + } + return a[top]; } } diff --git a/LinkedList.java b/LinkedList.java new file mode 100644 index 000000000..6adc32872 --- /dev/null +++ b/LinkedList.java @@ -0,0 +1,91 @@ +import java.io.*; +//Time Complexity : O(n) +//Space Complexity : O(n) + +// 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 + 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 temp=list.head; + while(temp.next!=null){ + temp=temp.next; + } + // Insert the new_node at last node + temp.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 cNode=list.head; + while(cNode!=null){ + // Print the data at current node + System.out.print(cNode.data + " "); + // Go to next node + cNode=cNode.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); + } +} \ No newline at end of file diff --git a/StackAsLinkedList.java b/StackAsLinkedList.java new file mode 100644 index 000000000..dc0615dab --- /dev/null +++ b/StackAsLinkedList.java @@ -0,0 +1,86 @@ +//Time Complexity: O(1) +//Space Complexity: O(n) + +public class StackAsLinkedList { + + StackNode root; + + static class StackNode { + int data; + StackNode next; + + 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. + if(root==null) + return true; + + return false; + + } + + public void push(int data) + { + //Write code to push data to the stack. + StackNode newnode=new StackNode(data); + if(root == null){ + root=newnode; + }else{ + StackNode temp=root; + root=newnode; + newnode.next=temp; + + } + + } + + 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.print("Stack Underflow"); + return 0; + + } + int popped=root.data; + root=root.next; + return popped; + } + + public int peek() + { + //Write code to just return the topmost element without removing it. + if(isEmpty()){ + System.out.println("Stack Is Empty"); + return 0; + } + return root.data; + } + + //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()); + } +}