i am trying to make a simple project .
which can read and write a data in form of linklist form file.
i searched alot but i dont make any sense, i need algorithms .
from where i can learn this thing ....???
struct Node/// will represent our nodes.
{
int data;
Node* next;
Node(int value,Node* ptr): data(value), next(ptr){}
};
class list/// will manage our list.
{
public:
list(): root(nullptr),last(nullptr){}
Node* getroot() const {return root;}
void add_nodes(ifstream& in);
///more functions
private:
Node* root;
Node* last;
};
void list::add_nodes(ifstream& in)
{
1. if the stream is aleady attached to a file check if it is in an error state else open
your file.
2. while your file is open and no error state is set read values from your file
and add them to your list.
for my case i would do something like this inside a loop
in>>current_data; ///current_data is a temporal int variable am using to read data
/// from file.
/// to add the first node i would do something like
root=last= new Node(current_data, nullptr); last=last->next;
//// adding subsequent nodes you'll just need to change the last pointer if the
/// order of your values isn't really important, if it matters you could create a
/// sorting function or might be develop a more complex function thet adds your
/// values at their right positions
last=new Node(current_data,nullptr); last=last->next;
3. continue reading till you hit the end of file marker, close you'r stream and you are
done.
}