Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please do a line by line comment of what each line of code does #include using n

ID: 646173 • Letter: P

Question

please do a line by line comment of what each line of code does

#include

using namespace std;

#include "stack.h"

bool Stack::Empty() {

return head == NULL;

}

void Stack::Push(int data) {

Node* newNode = new Node();

newNode->SetData((int)data);

newNode->SetNext(head);

head = newNode;

}

int Stack::Pop() {

if (head == NULL)

return 0;

int data = head->Data();

Node* tmp = head;

head = head->Next();

delete tmp;

return data;

}

int main() {

Stack stack;

int n;

cout << "Enter the number n: ";

cin >> n;

int org = n;

int num = 2;

while (num <= org && n > 1)

{

if (n % num == 0)

{

stack.Push(num);

n = n / num;

}

else

{

num++;

}

}

cout << org << " = " << stack.Pop();

while (!stack.Empty())

cout << " * " << stack.Pop();

cout << endl;

return 0;

}


Explanation / Answer

#include

using namespace std;

#include "stack.h"

bool Stack::Empty() {

return head == NULL;

}

// empty() method sets head null

void Stack::Push(int data) {

Node* newNode = new Node();

newNode->SetData((int)data);

newNode->SetNext(head);

head = newNode;

}

// Push method is used to add node

int Stack::Pop() {

if (head == NULL)

return 0;

int data = head->Data();

Node* tmp = head;

head = head->Next();

delete tmp;

return data;

}

// Pop method is used to delete node

int main() {

Stack stack;

// here stack is declared

int n;

cout << "Enter the number n: ";

cin >> n;

int org = n;

int num = 2;

//while loop is used to work with stack

while (num <= org && n > 1)

{

if (n % num == 0)

{

stack.Push(num);

n = n / num;

}

else

{

num++;

}

//this if is used to add node at begining or at end

}

cout << org << " = " << stack.Pop();

while (!stack.Empty())

cout << " * " << stack.Pop();

cout << endl;

return 0;

}