Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4546286
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:56:03+00:00 2026-05-21T15:56:03+00:00

Im really new to this and am now learning singly linked lists. I am

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:56:04+00:00Added an answer on May 21, 2026 at 3:56 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating this simple program which would save me alot of time, but I'm
I'm having a really hard time with this one, EDIT: I'm putting this edit
I am trying to understand the practical difference during the execution of a program
I have been a user of jQuery (and some of its minor plugins) for
I'm a new person to C++ dll import topic and may be my question
I expect there's one of two answers to this, either impossible or extremely simple
I have an object that I am trying to update using MVC(2), also using
I am trying to do the typical thing where you load data into a
There doesn't seem to be any tried and true set of best practices to
I have a project that adds elements to an AutoCad drawing. I noticed that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.