diff --git a/Exercise_1.cpp b/Exercise_1.cpp index 381e274d5..13e926223 100644 --- a/Exercise_1.cpp +++ b/Exercise_1.cpp @@ -1,5 +1,13 @@ -#include - +// Time Complexity : Push: O(1), Pop: O(1), Peek: O(1), isEmpty: O(1) +// Space Complexity : Push: O(1), Pop: O(1), Peek: O(1), isEmpty: O(1) +// Did this code successfully run on Leetcode : yes (problem 1381) +// Any problem you faced while coding this : yes confusion with array bound while looping + + +#include +#include +#include + using namespace std; #define MAX 1000 @@ -12,7 +20,9 @@ class Stack { public: int a[MAX]; // Maximum size of Stack - Stack() { //Constructor here } + Stack() { + top = -1; //Intialize top to -1 + } bool push(int x); int pop(); int peek(); @@ -21,24 +31,36 @@ class Stack { bool Stack::push(int x) { - //Your code here - //Check Stack overflow as well + if (top >= MAX - 1) { // Check if stack is already full + cout << "Stack Overflow" << endl; + return false; + } + a[++top] = x; // Increment top then assign + cout << "Pushed " << x << " on to top of stack\n"; + return true; } int Stack::pop() { - //Your code here - //Check Stack Underflow as well + if (top < 0) { // Check if stack is already empty + cout << "Stack is empty" << endl; + return -1; + } + return a[top--]; // return current and then decrement top for next cycle; + } int Stack::peek() { - //Your code here - //Check empty condition too + if (top < 0) { // check if stack is empty + cout << "Stack is empty" << endl; + return -1; + } + return a[top]; // Just return top without decrementing } bool Stack::isEmpty() { - //Your code here + return (top < 0); // Same approach but return boolean } // Driver program to test above functions diff --git a/Exercise_2.cpp b/Exercise_2.cpp index 1eb3de9b9..1c9cc9c82 100644 --- a/Exercise_2.cpp +++ b/Exercise_2.cpp @@ -1,14 +1,23 @@ -#include +// Time Complexity : Push: O(1), Pop: O(1), Peek: O(1), isEmpty: O(1) +// Space Complexity : Push: O(1), Pop: O(1), Peek: O(1), isEmpty: O(1) +// Did this code successfully run on Leetcode : No, Didn't find a similar problem +// Any problem you faced while coding this : confusion with pointers and dereferencing + + +#include +#include +#include + using namespace std; // A structure to represent a stack class StackNode { public: - int data; - StackNode* next; + int data; // Data part of stack node object + StackNode* next; // pointer to next node }; -StackNode* newNode(int data) +StackNode* newNode(int data) // function to implement new node, returns a pointer to the created node { StackNode* stackNode = new StackNode(); stackNode->data = data; @@ -18,22 +27,32 @@ StackNode* newNode(int data) int isEmpty(StackNode* root) { - //Your code here + if (root == NULL) return 1; // check if root itself is null and if it is return 1 (true) else 0 (false) + return 0; } void push(StackNode** root, int data) { - //Your code here + StackNode* newnodeptr = newNode(data); // create a newnode pointer + newnodeptr->next = *root; // assign the next node to newnode as current root + *root = newnodeptr; // reassign root to newnode + } int pop(StackNode** root) { - //Your code here + if (isEmpty(*root)) return -1; // check if stack is empty if so return -1 + StackNode* temp = *root; // assign root to a temporary node + int data = (*root)->data; // gather the data from node to be poped + *root = (*root)->next; // shift the root to the next node orphaning the popped node + delete temp; // prevent memory leak of orphaned node + return data; // return the data } int peek(StackNode* root) { - //Your code here + if (isEmpty(root)) return -1; // check if root is empty + return root->data; // if not return the data } int main() diff --git a/Exercise_3.cpp b/Exercise_3.cpp index f34d89ac1..51b157b88 100644 --- a/Exercise_3.cpp +++ b/Exercise_3.cpp @@ -1,4 +1,12 @@ -#include +// Time Complexity : Push: O(1), insertAfter: O(1) (given previous node), Append: O(n), printList: O(n) +// Space Complexity : Push: O(1), insertAfter: O(1), Append: O(1), printList: O(1) +// Did this code successfully run on Leetcode : No, Didn't find a similar problem +// Any problem you faced while coding this : None + +#include +#include +#include + using namespace std; // A linked list node (changes) @@ -15,54 +23,77 @@ a new node on the front of the list. */ void push(Node** head_ref, int new_data) { /* 1. allocate node */ + Node* nodeptr = new Node(); - /* 2. put in the data */ - - /* 3. Make next of new node as head */ + /* 2. put in the data */ + nodeptr->data = new_data; + + /* 3. Make next of new node as head */ + nodeptr->next = *head_ref; /* 4. move the head to point to the new node */ + *head_ref = nodeptr; } /* 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 */ - + /*1. check if the given prev_node is NULL */ + if (prev_node == NULL) return; + + /* 2. allocate new node */ + Node* newnodeptr = new Node(); + + /* 3. put in the data */ + newnodeptr->data = new_data; + /* 4. Make next of new node as next of prev_node */ - - /* 5. move the next of prev_node as new_node */ + newnodeptr->next = prev_node->next; + + /* 5. move the next of prev_node as new_node */ + prev_node->next = newnodeptr; } /* 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 */ + /* 1. allocate node */ + Node* newnodeptr = new Node(); - /* 2. put in the data */ + /* 2. put in the data */ + newnodeptr->data = new_data; /* 3. This new node is going to be the last node, so make next of - it as NULL*/ + it as NULL*/ + newnodeptr->next = NULL; /* 4. If the Linked List is empty, then make the new node as head */ - + if (*head_ref == NULL) { + *head_ref = newnodeptr; + return; + } /* 5. Else traverse till the last node */ - - /* 6. Change the next of last node */ + Node* lastptr = *head_ref; + while (lastptr->next != NULL) { + lastptr = lastptr->next; + } + /* 6. Change the next of last node */ + lastptr->next = newnodeptr; } // 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; + } + cout << endl; } /* Driver code*/ diff --git a/singly_ll b/singly_ll new file mode 100755 index 000000000..3817f73b1 Binary files /dev/null and b/singly_ll differ diff --git a/stack_arr b/stack_arr new file mode 100755 index 000000000..d4d3c5eb8 Binary files /dev/null and b/stack_arr differ diff --git a/stack_ll b/stack_ll new file mode 100755 index 000000000..e8c692988 Binary files /dev/null and b/stack_ll differ