Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions Exercise_1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#include <bits/stdc++.h>

// 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 <vector>
#include <algorithm>
#include <iostream>

using namespace std;

#define MAX 1000
Expand All @@ -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();
Expand All @@ -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
Expand Down
35 changes: 27 additions & 8 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
#include <bits/stdc++.h>
// 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 <vector>
#include <algorithm>
#include <iostream>

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;
Expand All @@ -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()
Expand Down
69 changes: 50 additions & 19 deletions Exercise_3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#include <bits/stdc++.h>
// 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 <algorithm>
#include <vector>
#include <iostream>

using namespace std;

// A linked list node (changes)
Expand All @@ -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*/
Expand Down
Binary file added singly_ll
Binary file not shown.
Binary file added stack_arr
Binary file not shown.
Binary file added stack_ll
Binary file not shown.