为什么push函数中的cout显示参数而不是rear-> value和front->值?

时间:2016-04-22 16:55:57

标签: c++ memory-management g++ cout

此代码从文件中获取数据并插入到队列中。除此之外,它还执行其他一些操作但不关心错误。

需要注意的是

  

我已经很好地调试了代码

并且无法弄清楚为什么队列的内容没有被更新(或者如果更新,没有正确显示)
主要是显示队列的垃圾值

    #include<iostream>
    #include<fstream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    ifstream infile;
    ofstream outfile;
    struct node
    {
        struct node *next;
        char *value;
    }*front,*rear;
    struct node * createnode(char *val)
    {
        struct node *new1=new node;
        new1->next=NULL;
        new1->value=val;
        return new1;
    }
    void push(char *val) //This function should display "rear->value" and "front->value" but instead it is displaying "val"
    {
        if(front && rear)
        cout<<"Push called->"<<rear->value<<"->"<<front->value<<" -> "<<val<<endl;
        struct node *new1=createnode(val);
        if(front==NULL)
        {
            rear=new1;
            front=new1;
            cout<<"Created a node 1 -> "<<rear->value<<"->"<<front->value<<" -> "<<val<<"\n";
        }
        else
        {
            rear->next=new1;
            rear=rear->next;
            cout<<"created a node 2 -> "<<rear->value<<"->"<<front->value<<" -> "<<val<<"\n";
        }
    }                           
    char *pop()
    {
        cout<<"Pop called\n";
        char *ch;
        if(front==NULL)
            return NULL;
        else if(front==rear)
        {
            ch=front->value;
            front=NULL;
            rear=NULL;
            return ch;
        }
        else
        {
            ch=front->value;
            front=front->next;
            return ch;
        }
    }
    void scanitall()
    {
        cout<<"scanitall called\n";
        string line;
        ifstream infile;
        infile.open("outputproject.txt");
        while(getline(infile,line))
        {
            char line1[100];
            strncpy(line1, line.c_str(), sizeof(line1));
            line1[sizeof(line1) - 1] = 0;   
            push(line1);
        }
        infile.close();
    }
    void processit(char *abc)
    {
        // cout<<"PRocess it called\n";
        outfile.open("outputproject.txt",ios::app);
        if(!outfile)
            cout<<"Error file not opened!";
        int i,len,flag=0;
        len=strlen(abc);
        for(i=0;i<len;i++)
        {
            if(abc[i]=='*' && abc[i+1]=='*' && flag==0)
                {
                    flag=1;
                    i+=2;
                }

            if(abc[i]=='*' && abc[i+1]=='*' && flag==1)
            {
                outfile<<endl;
                flag=0;
                i+=2;
            }

            if(flag==1)
                outfile<<abc[i];
        }
        outfile.close();
    }
    void display(char *line)
    {
        // cout<<"display called\n";
        ifstream infile;
        string line2;
        char ch;
        infile.open(line);
        if(!infile)
            cout<<"File not opened!  ";
        while(getline(infile,line2))
        {
            cout<<line2;
            cout<<endl;
        }
        cout<<endl;
        cout<<endl;
        infile.close();
    }
    void openallfiles()
    {
        // cout<<"openallfiles called\n";
        string line;
        char line1[100];
        ifstream infile;
        infile.open("outputproject.txt");
        while(getline(infile,line))
        {
            strncpy(line1, line.c_str(), sizeof(line1));
            line1[sizeof(line1) - 1] = 0;
            display(line1);
        }
        infile.close();
    }
    int main()
    {
        front=NULL;
        rear=NULL;
        scanitall();
        cout<<front->value<<"  Test\n";
        cout<<"Previously scanned contents are: \n";
        struct node *temp=new node;
        temp=front;
        if(!front)
            cout<<"memory error!\n";
        while(front)
        {
            cout<<pop()<<" -> ";
        }
        cout<<endl;
        ifstream infile;
        string line;
        char line1[100];
        infile.open("inputproject.txt");
        while(getline(infile,line))
        {
            strncpy(line1, line.c_str(), sizeof(line1));
            line1[sizeof(line1) - 1] = 0;
            processit(line1);
        }
        infile.close();
        openallfiles();
        return 0;
    }

0 个答案:

没有答案