【问题标题】:How to assign every line of the file to the node of the double linked list in c++?如何将文件的每一行分配给c++中双链表的节点?
【发布时间】:2020-11-13 10:04:54
【问题描述】:

我有一个单独的类有这个assign方法,我需要使用我的双链表实现这个类的方法

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;
    }
}

现在它只是通过将每个句子分配到一个新行来过滤文本文件,在这个循环中我需要将它分配给链表的节点,但我不确定如何实现它。

谢谢!

【问题讨论】:

  • 请尝试提出您的问题,就好像读者最初不知道您在说什么一样。花更多时间解释,强迫自己写出细节。 示例:“单独的类”与什么分开?我读了“我的双链表”并想“你有一个链表吗?我不知道。”
  • 您需要做的是将一个复杂的问题拆分为两个较小的问题。问题 1 是编写一些方法来将项目添加到您的链表中。目前链表没有方法,所以你不能用它做任何事情。当你完成问题 1(而不是之前)后,开始问题 2,即使用你在问题 1 中编写的方法将句子添加到链表中。这就是所有编程的工作方式,处理一个复杂的问题并分解成更小的部分。

标签: c++ linked-list doubly-linked-list


【解决方案1】:

可能是这样的:

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); }
}

在这里,我们封装了添加到您的 LinkedList 的功能,因此您只需实现一次,如果操作正确,就不必担心内存泄漏。 iterator 还提供标准的 c++ 风格的双向容器访问,如果您愿意,您可以轻松地遍历链表并利用 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"; });

注意:我没有测试此代码,但它应该让您了解如何继续。

【讨论】:

  • 正确的做法是在链表中加入一些方法,不要公开私有数据。如果我是你的老师,我会在上述方法中失败,因为我所做的并不是全部投反对票。
  • @john 是的,你是对的,我很懒惰。我已经更新了我的答案。
猜你喜欢
  • 2019-06-09
  • 2014-08-17
  • 1970-01-01
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多