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

Write a method called stackSum that returns an int and has no parameters which w

ID: 3918024 • Letter: W

Question

Write a method called stackSum that returns an int and has no parameters which will find the sum of a stack using a recursive technique and leave the stack unchanged. Test the method in a driver program with the following items pushed onto a stack in the following hardcoded order: -6, 4, 2, 10, 8. The output of the program should display the sum in a message to the console. Assume the method is a member of a linked stack class called LinkedStack that has a private data member: top, and methods: getInfo() and getLink(). The data member top is of type: LSNode (linked stack node), and each LSNode has data members: info and link.

This should be coded in C++ .Please show output thanks!

Explanation / Answer

SOLUTION:
This solution having explanation , full c++ code and comments with

code clear and better understanding.

EXPLANATION:

Before going to direct answer let's understand the step for solving this problem.

first of all create structure of LSNode and then make a class of data member

'top' and many member functions.

then make a main function and call functions as requirement.

for more visualization see the code below:

c++ code:

#include <iostream>
#include <stdlib.h>
using namespace std;
// structure

struct LSNode
{
int info;
struct LSNode *link;
};

// class

class LinkedStack
{
private:
   struct LSNode *top;
public:
   //void getInfo();
          void getLink();
   void push(int);
          void pop();
          void show();
          int GetSum();

};
// push function

void LinkedStack::push(int x)
{

   struct LSNode *ptr;
ptr=new LSNode;

// pushing value
ptr->info=x;


if(top!=NULL)
ptr->link=top;
else
      ptr->link=NULL;

   top=ptr;

   cout<<endl<<endl<<x<<" pushed to stack ";

}

// pop function

void LinkedStack::pop()
{
   if(top==NULL)
   { cout<<"stack is empty ";
     exit(1);
   }
     struct LSNode *temp;
      temp=top;
      top=top->link;
// deleting node
     delete temp;
}

// for showing stacknumbers

void LinkedStack::show()
{
    struct LSNode *ptr1=top;
    cout<<" The stack number: ";
    while(ptr1!=NULL)
    {
        cout<<ptr1->info<<" ->";
        ptr1=ptr1->link;
    }
}

// function to find sum
int LinkedStack::GetSum()
{
int sum=0;
struct LSNode *ptr1=top;

     while(ptr1->link)
    {
// sum at each node
      sum=sum+ptr1->info;
       ptr1=ptr1->link;
    }

    return sum;
}
// main function

int main(void)
{ int option;
int check=1,value;

// class object
LinkedStack LS1;

   while(check)
{
   cout<<"1.Push 2.Pop 3.GetSum 4.Display 5.exit ";
   cout<<"Choose option: ";
   cin>>option;

if(option==1)
{
     int value;
   cout<<"Enter value to push: ";
   cin>>value;
   LS1.push(value);
}
else if(option==2)
{
LS1.pop();
}
else if(option==3)
{ cout<<" sum: "<<LS1.GetSum()<<endl;
}
else if(option==4)
LS1.show();
else if(option==5)
exit(1);
else
      cout<<"Invalid choice ";

}  
// returning from main
     return 0;
}


since code is provided with all comments and in proper way for better and clear understanding .

However further if any difficulty in understanding the code then feel

free to ask .

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