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

Write a C++ program that will implement the stack algorithm using linked list. T

ID: 3693462 • Letter: W

Question

Write a C++ program that will implement the stack algorithm using linked list.

The program should read data from a data file.

The content of the file has the following format.
push <n> means that the following <n> items should be pushed on the stack.
pop <m> means that <m> items from the stack should be poped out and printed on the screen.
<name> means that the name should be pushed on the stack.

Explanation / Answer

#include using namespace std; // Node class class Node { int data; Node* next; public: Node() {}; void SetData(int aData) { data = aData; }; void SetNext(Node* aNext) { next = aNext; }; int Data() { return data; }; Node* Next() { return next; }; }; // List class class List { Node *head; public: List() { head = NULL; }; void Print(); void Append(int data); void Delete(int data); }; /** * Print the contents of the list */ void List::Print() { // Temp pointer Node *tmp = head; // No nodes if ( tmp == NULL ) { cout SetNext(tmp->Next()); // Delete the current node delete tmp; } } int main() { // New list List list; // Append nodes to the list list.Append(100); list.Print(); list.Append(200); list.Print(); list.Append(300); list.Print(); list.Append(400); list.Print(); list.Append(500); list.Print(); // Delete nodes from the list list.Delete(400); list.Print(); list.Delete(300); list.Print(); list.Delete(200); list.Print(); list.Delete(500); list.Print(); list.Delete(100); list.Print(); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote