diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..cea4d3f4e --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..1d43f8347 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "e:/Learning/S30/PreCourse-1", + "program": "e:/Learning/S30/PreCourse-1/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..bb879da5a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/Exercise_1.cpp b/Exercise_1.cpp deleted file mode 100644 index 381e274d5..000000000 --- a/Exercise_1.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include - -using namespace std; - -#define MAX 1000 - -class Stack { - //Please read sample.java file before starting. - //Kindly include Time and Space complexity at top of each file - int top; - -public: - int a[MAX]; // Maximum size of Stack - - Stack() { //Constructor here } - bool push(int x); - int pop(); - int peek(); - bool isEmpty(); -}; - -bool Stack::push(int x) -{ - //Your code here - //Check Stack overflow as well -} - -int Stack::pop() -{ - //Your code here - //Check Stack Underflow as well -} -int Stack::peek() -{ - //Your code here - //Check empty condition too -} - -bool Stack::isEmpty() -{ - //Your code here -} - -// Driver program to test above functions -int main() -{ - class Stack s; - s.push(10); - s.push(20); - s.push(30); - cout << s.pop() << " Popped from stack\n"; - - return 0; -} diff --git a/Exercise_1.java b/Exercise_1.java deleted file mode 100644 index 314a3cb45..000000000 --- a/Exercise_1.java +++ /dev/null @@ -1,46 +0,0 @@ -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[]) - { - Stack s = new Stack(); - s.push(10); - s.push(20); - s.push(30); - System.out.println(s.pop() + " Popped from stack"); - } -} diff --git a/Exercise_1.js b/Exercise_1.js deleted file mode 100644 index 207189ea0..000000000 --- a/Exercise_1.js +++ /dev/null @@ -1,35 +0,0 @@ -class Stack { - //Please read sample.java file before starting. - //Kindly include Time and Space complexity at top of each file -​ - constructor() { - //Initialize your constructor - this.MAX = 1000; - this.top = -1; - this.a = new Array(this.MAX); - } -​ - function isEmpty() { - //Write your code here - } -​ - function push(x) { - //Check for stack Overflow - //Write your code here - } -​ - function pop() { - //If empty return 0 and print " Stack Underflow" - //Write your code here - } -​ - function peek() { - //Write your code here - } -} -​ -let s = new Stack(); -s.push(10); -s.push(20); -s.push(30); -console.log(s.pop() + " Popped from stack"); diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..135e434a4 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,21 +1,43 @@ +#Time complexity for this program is O(1) and for show() it will O(n) +#Space complexity for this program is O(n) and n is the number of elements in the stack + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.stack = [] + self.top = -1 def isEmpty(self): + return self.top == -1 def push(self, item): - + self.top +=1 + self.stack.append(it) def pop(self): + if self.isEmpty(): + print("Stack is empty") + return None + item = self.stack[self.top] + self.top -= 1 + return item + def peek(self): + if self.isEmpty(): + print("Stack is empty") + return None + return self.stack[self.top] def size(self): - + return self.top + 1 def show(self): - + if self.isEmpty(): + print("Stack is empty: Nothing to show.") + return [] + return self.stack[:self.top + 1] + s = myStack() s.push('1') diff --git a/Exercise_2.cpp b/Exercise_2.cpp deleted file mode 100644 index 1eb3de9b9..000000000 --- a/Exercise_2.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include -using namespace std; - -// A structure to represent a stack -class StackNode { -public: - int data; - StackNode* next; -}; - -StackNode* newNode(int data) -{ - StackNode* stackNode = new StackNode(); - stackNode->data = data; - stackNode->next = NULL; - return stackNode; -} - -int isEmpty(StackNode* root) -{ - //Your code here -} - -void push(StackNode** root, int data) -{ - //Your code here -} - -int pop(StackNode** root) -{ - //Your code here -} - -int peek(StackNode* root) -{ - //Your code here -} - -int main() -{ - StackNode* root = NULL; - - push(&root, 10); - push(&root, 20); - push(&root, 30); - - cout << pop(&root) << " popped from stack\n"; - - cout << "Top element is " << peek(root) << endl; - - return 0; -} \ No newline at end of file diff --git a/Exercise_2.java b/Exercise_2.java deleted file mode 100644 index 5a9c4868c..000000000 --- a/Exercise_2.java +++ /dev/null @@ -1,52 +0,0 @@ -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) - { - //Write code to push data to the stack. - } - - 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 - } - - public int peek() - { - //Write code to just return the topmost element without removing it. - } - - //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()); - } -} diff --git a/Exercise_2.js b/Exercise_2.js deleted file mode 100644 index 2e3216f94..000000000 --- a/Exercise_2.js +++ /dev/null @@ -1,36 +0,0 @@ -class StackAsLinkedList { -​ - static stackNode = class { -​ - constructor(d) { - //Constructor here - this.data = d; - this.next = null; - } - } -​ - function isEmpty() { - //Write your code here for the condition if stack is empty. - } -​ - function push(data) { - //Write code to push data to the stack. - } -​ - function pop() { - //If Stack Empty Return 0 and print "Stack Underflow" - //Write code to pop the topmost element of stack. - //Also return the popped element - } -​ - function peek() { - //Write code to just return the topmost element without removing it. - } -} -//Driver code -const sll = new StackAsLinkedList(); -sll.push(10); -sll.push(20); -sll.push(30); -console.log(sll.pop() + " popped from stack"); -console.log("Top element is " + sll.peek()); diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..a6ffb253d 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,5 @@ - +#Time complexity for this program is O(1) +#Space complexity for this program is O(n), where n is number of elements in stack class Node: def __init__(self, data): self.data = data @@ -6,10 +7,19 @@ def __init__(self, data): class Stack: def __init__(self): + self.top = None def push(self, data): + newNode=Node(data) + newNode.next = self.top + self.top = newNode def pop(self): + if self.top is None: + return None + popData = self.top.data + self.top = self.top.next + return popData a_stack = Stack() while True: diff --git a/Exercise_3.cpp b/Exercise_3.cpp deleted file mode 100644 index f34d89ac1..000000000 --- a/Exercise_3.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include -using namespace std; - -// A linked list node (changes) -class Node -{ - public: - int data; - Node *next; -}; - -/* Given a reference (pointer to pointer) -to the head of a list and an int, inserts -a new node on the front of the list. */ -void push(Node** head_ref, int new_data) -{ - /* 1. allocate node */ - - /* 2. put in the data */ - - /* 3. Make next of new node as head */ - - /* 4. move the head to point to the new node */ -} - -/* Given a node prev_node, insert a new node after the given -prev_node */ -void insertAfter(Node* prev_node, int new_data) -{ - /*1. check if the given prev_node is NULL */ - - /* 2. allocate new node */ - - /* 3. put in the data */ - - /* 4. Make next of new node as next of prev_node */ - - /* 5. move the next of prev_node as new_node */ -} - -/* Given a reference (pointer to pointer) to the head -of a list and an int, appends a new node at the end */ -void append(Node** head_ref, int new_data) -{ - /* 1. allocate node */ - - /* 2. put in the data */ - - /* 3. This new node is going to be - the last node, so make next of - it as NULL*/ - - /* 4. If the Linked List is empty, - then make the new node as head */ - - /* 5. Else traverse till the last node */ - - /* 6. Change the next of last node */ -} - -// This function prints contents of -// linked list starting from head -void printList(Node *node) -{ - //Your code here -} - -/* Driver code*/ -int main() -{ - Node* head = NULL; - append(&head, 6); - push(&head, 7); - push(&head, 1); - append(&head, 4); - insertAfter(head->next, 8); - cout<<"Created Linked list is: "; - printList(head); - return 0; -} \ No newline at end of file diff --git a/Exercise_3.java b/Exercise_3.java deleted file mode 100644 index fb66d329d..000000000 --- a/Exercise_3.java +++ /dev/null @@ -1,70 +0,0 @@ -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) - { - /* 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/Exercise_3.js b/Exercise_3.js deleted file mode 100644 index d1511f80e..000000000 --- a/Exercise_3.js +++ /dev/null @@ -1,49 +0,0 @@ -// Java program to implement -// a Singly Linked List -class LinkedList { - constructor() { - this.head = null; - } - // Linked list Node. - static Node = class { - constructor(d) { - this.data = d; - this.next = null; - } - } -​ - // Method to insert a new node - function insert(list, 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. - function printList(list) { - // Traverse through the LinkedList -​ - // Print the data at current node -​ - // Go to next node - } -} - // Driver code - /* Start with the empty list. */ - let list = new LinkedList(); -​ - // ******INSERTION****** - // Insert the values - list.insert(list, 1); - list.insert(list, 2); - list.insert(list, 3); - list.insert(list, 4); - // Print the LinkedList - list.printList(list); diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..589bfc715 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,12 @@ +#Time Complexity is O(1) and Space Complexity is O(N). + class ListNode: """ A node in a singly-linked list. """ def __init__(self, data=None, next=None): + self.data = data + self.next = next class SinglyLinkedList: def __init__(self): @@ -17,6 +21,14 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + newNode = ListNode(data) + if not self.head: + self.head = newNode + return + current = self.head + while current.next: + current = current.next + current.next = newNode def find(self, key): """ @@ -24,9 +36,33 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + current = self.head + while current: + if current.data == key: + return current + current = current.next + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + current = self.head + previous = None + + if current and current.data == key: + self.head = current.next + current = None + return + while current: + if current.data == key: + break + previous = current + current = current.next + + if not current: + print("Key not found.") + return + previous.next = current.next + current = None diff --git a/README.md b/README.md deleted file mode 100644 index 1e1abed69..000000000 --- a/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# PreCourse_1 - -# All Instructions are already provided in the respective files. - -Exercise_1 : Implement Stack using Array. - -Exercise_2 : Implement Stack using Linked List. - -Exercise_3 : Implement Singly Linked List. - -*After completing the project kindly submit a pull request* diff --git a/Sample.java b/Sample.java deleted file mode 100644 index 1739a9cbc..000000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach