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

Write a program in C++ to implement a stack for the same data and show its imple

ID: 3848984 • Letter: W

Question

Write a program in C++ to implement a stack for the same data and show its implementation. Here is the structure and class definitions you will need for this problem.

struct StackItem

{

string fname;

string lname;

double salary;

StackItem *next;

};

class Stack

{

private:

StackItem *top;

bool isempty;

public:

Stack()

{

top=NULL;

isempty=true;

}

void push(StackItem *item);

void pop();

};

Write definitions for the push and pop functions. Use the following main function to test your code.

int main()

{

ifstream fin;

fin.open("HW3Data.dat");

Stack my_Stack;

string fname, lname;

double salary;

StackItem *item=new StackItem;

while(fin>>fname>>lname>>salary)

{

item->fname=fname;

item->lname=lname;

item->salary=salary;

my_Stack.push(item);

}

for (int i=0;i<9;++i)

my_Stack.pop();

return 0;

}

You must get the following output.

John Harris 50000

Lisa Smith 75000.5

Adam Johnson 68500.1

Sheila Smith 150000

Tristen Major 75800.8

Yannic Lennart 58000.6

Lorena Emil 43000

Tereza Santeri 48000

Stack is empty. Nothing to remove.

Explanation / Answer

Answer:

Stack Definition: A stack is a container of objects that are push and popped elements from stacks according to the Last in first out (LIFO) principle. There are two operations are allowed in stack:
1) Push the item into the stack.
2) Pop the item from the stack.

=> Definition for PUSH function:

void Stack::PUSH(StackItem *item)
{
StackItem *temp;
temp = new StackItem;
if(temp == NULL)
{
cout<<" No Memory to create the Node:"<<endl;
exit(1);
}
cout<<" Items push into stacks is:"<<endl;
temp->item;
temp->next=top;
top=temp;
}

=> Definition for POP function:

void Stack::pop()
{
StackItem *temp;
If( top == NULL)
{
cout<<"Stack is empty. Nothing to remove:"<<endl;
}
else
{
cout<<" Item pop from stack:"<<endl;
temp=top;
top=top->temp;
delete(temp);
}
}

=> Complete C++ program to implement a stack:

#include<iostream>
#include<stdlib.h>

using namespace std;

struct StackItem
{
string fname;
string lname;
double salary;
StackItem *next;
StackItem *temp;
StackItem *item;
};
class Stack
{
private:
StackItem *top;
bool isempty;
public:
Stack()
{
top=NULL;
isempty=true;
}
void push(StackItem *item);
void pop();
};

// PUSH Function Defenition
void Stack::push(StackItem *item)
{

struct StackItem *temp;
temp = new StackItem;
if(temp == NULL)
{
cout<<" No Memory to create the Node…"<<endl;;
exit(1);
}
cout<<" Items push into stacks is:"<<endl;
temp->item;
temp->next=top;
top=temp;
}

// POP Function Defintiton
void Stack::pop()
{
struct StackItem *temp;
if( top == NULL)
{
cout<<"Stack is empty. Nothing to remove."<<endl;
}
else
{
cout<<" Item pop from stack:"<<endl;
temp = top;
top = top -> temp;
delete(temp);
}
}

int main()
{
ifstream fin;
fin.open("HW3Data.dat");
Stack my_Stack;
string fname, lname;
double salary;
StackItem *item = new StackItem;
while(fin>>fname>>lname>>salary)
{
item->fname=fname;
item->lname=lname;
item->salary=salary;
my_Stack.push(item);
}
for (int i=0;i<9;++i)
my_Stack.pop();
return 0;
}

Note: Code not tested "HW3Data.dat" because file not present.

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