From 5219518e73bcd925cc6a11d08ca21196a19445ef Mon Sep 17 00:00:00 2001 From: rbhargav0104 Date: Mon, 22 Dec 2025 16:05:23 +0530 Subject: [PATCH] Donen Pre Course 1 --- .vscode/c_cpp_properties.json | 25 ++++++++++++++ .vscode/tasks.json | 29 ++++++++++++++++ Exercise_1.cpp | 40 ++++++++++++++++----- Exercise_2.cpp | 29 +++++++++++++--- Exercise_3.cpp | 65 ++++++++++++++++++----------------- 5 files changed, 145 insertions(+), 43 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/tasks.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..f8fe69652 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,25 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "cStandard": "c17", + "cppStandard": "c++14", + "intelliSenseMode": "macos-clang-arm64" + }, + { + "name": "Mac", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "cStandard": "c17", + "cppStandard": "c++14", + "intelliSenseMode": "macos-clang-arm64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 000000000..531866790 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: clang++ build active file", + "command": "/usr/bin/clang++", + "args": [ + "-fcolor-diagnostics", + "-fansi-escape-codes", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/Exercise_1.cpp b/Exercise_1.cpp index 381e274d5..ea9e8696c 100644 --- a/Exercise_1.cpp +++ b/Exercise_1.cpp @@ -1,3 +1,6 @@ +// Time Complexity = O(1) ; fixed number of steps +// Space Complexity = O(1) ; fixed size of the array + #include using namespace std; @@ -12,7 +15,7 @@ class Stack { public: int a[MAX]; // Maximum size of Stack - Stack() { //Constructor here } + Stack() { top = -1; } bool push(int x); int pop(); int peek(); @@ -21,24 +24,45 @@ class Stack { bool Stack::push(int x) { - //Your code here - //Check Stack overflow as well + if (top == MAX - 1){ + return false; + } + else{ + top = top + 1; + a[top] = x; + return true; + } } int Stack::pop() { - //Your code here - //Check Stack Underflow as well + if (top == -1){ + return -1; + } + else{ + int temp = a[top]; + top--; + return temp; + } } int Stack::peek() { - //Your code here - //Check empty condition too + if (top == -1){ + return -1; + } + else { + return a[top]; + } } bool Stack::isEmpty() { - //Your code here + if (top == -1){ + return true; + } + else{ + return false; + } } // Driver program to test above functions diff --git a/Exercise_2.cpp b/Exercise_2.cpp index 1eb3de9b9..2c68a123b 100644 --- a/Exercise_2.cpp +++ b/Exercise_2.cpp @@ -18,22 +18,43 @@ StackNode* newNode(int data) int isEmpty(StackNode* root) { - //Your code here + if(root == NULL){ + return 1; + } + else{ + return 0; + } } void push(StackNode** root, int data) { - //Your code here + StackNode* temp = newNode(data); + temp->next = *root; + *root = temp; } int pop(StackNode** root) { - //Your code here + if (*root == NULL){ + return -1; + } + else{ + StackNode* temp = *root; + int popped = temp->data; + *root = temp->next; + delete temp; + return popped; + } } int peek(StackNode* root) { - //Your code here + if(root == NULL){ + return -1; + } + else{ + return root->data; + } } int main() diff --git a/Exercise_3.cpp b/Exercise_3.cpp index f34d89ac1..17ee01916 100644 --- a/Exercise_3.cpp +++ b/Exercise_3.cpp @@ -14,55 +14,58 @@ 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 */ + Node* node = new Node(); + node->data = new_data; + node->next = *head_ref; + *head_ref = 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 */ + if(prev_node == NULL){ + return; + } + else{ + Node* node = new Node(); + node->data = new_data; + node->next = prev_node->next; + prev_node->next = 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 */ + Node* new_node = new Node(); + + new_node->data = new_data; + new_node->next = NULL; + + if(*head_ref == NULL){ + *head_ref = new_node; + return; + } + + Node* last = *head_ref; + + while(last->next != NULL){ + last = last->next; + } + + last->next = new_node; } // This function prints contents of // linked list starting from head void printList(Node *node) { - //Your code here + while(node != NULL){ + cout << node->data << " "; + node = node->next; + } } /* Driver code*/