I know I have to store the data from the text file to a vector and then somehow store each vector element to the circular doubly linked list using the loop |
Vector? Why?
Reading from file should be relatively trivial:
1 2 3
|
while ( read entry from file ) {
add entry to container
}
|
It seems that one "entry" is one
int
. Furthermore, you probably want to preserve the order of entries.
1 2 3 4 5 6
|
void fetch( std::ifsream& in, T & container ) {
int entry {};
while ( in >> entry ) {
container.push_back( entry );
}
}
|
If T were std::vector<int>, the read-part would be done, but then you have to copy from vector to list:
1 2 3
|
std::vector<int> dummy;
fetch( infile, dummy );
for ( auto e : dummy ) list.push_back( e );
|
That, however, seems waste of memory and instructions.
Lets make T a
double_clist
:
1 2 3
|
double_clist cdl;
fetch( infile, cdl );
// done
|
Now we have added a requirement that the
double_clist
should have member
push_back(int foo)
that creates a new node with value foo and inserts it at the "logical end" of the list.
You don't quite have such member.
Note: one does never ever use
free()
on memory that came from
new
. Use
delete
.
Note: your
last
is redundant. It is always, by definition of circular, the same as
start->prev
.
Note: you use syntax
struct node *s;
. The struct is not necessary in C++. Use plain
node *s;
Note: you define three global variables: start, last, and counter. You can now have only one list. Change those globals into class members.