Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:05:52+00:00 2026-05-26T20:05:52+00:00

I am writing a program that creates a stack using linked lists. I have

  • 0

I am writing a program that creates a stack using linked lists. I have all the functionality finished like push(), pop(), top(), etc.

The thing I am trying to figure out is how to remove two values from the stack, add them together, then push that into the stack. This is part of the assignment and we have to continue to do it until all the items are added together and only the sum remains in the stack.

Any help or tips would be appreciated!

EDIT: I solved my problem by just making another function!
Thank you to everyone who tried to help!

Here is my code:

#include <cstdlib>
#include <iostream>

using namespace std;

//Creating a node structure
struct node
{
int data;
struct node *next;
};

//class stack
class stack
{
struct node *top;
int size;
public:
stack()
{
    top=NULL;
}
void push(); // insert an element
void pop();  // delete an element
void stackTop(); //retrive top item without removal
void stackSize(); //return the size of the stack
void isEmpty(); // return 1 if empty, 0 if not
void show(); // show the stack
};

//push items into a stack
void stack::push()
{
int value;
struct node *ptr;

cout << "\nEnter a number to insert: ";
cin >> value;
ptr = new node;
ptr->data = value;
ptr->next = NULL;
if(top != NULL)
{
    ptr->next = top;
}
top = ptr;
cout<<"\nNew item is inserted to the stack!!!" << endl;
size ++;
}

//remove the top item
void stack::pop()
{
struct node *temp;
if(top == NULL)
{
    cout<<"\nThe stack is empty!!!";
    return;
}
temp = top;
top = top->next;
cout << "\nPoped value is " << temp->data << endl;

delete temp;
size--;
}

//retrive top value without removing it
void stack::stackTop()
{
struct node *temp;
if(top == NULL)
{
    cout<<"\nThe stack is empty!!!";
    return;
}

temp = top;
cout << "The top item is: " << temp->data << endl;
delete temp;
}


//show the stack
void stack::show()
{
struct node *ptr1 = top;
cout << "\nThe stack is:" << endl;
while(ptr1 != NULL)
{
    cout << ptr1->data << " ->";
    ptr1 = ptr1->next;
}
cout << "NULL" << endl;
}

//return if empty or not
void stack::isEmpty()
{
if(top == NULL)
{
    cout<<"\nThe stack is empty!!!" << endl;
    return;
}
else
{
    cout << "\nThe stack is not empty!!!" << endl;
    return;
}
}

//return the number of items in the stack
void stack::stackSize()
{
if(top == NULL)
{
    cout<<"\nThe stack is empty!!!" << endl;
    return;
}
else
{
    cout << "\nThe stack has " << size << " items" << endl;
    return;
}
}

//main function
int main()
{
stack s;
int choice;

while(1)
{
    cout << "\nSTACK USING LINKED LIST" << endl << endl;
    cout << "1:PUSH" << endl;
    cout << "2:POP" << endl;
    cout << "3:DISPLAY STACK" << endl;
    cout << "4:RETRIVE TOP ITEM" << endl;
    cout << "5:GET THE SIZE" << endl;
    cout << "6:IS THE STACK EMPTY?" << endl;
    cout << "7:EXIT" << endl;
    cout << "Enter your choice(1-7): ";
    cin >> choice;

    switch(choice)
    {
        case 1:
            s.push();
            break;
        case 2:
            s.pop();
            break;
        case 3:
            s.show();
            break;
        case 4:
            s.stackTop();
            break;
        case 5:
            s.stackSize();
            break;
        case 6:
            s.isEmpty();
            break;
        case 7:
            exit(0);
            break;
        default:
            cout << "\nPlease enter correct choice(1-7)!!!" << endl;
            break;
    }
}
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:05:53+00:00Added an answer on May 26, 2026 at 8:05 pm

    Is the interface you have mandatory? Normally you would have your pop() operation return the value you just popped (rather than void), instead of just printing it. If you do that, your problem becomes simple, and you can just repeatedly use your algorithm to add them together.

    As a matter of fact, pop(), stackTop(), stackSize(), and isEmpty() should probably all return their values. If the print statements aren’t required within the functions, you could then just have your main program print the values it finds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that creates a Word document with sensitive information. I'd like
I am writing a program that creates reverse tunnels with ssh. I am using
I'm writing a program that creates and edits directories and files in a file
I'm writing a program that creates a 2D array from a integer n. I
Im writing a program that should read input via stdin, so I have the
I am writing a program that creates ICC color formats. These formats specify a
I'm writing a small program that creates a new Windows desktop, switches to it
I'm writing an application that creates a SQL Server database for another program. For
I have a Java program that creates a server socket and accepts connections from
I'm writing a program that calls a function in main that creates a node

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.