反转升序整数列表 [英] Reversing an ascending integer list

查看:86
本文介绍了反转升序整数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图使用堆栈来反转列表,但是在运行程序时崩溃.该程序的说明在下面的序言中. !我对程序中的错误感到厌倦!

Hi,
I tried to reverse a list using a stack but on running the program crashes. The description of the program is in the prologue below. huff! I am tired by bugs in my programs!

/*Read a sequence of integer type and revers the list.
Input terminates as soon as integer does not exceed its predecesor and the list is printed */
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    int item=1;
    stack<int> numbers; //Stack initialized to zero
    cout<<"Enter the numbers in only increasing order: "<<endl;
    while(item>numbers.top())
    {
    cin>>item;
    numbers.push(item);
    }
    cout<<endl<<endl;
    while(!numbers.empty())
    {
        cout<<numbers.top()<<" ";
        numbers.pop();
    }
}

推荐答案

来自MSDN ^ ]: top函数返回堆栈的最顶层元素.在调用top函数之前,应确保堆栈上有一个或多个元素.

尽管您的堆栈确实存在,但它不包含任何条目,因此对top()的调用将引发异常.并且由于您不在try{}/catch{}块中,因此程序崩溃.如果将while{}更改为do{}/while{},则可以确保测试之前堆栈将始终具有至少一个值.
From MSDN STD::stack::top[^]: The top function returns the topmost element of the stack. You should ensure that there are one or more elements on the stack before calling the top function.

Although your stack does exist it does not contain any entries, so a call to top() will throw an exception; and since you are not in a try{}/catch{} block your program crashes. If you change your while{} to a do{}/while{} you can ensure that the stack will always have at least one value before doing the test.


您好,

正如Richard所提到的(在之间)在调用top之前将项目添加到堆栈中-还有另一个bug(顺序已更改)

Hi,

As Richard mentioned (in between) add items to the stack before calling top - there was another bug (changed order)

#include <iostream>
#include <stack>

using namespace std;

int main()
{
    int item = 1;
    stack<int> numbers;
    numbers.push(0); // push a first item on the stack or top() will fail
    cout << "Enter the numbers in only increasing order : " << endl;

    while(item > numbers.top())
    {
        numbers.push(item); // changed order!
        cin >> item;        // -"-
    }

    cout<<endl<<endl;
    while(!numbers.empty())
    {
        cout << numbers.top() << " ";
        numbers.pop();
    }
}


为什么不喜欢这样:

Why not like this:

#include <iostream>
#include <stack>

using namespace std;

int main()
{
    int lastItem = -1; //Since you start with 1 anyway -1 will be smaller from the start in the condition of the while statment
    int item = 1;
    stack<int> numbers;
    
    cout << "Enter the numbers in only increasing order :   " << endl;

    cin >> item;        // Read first number here
    while(item > lastItem) // proceed only if input was larger than last input
    {
        numbers.push(item);       // changed order!
        lastItem = item;        
        cin >> item;
    }

    cout<<endl<<endl;
    while(!numbers.empty())
    {
        cout << numbers.top() << " ";
        numbers.pop();
    }
}



那应该做!除非您当然也要输入负数.但是,作为额外的奖励,您将不会得到未明确输入的任何其他元素.


干杯,

曼弗雷德(Manfred)



That should do it! Unless of course you''d also wanted to enter negative numbers. But as an added bonus you wont get any extra elements you had not explicitely entered.


Cheers,

Manfred


这篇关于反转升序整数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆