简体   繁体   English

C ++中的循环链表(开头插入)

[英]Circular linked list in C++ (insert at beginning)

I am studying old exams for an exam. 我正在为考试学习旧考试。 One task is to implement an insert and print function that inserts elements at the beginning of a circular list. 一个任务是实现在循环列表的开头插入元素的插入和打印功能。 A program is provided to test the students solution. 提供了一个程序来测试学生解决方案。

My solution for the insert is: 我对插入的解决方案是:

void Circular_List::insert(std::string const& str)
{
    if (entry == nullptr) {
        entry = new Element(str);
        entry -> next = entry;
    }
    else {
        Element* temp = entry;
        entry = new Element(str);
        entry -> next = temp;
    }
}

My thought process: 我的思考过程: 在此处输入图片说明

It seems to work because my print: 这似乎有效,因为我的打印:

void Circular_List::print() const
{
    Element* temp = entry;
    while (temp -> next != temp) {
        cout << temp -> name << endl;
        temp = temp -> next; 
    }
}

prints the list in the right order EXCEPT for the element that I first added. 以正确的顺序打印列表,但不包括我第一次添加的元素。 I don't understand why it doesn't print the first element. 我不明白为什么它不打印第一个元素。 The program provided prints out 20 iterations. 该程序提供了打印出20次迭代的信息。 For example if I insert a,b,c,d,e the program will print 例如,如果我插入a,b,c,d,e程序将打印

d->c->b->a->a->a->a->a->a->a->a->a->a->a->a->a->a->a->a->a-> d-> c-> b-> a-> a-> a-> a-> a-> a-> a-> a-> a-> a-> a-> a-> a-> a-> a- > a-> a-> a->

This seems very off, should it not loop to the front of the list instead of just printing a? 这似乎很不正常,是否应该循环到列表的最前面而不是仅仅打印一个?

The test program: 测试程序:

  int j = 0;
  for (Circular_List::Iterator i = l.begin(); i != l.end() && j < 20; ++i, ++j)
  {
    cout << *i << "->";
  }
  cout << endl;

class Iterator
  {
  public:
    Iterator(Element* e) : pos(e) {}
    ~Iterator() = default;
    Iterator(Iterator const&) = default;
    Iterator& operator=(Iterator const&) = default;
    bool operator!=(Iterator const& i) { return pos != i.pos; }
    operator bool() { return pos != nullptr; }
    Iterator& operator++() { pos = pos->next; return *this;}
    std::string operator*() { return pos->name; }
  private:
    Element* pos;
  };

I assume my insert is wrong but I can't figure out what I'm doing wrong? 我以为我的插入内容有误,但是我不知道自己在做什么错?

When you are inserting the second element to the list, your first element keeps next entry pointing back to itself. 当您将第二个元素插入列表时,第一个元素将使下一个条目指向其自身。 You need to update two next fields: one in inserted record and another in the list to point back to inserted record. 您需要更新两个下一个字段:一个在插入的记录中,另一个在列表中以指向回插入的记录。 You fail to do the latter. 您无法做到后者。 The else clause should LIKELY be: else子句应该是:

    Element* temp = new Element(str);
    temp->next = entry->next;
    entry->next = temp;
    entry = temp;

This makes entry a pointer to LAST cycle element and entry->next - the first cycle element; 这使entry指向LAST循环元素,而entry-> next-第一个循环元素;

BTW, your second drawing is incorrect as 'a'->next should be pointing back to 'a' and not to entry. 顺便说一句,您的第二个图形是不正确的,因为'a'-> next应该指向'a'而不是输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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