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
25 changes: 25 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -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"
}
40 changes: 32 additions & 8 deletions Exercise_1.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Time Complexity = O(1) ; fixed number of steps
// Space Complexity = O(1) ; fixed size of the array

#include <bits/stdc++.h>

using namespace std;
Expand All @@ -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();
Expand All @@ -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
Expand Down
29 changes: 25 additions & 4 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
65 changes: 34 additions & 31 deletions Exercise_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down