简体   繁体   中英

Singly linked list C++ ostream and istream — outputs memory addresses

Im really new to this and am now learning singly linked lists. I am writing some code but I am really confused. I am trying to write a read method and a write method. I have a test harness I cant change though. I just want to be able to read to the stream and output the stream so it doesnt come back with the memory addresses.

can anyone explain in a really simple way please and help me fix this code?

void SLLIntStorage::Read(istream& r)
{
    char c[13];
    r >> c;
    r >> NumberOfInts;

    Node *node = new Node;
    head = node; //start of linked list

    for(int i = 0; i < NumberOfInts; i++) //this reads from the file and works
    {
        r >> node->data;
        cout << node->data << endl;
        node ->next = new Node; //creates a new node
        node = node->next;
    }
}

void SLLIntStorage::Write(ostream& w)
{
    Node *node = new Node;
    head = node;

    for(int i = 0; i < NumberOfInts; i++)
    {
        w << node->data << endl;
        //cout << i << endl;
    }
}

and in the header file

#pragma once

#include <iostream>

using namespace std;

struct Node
{
    int data; //data in current node
    Node *next; //link of address to next node
};

class SLLIntStorage
{

private:
    Node *head;// start of linked list
    //Node *tail;
    Node current; //current node
public:
    void setReadSort(bool);
    void sortOwn();

    void Read(istream&);
    void Write(ostream&);

    void add(int i);
    void del();

    bool _setRead;
    int NumberOfInts;

    SLLIntStorage(void);
    ~SLLIntStorage(void);
};

inline ostream& operator<< (ostream& out, SLLIntStorage& n) 
{
    n.Write(out); 
    return out;
}
inline istream& operator>> (istream& in, SLLIntStorage& s) 
{
    s.Read(in); 
    return in;
}

thank you!

Your write method seems a bit messed up. You want to write the elements, not create new ones. Something like this should work better:

void SLLIntStorage::Write(ostream& w)
{
    Node *node = head;

    for(int i = 0; i < NumberOfInts; i++)
    {
        w << node->data << endl;
        node = node->next;
        //cout << i << endl;
    }
}

By the way: The way your implementation seems to work for me, you have a potentially big memory leak. Once the Read method gets called twice in row, the old list is discarded without freeing the memory. You should think about what your class shall do if write is called while there is another file saved. Append it? Delete the old list first?

In your Write() method you clobber the entire list by doing

Node *node = new Node;
head = node;

This replaces a the entire list by an empty list if you ask me. NumberOfInts is no longer correct and you proceed to print the same node->data NumberOfInts times.

I don't know where to start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM