From 26dd17e3a9cde86d9d4e28ade44743ad6b691e0e Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 23 Oct 2024 21:55:22 -0700 Subject: [PATCH] complete Precourse-1 --- Exercise_1.cpp | 35 +++++++++++++++++++++++++++++++++-- Exercise_2.cpp | 43 ++++++++++++++++++++++++++++++++++--------- Exercise_3.cpp | 47 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 100 insertions(+), 25 deletions(-) diff --git a/Exercise_1.cpp b/Exercise_1.cpp index 381e274d..8e1633a8 100644 --- a/Exercise_1.cpp +++ b/Exercise_1.cpp @@ -6,13 +6,23 @@ using namespace std; class Stack { //Please read sample.java file before starting. + // firstly we need to decide which data structure to go with + // if they have allowed us to use the stack our first choice is stackl + // if not allowed our second choice is arraylist + + // Time complexity of each operation in this + // push , pop,peek,isempty as we are using single operation to add value to stack time complexity is O(1) + // and the maximum capacity to store the is already define 1000 //Kindly include Time and Space complexity at top of each file int top; public: int a[MAX]; // Maximum size of Stack - Stack() { //Constructor here } + Stack() { //Constructor here + // we need to start with index -1 + top = -1; + } bool push(int x); int pop(); int peek(); @@ -23,22 +33,43 @@ bool Stack::push(int x) { //Your code here //Check Stack overflow as well + if(top >= MAX-1) + { + cout << "stack overflow" << endl; + return false; + } + + // now we need to store each element using the array + a[++top] = x; + + return true; + } int Stack::pop() { //Your code here //Check Stack Underflow as well + if(top <= -1) + return -1; + int x = a[top--]; + return x; } int Stack::peek() { //Your code here //Check empty condition too + if(top <= -1) + return -1; + + return a[top]; } bool Stack::isEmpty() { - //Your code here + if(top <= -1) + return true; + return false; } // Driver program to test above functions diff --git a/Exercise_2.cpp b/Exercise_2.cpp index 1eb3de9b..2bdb84cc 100644 --- a/Exercise_2.cpp +++ b/Exercise_2.cpp @@ -2,12 +2,16 @@ using namespace std; // A structure to represent a stack + +// if we want to design the stack using the linked list we need to keep track of previous node for the tracking +// again as we are doing all the operation in one go the time complexity is O(1) for all the operations + class StackNode { public: int data; StackNode* next; }; - + StackNode* newNode(int data) { StackNode* stackNode = new StackNode(); @@ -15,36 +19,57 @@ StackNode* newNode(int data) stackNode->next = NULL; return stackNode; } - +StackNode *head; int isEmpty(StackNode* root) { //Your code here + return head==NULL; } -void push(StackNode** root, int data) +void push(StackNode* root, int data) { //Your code here + // base case would first node we need to check + if(head == NULL) + { + head = newNode(data); + } + else + { + root->next = head; + head = root; + } } -int pop(StackNode** root) +int pop(StackNode* root) { //Your code here + if(head == NULL) + return -1; + + int x = head->data; + head = head->next; + + return x; } int peek(StackNode* root) { - //Your code here + if(head==NULL) + return -1; + //Your code here + return head->data; } int main() { StackNode* root = NULL; - push(&root, 10); - push(&root, 20); - push(&root, 30); + push(root, 10); + push(root, 20); + push(root, 30); - cout << pop(&root) << " popped from stack\n"; + cout << pop(root) << " popped from stack\n"; cout << "Top element is " << peek(root) << endl; diff --git a/Exercise_3.cpp b/Exercise_3.cpp index f34d89ac..27dfd258 100644 --- a/Exercise_3.cpp +++ b/Exercise_3.cpp @@ -7,20 +7,25 @@ class Node public: int data; Node *next; + + Node(int new_data){ + data = new_data; + next = NULL; + } }; - +Node *head; /* 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 */ - + Node *new_node = new Node(new_data); /* 3. Make next of new node as head */ - + new_node->next = *head_ref; /* 4. move the head to point to the new node */ + *head_ref = new_node; } /* Given a node prev_node, insert a new node after the given @@ -28,14 +33,17 @@ prev_node */ 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 */ - /* 3. put in the data */ - + Node *new_node = new Node(new_data); /* 4. Make next of new node as next of prev_node */ - - /* 5. move the next of prev_node as new_node */ + new_node->next = prev_node->next; + /* 5. move the next of prev_node as new_node */ + prev_node->next = new_node; } /* Given a reference (pointer to pointer) to the head @@ -43,19 +51,25 @@ 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*/ - + Node *new_node = new Node(new_data); /* 4. If the Linked List is empty, then make the new node as head */ - + if(*head_ref==NULL) + { + *head_ref = new_node; + return; + } + /* 5. Else traverse till the last node */ - + Node *curr = *head_ref; + while(curr->next != NULL) + curr= curr->next; /* 6. Change the next of last node */ + curr->next = new_node; } // This function prints contents of @@ -63,6 +77,11 @@ void append(Node** head_ref, int new_data) void printList(Node *node) { //Your code here + while(node != NULL) + { + printf("%d ",node->data); + node = node->next; + } } /* Driver code*/