Write a program to implement a stack for the same data and show its implementati
ID: 3849619 • Letter: W
Question
Write a program 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 ur 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: iExplanation / Answer
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<alloc.h>
struct StackItem
{
char fname[15];
char lname[15];
double salary;
StackItem *next;
};
class Stack
{
private:
int isempty;
StackItem *top,*temp;
public:
Stack()
{
top=NULL;
isempty=1;
}
void push(StackItem *item);
void pop();
};
void Stack :: push(StackItem *item)
{
if(top==NULL)
{
top=item;
item->next=NULL;
}
else
{
item->next=top;
top=item;
}
cout<<endl<<"element inserted";
}
void Stack::pop()
{
if(top==NULL)
cout<<"stack is empty";
else
{
temp = top;
cout<<"deleted element is "<<temp->fname<<" "<<temp->lname<<" "<<temp->salary<<endl;
top=top->next;
free(temp);
}
}
int main()
{
ifstream fin;
fin.open("HW3DATA.DAT");
Stack my_Stack;
char fname[15],lname[15];
double salary;
StackItem *item=new StackItem;
while(fin>>fname)
{
fin>>lname;
fin>>salary;
strcpy(item->fname,fname);
strcpy(item->lname,lname);
item->salary=salary;
my_Stack.push(item);
}
for(int i=0;i<9;++i)
my_Stack.pop();
return 0;
}
// text file is your file name with the following data format you can get the out put
RAJA CHUNDRU 5000 RAVU RAN 5500 KARTHIK C 5000 K K 2000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.