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;
}
Explanation / Answer
/* * C++ Program to Implement Singly Linked List */ #include #include #include using namespace std; /* * Node Declaration */ struct node { int info; struct node *next; }*start; /* * Class Declaration */ class single_llist { public: node* create_node(int); void insert_begin(); void insert_pos(); void insert_last(); void delete_pos(); void sort(); void search(); void update(); void reverse(); void display(); single_llist() { start = NULL; } }; /* * Main :contains menu */ main() { int choice, nodes, element, position, i; single_llist sl; start = NULL; while (1) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.