从堆栈中删除两个项目并添加它们的值

时间:2011-11-11 19:24:13

标签: stack linked-list

我正在编写一个使用链表创建堆栈的程序。我完成了所有功能,如push(),pop(),top()等。

我想弄清楚的是如何从堆栈中删除两个值,将它们添加到一起,然后将其推入堆栈。这是作业的一部分,我们必须继续这样做,直到所有项目被加在一起,只有总和仍然在堆栈中。

任何帮助或提示都将不胜感激!

编辑:我通过制作另一个功能解决了我的问题!       感谢所有试图提供帮助的人!

这是我的代码:

#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 个答案:

答案 0 :(得分:2)

您是强制性的界面吗?通常你会让你的pop()操作返回你刚刚弹出的值(而不是空白),而不是只打印它。如果你这样做,你的问题变得简单,你可以反复使用你的算法将它们加在一起。

事实上,pop(),stackTop(),stackSize()和isEmpty()应该都返回它们的值。如果函数中不需要打印语句,则可以让主程序打印它找到的值。