├── Exercise_1.cpp ├── Exercise_1.java ├── Exercise_1.js ├── Exercise_1.py ├── Exercise_2.cpp ├── Exercise_2.java ├── Exercise_2.js ├── Exercise_2.py ├── Exercise_3.cpp ├── Exercise_3.java ├── Exercise_3.js ├── Exercise_3.py ├── README.md └── Sample.java /Exercise_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define MAX 1000 6 | 7 | class Stack { 8 | //Please read sample.java file before starting. 9 | //Kindly include Time and Space complexity at top of each file 10 | int top; 11 | 12 | public: 13 | int a[MAX]; // Maximum size of Stack 14 | 15 | Stack() { //Constructor here } 16 | bool push(int x); 17 | int pop(); 18 | int peek(); 19 | bool isEmpty(); 20 | }; 21 | 22 | bool Stack::push(int x) 23 | { 24 | //Your code here 25 | //Check Stack overflow as well 26 | } 27 | 28 | int Stack::pop() 29 | { 30 | //Your code here 31 | //Check Stack Underflow as well 32 | } 33 | int Stack::peek() 34 | { 35 | //Your code here 36 | //Check empty condition too 37 | } 38 | 39 | bool Stack::isEmpty() 40 | { 41 | //Your code here 42 | } 43 | 44 | // Driver program to test above functions 45 | int main() 46 | { 47 | class Stack s; 48 | s.push(10); 49 | s.push(20); 50 | s.push(30); 51 | cout << s.pop() << " Popped from stack\n"; 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Exercise_1.java: -------------------------------------------------------------------------------- 1 | class Stack { 2 | //Please read sample.java file before starting. 3 | //Kindly include Time and Space complexity at top of each file 4 | static final int MAX = 1000; 5 | int top; 6 | int a[] = new int[MAX]; // Maximum size of Stack 7 | 8 | boolean isEmpty() 9 | { 10 | //Write your code here 11 | } 12 | 13 | Stack() 14 | { 15 | //Initialize your constructor 16 | } 17 | 18 | boolean push(int x) 19 | { 20 | //Check for stack Overflow 21 | //Write your code here 22 | } 23 | 24 | int pop() 25 | { 26 | //If empty return 0 and print " Stack Underflow" 27 | //Write your code here 28 | } 29 | 30 | int peek() 31 | { 32 | //Write your code here 33 | } 34 | } 35 | 36 | // Driver code 37 | class Main { 38 | public static void main(String args[]) 39 | { 40 | Stack s = new Stack(); 41 | s.push(10); 42 | s.push(20); 43 | s.push(30); 44 | System.out.println(s.pop() + " Popped from stack"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Exercise_1.js: -------------------------------------------------------------------------------- 1 | class Stack { 2 | //Please read sample.java file before starting. 3 | //Kindly include Time and Space complexity at top of each file 4 | ​ 5 | constructor() { 6 | //Initialize your constructor 7 | this.MAX = 1000; 8 | this.top = -1; 9 | this.a = new Array(this.MAX); 10 | } 11 | ​ 12 | function isEmpty() { 13 | //Write your code here 14 | } 15 | ​ 16 | function push(x) { 17 | //Check for stack Overflow 18 | //Write your code here 19 | } 20 | ​ 21 | function pop() { 22 | //If empty return 0 and print " Stack Underflow" 23 | //Write your code here 24 | } 25 | ​ 26 | function peek() { 27 | //Write your code here 28 | } 29 | } 30 | ​ 31 | let s = new Stack(); 32 | s.push(10); 33 | s.push(20); 34 | s.push(30); 35 | console.log(s.pop() + " Popped from stack"); 36 | -------------------------------------------------------------------------------- /Exercise_1.py: -------------------------------------------------------------------------------- 1 | class myStack: 2 | #Please read sample.java file before starting. 3 | #Kindly include Time and Space complexity at top of each file 4 | def __init__(self): 5 | 6 | def isEmpty(self): 7 | 8 | def push(self, item): 9 | 10 | def pop(self): 11 | 12 | 13 | def peek(self): 14 | 15 | def size(self): 16 | 17 | def show(self): 18 | 19 | 20 | s = myStack() 21 | s.push('1') 22 | s.push('2') 23 | print(s.pop()) 24 | print(s.show()) 25 | -------------------------------------------------------------------------------- /Exercise_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // A structure to represent a stack 5 | class StackNode { 6 | public: 7 | int data; 8 | StackNode* next; 9 | }; 10 | 11 | StackNode* newNode(int data) 12 | { 13 | StackNode* stackNode = new StackNode(); 14 | stackNode->data = data; 15 | stackNode->next = NULL; 16 | return stackNode; 17 | } 18 | 19 | int isEmpty(StackNode* root) 20 | { 21 | //Your code here 22 | } 23 | 24 | void push(StackNode** root, int data) 25 | { 26 | //Your code here 27 | } 28 | 29 | int pop(StackNode** root) 30 | { 31 | //Your code here 32 | } 33 | 34 | int peek(StackNode* root) 35 | { 36 | //Your code here 37 | } 38 | 39 | int main() 40 | { 41 | StackNode* root = NULL; 42 | 43 | push(&root, 10); 44 | push(&root, 20); 45 | push(&root, 30); 46 | 47 | cout << pop(&root) << " popped from stack\n"; 48 | 49 | cout << "Top element is " << peek(root) << endl; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Exercise_2.java: -------------------------------------------------------------------------------- 1 | public class StackAsLinkedList { 2 | 3 | StackNode root; 4 | 5 | static class StackNode { 6 | int data; 7 | StackNode next; 8 | 9 | StackNode(int data) 10 | { 11 | //Constructor here 12 | } 13 | } 14 | 15 | 16 | public boolean isEmpty() 17 | { 18 | //Write your code here for the condition if stack is empty. 19 | } 20 | 21 | public void push(int data) 22 | { 23 | //Write code to push data to the stack. 24 | } 25 | 26 | public int pop() 27 | { 28 | //If Stack Empty Return 0 and print "Stack Underflow" 29 | //Write code to pop the topmost element of stack. 30 | //Also return the popped element 31 | } 32 | 33 | public int peek() 34 | { 35 | //Write code to just return the topmost element without removing it. 36 | } 37 | 38 | //Driver code 39 | public static void main(String[] args) 40 | { 41 | 42 | StackAsLinkedList sll = new StackAsLinkedList(); 43 | 44 | sll.push(10); 45 | sll.push(20); 46 | sll.push(30); 47 | 48 | System.out.println(sll.pop() + " popped from stack"); 49 | 50 | System.out.println("Top element is " + sll.peek()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Exercise_2.js: -------------------------------------------------------------------------------- 1 | class StackAsLinkedList { 2 | ​ 3 | static stackNode = class { 4 | ​ 5 | constructor(d) { 6 | //Constructor here 7 | this.data = d; 8 | this.next = null; 9 | } 10 | } 11 | ​ 12 | function isEmpty() { 13 | //Write your code here for the condition if stack is empty. 14 | } 15 | ​ 16 | function push(data) { 17 | //Write code to push data to the stack. 18 | } 19 | ​ 20 | function pop() { 21 | //If Stack Empty Return 0 and print "Stack Underflow" 22 | //Write code to pop the topmost element of stack. 23 | //Also return the popped element 24 | } 25 | ​ 26 | function peek() { 27 | //Write code to just return the topmost element without removing it. 28 | } 29 | } 30 | //Driver code 31 | const sll = new StackAsLinkedList(); 32 | sll.push(10); 33 | sll.push(20); 34 | sll.push(30); 35 | console.log(sll.pop() + " popped from stack"); 36 | console.log("Top element is " + sll.peek()); 37 | -------------------------------------------------------------------------------- /Exercise_2.py: -------------------------------------------------------------------------------- 1 | 2 | class Node: 3 | def __init__(self, data): 4 | self.data = data 5 | self.next = None 6 | 7 | class Stack: 8 | def __init__(self): 9 | 10 | def push(self, data): 11 | 12 | def pop(self): 13 | 14 | a_stack = Stack() 15 | while True: 16 | #Give input as string if getting an EOF error. Give input like "push 10" or "pop" 17 | print('push ') 18 | print('pop') 19 | print('quit') 20 | do = input('What would you like to do? ').split() 21 | #Give input as string if getting an EOF error. Give input like "push 10" or "pop" 22 | operation = do[0].strip().lower() 23 | if operation == 'push': 24 | a_stack.push(int(do[1])) 25 | elif operation == 'pop': 26 | popped = a_stack.pop() 27 | if popped is None: 28 | print('Stack is empty.') 29 | else: 30 | print('Popped value: ', int(popped)) 31 | elif operation == 'quit': 32 | break 33 | -------------------------------------------------------------------------------- /Exercise_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // A linked list node (changes) 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | }; 11 | 12 | /* Given a reference (pointer to pointer) 13 | to the head of a list and an int, inserts 14 | a new node on the front of the list. */ 15 | void push(Node** head_ref, int new_data) 16 | { 17 | /* 1. allocate node */ 18 | 19 | /* 2. put in the data */ 20 | 21 | /* 3. Make next of new node as head */ 22 | 23 | /* 4. move the head to point to the new node */ 24 | } 25 | 26 | /* Given a node prev_node, insert a new node after the given 27 | prev_node */ 28 | void insertAfter(Node* prev_node, int new_data) 29 | { 30 | /*1. check if the given prev_node is NULL */ 31 | 32 | /* 2. allocate new node */ 33 | 34 | /* 3. put in the data */ 35 | 36 | /* 4. Make next of new node as next of prev_node */ 37 | 38 | /* 5. move the next of prev_node as new_node */ 39 | } 40 | 41 | /* Given a reference (pointer to pointer) to the head 42 | of a list and an int, appends a new node at the end */ 43 | void append(Node** head_ref, int new_data) 44 | { 45 | /* 1. allocate node */ 46 | 47 | /* 2. put in the data */ 48 | 49 | /* 3. This new node is going to be 50 | the last node, so make next of 51 | it as NULL*/ 52 | 53 | /* 4. If the Linked List is empty, 54 | then make the new node as head */ 55 | 56 | /* 5. Else traverse till the last node */ 57 | 58 | /* 6. Change the next of last node */ 59 | } 60 | 61 | // This function prints contents of 62 | // linked list starting from head 63 | void printList(Node *node) 64 | { 65 | //Your code here 66 | } 67 | 68 | /* Driver code*/ 69 | int main() 70 | { 71 | Node* head = NULL; 72 | append(&head, 6); 73 | push(&head, 7); 74 | push(&head, 1); 75 | append(&head, 4); 76 | insertAfter(head->next, 8); 77 | cout<<"Created Linked list is: "; 78 | printList(head); 79 | return 0; 80 | } -------------------------------------------------------------------------------- /Exercise_3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | // Java program to implement 4 | // a Singly Linked List 5 | public class LinkedList { 6 | 7 | Node head; // head of list 8 | 9 | // Linked list Node. 10 | // This inner class is made static 11 | // so that main() can access it 12 | static class Node { 13 | 14 | int data; 15 | Node next; 16 | 17 | // Constructor 18 | Node(int d) 19 | { 20 | //Write your code here 21 | } 22 | } 23 | 24 | // Method to insert a new node 25 | public static LinkedList insert(LinkedList list, int data) 26 | { 27 | // Create a new node with given data 28 | 29 | // If the Linked List is empty, 30 | // then make the new node as head 31 | 32 | // Else traverse till the last node 33 | // and insert the new_node there 34 | 35 | // Insert the new_node at last node 36 | // Return the list by head 37 | 38 | } 39 | 40 | // Method to print the LinkedList. 41 | public static void printList(LinkedList list) 42 | { 43 | // Traverse through the LinkedList 44 | 45 | // Print the data at current node 46 | 47 | // Go to next node 48 | } 49 | 50 | // Driver code 51 | public static void main(String[] args) 52 | { 53 | /* Start with the empty list. */ 54 | LinkedList list = new LinkedList(); 55 | 56 | // 57 | // ******INSERTION****** 58 | // 59 | 60 | // Insert the values 61 | list = insert(list, 1); 62 | list = insert(list, 2); 63 | list = insert(list, 3); 64 | list = insert(list, 4); 65 | list = insert(list, 5); 66 | 67 | // Print the LinkedList 68 | printList(list); 69 | } 70 | } -------------------------------------------------------------------------------- /Exercise_3.js: -------------------------------------------------------------------------------- 1 | // Java program to implement 2 | // a Singly Linked List 3 | class LinkedList { 4 | constructor() { 5 | this.head = null; 6 | } 7 | // Linked list Node. 8 | static Node = class { 9 | constructor(d) { 10 | this.data = d; 11 | this.next = null; 12 | } 13 | } 14 | ​ 15 | // Method to insert a new node 16 | function insert(list, data) { 17 | // Create a new node with given data 18 | ​ 19 | // If the Linked List is empty, 20 | // then make the new node as head 21 | ​ 22 | // Else traverse till the last node 23 | // and insert the new_node there 24 | ​ 25 | // Insert the new_node at last node 26 | // Return the list by head 27 | } 28 | ​ 29 | // Method to print the LinkedList. 30 | function printList(list) { 31 | // Traverse through the LinkedList 32 | ​ 33 | // Print the data at current node 34 | ​ 35 | // Go to next node 36 | } 37 | } 38 | // Driver code 39 | /* Start with the empty list. */ 40 | let list = new LinkedList(); 41 | ​ 42 | // ******INSERTION****** 43 | // Insert the values 44 | list.insert(list, 1); 45 | list.insert(list, 2); 46 | list.insert(list, 3); 47 | list.insert(list, 4); 48 | // Print the LinkedList 49 | list.printList(list); 50 | -------------------------------------------------------------------------------- /Exercise_3.py: -------------------------------------------------------------------------------- 1 | class ListNode: 2 | """ 3 | A node in a singly-linked list. 4 | """ 5 | def __init__(self, data=None, next=None): 6 | 7 | class SinglyLinkedList: 8 | def __init__(self): 9 | """ 10 | Create a new singly-linked list. 11 | Takes O(1) time. 12 | """ 13 | self.head = None 14 | 15 | def append(self, data): 16 | """ 17 | Insert a new element at the end of the list. 18 | Takes O(n) time. 19 | """ 20 | 21 | def find(self, key): 22 | """ 23 | Search for the first element with `data` matching 24 | `key`. Return the element or `None` if not found. 25 | Takes O(n) time. 26 | """ 27 | 28 | def remove(self, key): 29 | """ 30 | Remove the first occurrence of `key` in the list. 31 | Takes O(n) time. 32 | """ 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PreCourse_1 2 | 3 | # All Instructions are already provided in the respective files. 4 | 5 | Exercise_1 : Implement Stack using Array. 6 | 7 | Exercise_2 : Implement Stack using Linked List. 8 | 9 | Exercise_3 : Implement Singly Linked List. 10 | 11 | *After completing the project kindly submit a pull request* 12 | -------------------------------------------------------------------------------- /Sample.java: -------------------------------------------------------------------------------- 1 | // Time Complexity : 2 | // Space Complexity : 3 | // Did this code successfully run on Leetcode : 4 | // Any problem you faced while coding this : 5 | 6 | 7 | // Your code here along with comments explaining your approach 8 | --------------------------------------------------------------------------------