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

Write a program to implement a stack for the same data and show its implementati

ID: 3848930 • 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 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

//StackItem.java
public class StackItem {
   public StackItem[] stackArray;
   public int maxSize;
   public int top;
  
   private String fname;
   private String lname;
   private double salary;
   public StackItem(String fname, String lname, double salary) {
       super();
       this.fname = fname;
       this.lname = lname;
       this.salary = salary;
   }
   public String getFname() {
       return fname;
   }
   public void setFname(String fname) {
       this.fname = fname;
   }
   public String getLname() {
       return lname;
   }
   public void setLname(String lname) {
       this.lname = lname;
   }
   public double getSalary() {
       return salary;
   }
   public void setSalary(double salary) {
       this.salary = salary;
   }
   public String toString(){
       return this.fname+" "+this.lname+" "+this.salary;
   }
  
   public StackItem() {
       super();
   }
   public void MyStack(int s) {
   maxSize = s;
   stackArray = new StackItem[maxSize];
   top = -1;
   }
   public void push(StackItem j) {
   stackArray[++top] = j;
   }
   public void pop() {
       StackItem temp=stackArray[top--];
       System.out.println(temp.toString() );
   }
   public StackItem peek() {
   return stackArray[top];
   }
   public boolean isEmpty() {
   return (top == -1);
   }
   public boolean isFull() {
   return (top == maxSize - 1);
   }
}

//End of StackItem Class

//StackTester.java
public class StackTester {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       StackItem myStack1= new StackItem();
       myStack1.MyStack(3);//Define the size of Stack here
      
       StackItem s1=new StackItem ("Bebo1","Sri1",123);
       StackItem s2=new StackItem ("Bebo2","Sri2",456);
       StackItem s3=new StackItem ("Bebo3","Sri3",789);
      
       //Pushing Order
      
       myStack1.push(s1);
       myStack1.push(s2);
       myStack1.push(s3);
       //Popping Operation
       while (!myStack1.isEmpty()) {
   myStack1.pop();
   }
   }

}

//End of Tester Class

Sample output:

Bebo3 Sri3 789.0
Bebo2 Sri2 456.0
Bebo1 Sri1 123.0

Please rate it if you get the answer. Thanks..)

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