动态双端队列中的C ++元素

时间:2015-12-02 08:11:10

标签: push fstream ifstream deque pop

我在从文本文件中向deque添加元素时遇到问题。文件包含如下数字:1 2 3 4.但是当我尝试从文件中提取数字时,deque中只填充了一个数字。

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <stdio.h>

using namespace std; 

int temp;
int br = 1;

struct delem
{

    int key;
    delem *next;

} *l = NULL, *r = NULL;

void push_l(int n) 
{

    delem *p;      
    p = l;           
    l = new delem;     
    l->key = n; 
    l->next = p;             
    if (r == NULL) 
    {

        r = l;
    }

}

void push_r(int n)             
{

    delem *p;           
    p = r;             
    r = new delem;    
    r->key = n;               
    r->next = NULL;   
    if (l == NULL)             
    {

        l = r; 

    }
    else                 
        p->next = r; 

}

int pop_l(int &n)             
{
    delem *p;                
    if (l)                     
    {
        n = l->key;        
        p = l;                  
        l = l->next;    
        if (l== NULL)             
            r = NULL;         
        delete p;             
        return 1;
    }
    else
        return 0;
}

int pop_r(int &n)
{
    delem *p;                 
    if (r)                     
    {

        n = r->key;                
        if (l == r)           
        {
            delete r;          
            l = r = NULL;  
        }
        else
        {
            p = l;                     
            while (p->next != r)
                p = p->next;                   
                //p++;
                n = r->key;                        
                p->next = NULL;         
                delete r;                  
                r = p;                    
                return 1;
        }

       }
       else
       return 0;

}

void get(int n)
{

    int i, t;
    for (i = 1; i < br; i++) 
    {

        pop_l(t); 
        if (i == n)
        {

            temp = t;

        }

        push_r(t); 

    }

}

void print_deque()
{

    for (int i = 1; i<br; i++) 
    {

        get(i);
        cout << temp << " ";

    }

}

void find_number()
{

    int n;
    int total = 0;
    int count = 0;
    double average = 0;
    ifstream deque_file;
    deque_file.open("deque.txt", ios::in);

    if (deque_file)
    {

        while (!deque_file.eof())
        {

            deque_file >> n;
            push_l(n);

        }

        while(pop_l(n))
        {

            total+=n;
            count++;

        }

        average = total/count;

        cout<<"\n\nSum: " << total << " Count: " << count << " Average: " << average << endl << endl;

    }
    else
        cout << "Error while opening input file!" << endl;

    deque_file.close();

}

int main()
{

    int ch;
    ifstream fd;
    fd.open("deque.txt", ios::in);
    while (fd)
    {

        fd >> ch;
        push_r(ch);
        br++;

    }

    print_deque();
    find_number();

    system("pause");
    return 0;

}

如何避免两次添加最后一个号码?

1 个答案:

答案 0 :(得分:0)

不要while (fd)(或while (!fd.eof()))。

取而代之的是while (fd >> ch)

原因是,在您第一次尝试从文件末尾读取之前,eofbit标志未设置,导致循环迭代一次到多次。