C++ help inplement the Singly linked list interface to store the data from Words
ID: 3856107 • Letter: C
Question
C++ help
inplement the Singly linked list interface to store the data from Words.txt
Here's my code I used for word.txt
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
template <typename Data>
class Demo {
public:
int capacity;
Data *_mdata;
int index;
Demo() {
capacity = 0;
index = 0;
_mdata = new Data[capacity];
}
void increase_capacity() {
Data* ptemp = new Data[capacity * 2];
for (int i = 0; i<index; i++)
ptemp[i] = _mdata[i];
delete[] _mdata;
_mdata = ptemp;
capacity *= 2;
}
void read(char* file_name) {
ifstream fin;
fin.open(file_name);
Data tmp;
while (fin >> tmp) {
if (index == capacity)
increase_capacity();
_mdata[index++] = tmp;
}
fin.close();
}
};
int main() {
Demo<string> demo;
cout << "Capacity was: " << demo.capacity << endl;
demo.read("Words.txt");
cout << "Capacity is: " << demo.capacity << endl;
}