diff --git a/Exercise_1.cpp b/Exercise_1.cpp index 381e274d5..181a8eecb 100644 --- a/Exercise_1.cpp +++ b/Exercise_1.cpp @@ -1,54 +1,101 @@ -#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; -} +#include + +using namespace std; + +#define MAX 1000 + +/* +Time Complexity : + push(): O(1) + pop(): O(1) + peek(): O(1) + isEmpty(): O(1) + +Space Complexity : + stack: O(n), n is number of elements in stack + +Did this code successfully run on Leetcode : Couldn't find the leetcode problem + +Any problem you faced while coding this : None +*/ +class Stack +{ + int top; + +public: + int a[MAX]; // Maximum size of Stack + + Stack() + { + top = -1; + } + bool push(int x); + int pop(); + int peek(); + bool isEmpty(); +}; + +// Push data onto the stack +bool Stack::push(int x) +{ + if (top >= MAX) + { + return false; + } + + a[++top] = x; + + return true; +} + +// Pop data from the stack +int Stack::pop() +{ + if (isEmpty()) + { + return -1; + } + else + { + return a[top--]; + } +} + +// Peek the top element of the stack +int Stack::peek() +{ + if (isEmpty()) + { + return -1; + } + + return a[top]; +} + +// Check if the stack is empty +bool Stack::isEmpty() +{ + if (top < 0) + { + return true; + } + + return false; +} + +// Driver program to test above functions +int main() +{ + class Stack s; + s.push(10); + s.push(20); + s.push(30); + cout << s.peek() << " Peeked from stack\n"; + cout << s.pop() << " Popped from stack\n"; + cout << s.peek() << " Peeked from stack\n"; + cout << s.pop() << " Popped from stack\n"; + cout << s.peek() << " Peeked from stack\n"; + cout << s.pop() << " Popped from stack\n"; + + return 0; +} diff --git a/Exercise_2.cpp b/Exercise_2.cpp index 1eb3de9b9..704c2806a 100644 --- a/Exercise_2.cpp +++ b/Exercise_2.cpp @@ -1,52 +1,100 @@ -#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 +#include +using namespace std; + +/* +Time Complexity : + push(): O(1) + pop(): O(1) + peek(): O(1) + isEmpty(): O(1) + +Space Complexity : + stack: O(n), n is number of elements in the stack + +Did this code successfully run on Leetcode : Couldn't find the leetcode problem + +Any problem you faced while coding this : None +*/ + +// 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; +} + +// Check if the stack is empty +int isEmpty(StackNode *root) +{ + return root == NULL; +} + +// Push data onto the stack +void push(StackNode **root, int data) +{ + StackNode *node = newNode(data); + + node->next = *root; + + *root = node; +} + +// Pop data from the stack +int pop(StackNode **root) +{ + if (isEmpty(*root)) + { + return -1; + } + + StackNode *node = *root; + + *root = (*root)->next; + + int popped = node->data; + + delete node; // Free memory to prevent memory leaks + + return popped; +} + +// Peek the top element of the stack +int peek(StackNode *root) +{ + if (isEmpty(root)) + { + return -1; + } + + return root->data; +} + +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; + + cout << pop(&root) << " popped from stack\n"; + + cout << "Top element is " << peek(root) << endl; + + cout << pop(&root) << " popped from stack\n"; + + return 0; +} \ No newline at end of file diff --git a/Exercise_3.cpp b/Exercise_3.cpp index f34d89ac1..7121d3b42 100644 --- a/Exercise_3.cpp +++ b/Exercise_3.cpp @@ -1,80 +1,116 @@ -#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 +#include +using namespace std; + +/* +Time Complexity : + push(): O(1) + insertAfter(): O(1) + append(): O(n), n is the number of elements in the list + printList(): O(n), n is the number of elements in the list + +Space Complexity : + linked list: O(n), n is number of elements in list + +Did this code successfully run on Leetcode : Couldn't find the leetcode problem + +Any problem you faced while coding this : None +*/ + +// 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 */ - +void push(Node** head_ref, int new_data) +{ + /* 1. allocate node */ + Node* node = new Node(); + + /* 2. put in the data */ + node->data = new_data; + /* 3. Make next of new node as head */ + node->next = *head_ref; /* 4. move the head to point to the new node */ -} - -/* Given a node prev_node, insert a new node after the given + *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 */ - +void insertAfter(Node* prev_node, int new_data) +{ + /*1. check if the given prev_node is NULL */ + if (prev_node == NULL) + { + return; + } + /* 2. allocate new node */ + Node *node = new Node(); + /* 3. put in the data */ + node->data = new_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 + node->next = prev_node->next; + /* 5. move the next of prev_node as new_node */ + 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, +void append(Node** head_ref, int new_data) +{ + /* 1. allocate node */ + Node *node = new Node(); + /* 2. put in the data */ + node->data = new_data; + /* 3. This new node is going to be + the last node, so make next of + it as NULL*/ + node->next = NULL; + /* 4. If the Linked List is empty, then make the new node as head */ - + if (*head_ref == NULL) + { + *head_ref = node; + return; + } /* 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 -} - + Node *iter = *head_ref; + + while (iter->next != NULL) + { + iter = iter->next; + } + + /* 6. Change the next of last node */ + iter->next = node; +} + +// This function prints contents of +// linked list starting from head +void printList(Node *node) +{ + while (node != NULL) + { + cout << node->data << endl; + node = node->next; + } +} + /* 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 +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