简体   繁体   中英

How to assign every line of the file to the node of the double linked list in c++?

I have a seperate a class that has this assign method, and I need to use my double linked list implementation to this class's method

typedef string Elem;
class A{
    private:

    public:
       method assign {
                 ifstream infile("initial_text.txt",ios::in);
                string current;
                string temp=" ";
                int count=0;
                while(getline(infile, current))
                {
                    for(int i=0;i<current.size();i++) {
                        temp+=current[i];
                        if(current[i]=='.' || current[i]=='?' || current[i]=='!') {
                            cout<<temp<<"\n";
                            count++;
                            temp="";
                        }
                        
                    }
                }
                cout<<"Total Sentences: "<<count<<"\n";


}

class Node {
        public:
            Node* next;
            Node* prev;
            Elem elem;
            friend class Linkedlist;
            Node(): next(NULL), prev(NULL)
            {}
            Node(Elem elem) : elem(elem)
            {}
    };
class Linkedlist { 
    private:
        Node *head;
        Node *tail;
        int N;

    public:
        Linkedlist();//
        ~Linkedlist();//


Linkedlist::Linkedlist() {
   N = 0;                  
   head = new Node;              
   tail  = new Node;
   head->next = tail;         
   tail->prev = head;
}
Linkedlist::~Linkedlist() {
    Node *current = head;
    while (current)
    {
        Node* next = current->next;
        delete current;
        current = next;
    }
}

For now it is just filters text file by assigning each sentence to a new line, and in this loop I need to assign it to the nodes of the linked list, but I am not sure how to implement it.

Thank you!

maybe something like this:

void assign() {
  ifstream infile("initial_text.txt",ios::in);
  string current;
  string temp=" ";
  int count=0;
  LinkedList list;  // create a linked list
  while(getline(infile, current))
  {
    for(int i=0;i<current.size();i++) {
      temp+=current[i];
      if(current[i]=='.' || current[i]=='?' || current[i]=='!') {
          cout<<temp<<"\n";
          // add `temp` to the end of the list
          list.add(temp);
          count++;
          temp="";
      }
    }
  }
  cout<<"Total Sentences: "<<count<<"\n";
}
class Linkedlist { 
 private:
  Node *head;
  Node *tail;
  int N;

 public:
  Linkedlist();//
  ~Linkedlist();//

  void add(Elem elem);  // TODO: implement this!!!

  /* These may be useful as well, but what functions you want to provide depend
   * on your application
   */
  void print();       // print entire list
  void remove(...);   // remove an element
  void at(int i);     // retrieve the element at index i
  void size() { return N; }
  // alternatively, iterator allows standard container access
  class iterator : public std::iterator<bidirectional_iterator_tag, Elem> {
    Node *node_;
   public:
    explicit iterator(Node *node) : node_(node) {}
    iterator& operator++() { ... }           // prefix operator, i.e. ++it
    iterator operator++(int) { ... }         // postfix operator, i.e. it++
    bool operator==(iterator other) { ... }  // equality operator, i.e. it1 == it2
    bool operator!=(iterator other) { ... }  // not equal operator, i.e. it1 != it2
    Elem& operator*() { ... }                // dereference operator, i.e. string &tmp = *it
  }
  iterator begin() { return iterator(head->next); }
  iterator end() { return iterator(tail); }
}

Here we've encapsulated the functionality of adding to your LinkedList so that you only have to implement it once and never have to worry about memory leaks if you do it right. The iterator also provides standard c++ style bidirectional container access if you like, so that you can easily iterate through the linked list and utilize pre-made algorithms in the STL

LinkedList list;
... // populate list
// print list
for (auto elem : list)
  std::cout << elem << std::endl;;
/*** STL pre-defined functions ***/
// reverse the list in place
reverse(list);
// remove duplicates in the list
auto last = std::unique(list.begin(), list.end());
list.erase(last, list.end());
// finds location of "hello" in list, or N if it is not in the list
auto index = std::find(list.begin(), list.end(), "hello") - list.begin();
// count the number of elements beginning with a capital letter
int numCaps = std::count_if(list.begin(), list.end(),
                            [](int elem){ return isupper(elem[0]); });
// append newline to every element
std::for_earch(list.begin(), list.end(), [](string &elem) { elem += "\n"; });

Note: I didn't test this code but it should give you an idea of how to proceed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM