【问题标题】:Infinite loop, removing duplicates from a stack?无限循环,从堆栈中删除重复项?
【发布时间】:2015-05-24 10:52:11
【问题描述】:

打印 removeDuplicates 函数返回的新堆栈时出现无限循环,但我不知道是什么原因造成的?所有其他功能都运行良好。如果元素重复,函数应保留第一次出现。这是代码:

class Stack { 
      Node *head; 

      public: Stack() { 
          head = NULL; 
      }; 

      ~Stack() {
          while (head) {
              Node * temp = head;
              head = head->next;
              delete temp;
          }
      }

      void push(int data); 
      int pop(); 
      bool isEmpty();
      void print();
      Stack removeDuplicates();
 }; 

 void Stack::push(int data) {
    Node *temp = new Node(data);
    temp->next = head;
    head = temp;
 }

 int Stack::pop() { 
    if (head != NULL ) {
        int x = head->data;
        Node *temp = head;
        head = head->next;
        delete temp;
        return x;
    } else {
        cout << "The stack is empty!";
        return -1;
    }
 }

 bool Stack::isEmpty(){
    return head == NULL;  
 } 

 void Stack::print() {
     Node * temp = head;
     while(temp != NULL ) {
         cout << temp->data << " ";
         temp = temp->next;
     }
 }

 Stack Stack::removeDuplicates(){
    Stack st;
    Node *temp = NULL;
    bool flag;

    while (head != NULL) {
        if (st.head == NULL) {
            st.push(head->data);
            temp = st.head;
        } else {
            flag = true;
            while (temp != NULL) {
                if (head->data == temp->data)
                    flag = false;
                temp = temp->next;
            }
            if (flag == true)
               st.push(head->data);
         }
         Node *del = head;
         head = head->next;
         delete del;
     }
     return st;
 }

【问题讨论】:

  • 请告诉我您尝试过使用调试器进行单步调试。
  • @AtlasC1 : st.head vs head ?
  • @AtlasC1 'st.head' 是新堆栈的头部;而 'head' 是原始堆栈的头.. 那会导致错误吗?
  • @norisknofun 'st.head' 是新堆栈的头部;而“头”是原始堆栈的头..?
  • 为什么不直接发布与堆栈相关的所有代码并干净地格式化它,这样人们就可以真正看到发生了什么而不是猜测变量?当所有上下文都可用时,很难调试。

标签: c++ stack infinite-loop


【解决方案1】:

head 在函数外声明为什么?

函数外的代码是否修改了head?

如果 head 为 null 进入函数,那么你的函数将不会做任何事情,并且取决于你也没有提供的打印代码,它可能只是因为这个原因而永远循环。

在你的第一个 if 语句中你有 if (st.head == NULL) ... 然后你将 temp 设置为 st.head,例如temp = NULL,那么 temp 什么时候会在 if 语句的 else 分支中使用非 null?

为什么不在函数内显式初始化所有变量,而不仅仅是声明它们并将它们与类型相关联,因为如果它们是堆栈变量,它们是未定义的。如果您格式化代码,它还有助于理解和维护代码,以便更容易进行可视化解析。您会惊讶于使用该规则可以捕获多少错误。

  Stack Stack::removeDuplicates() {
    Stack st;
    Node *temp = NULL;
    bool flag;

    while (head != NULL) {
        if (st.head == NULL) {
            st.push(head->data);
            temp = st.head;
        } else {
            flag = true;
            while (temp != NULL) {
                if (head->data == temp->data)
                    flag = false;
                temp = temp->next;
            }
            if (flag)
               st.push(head->data);
        }
        Node *del = head;
        head = head->next;
        delete del;
     }
     return st;
  }

【讨论】:

  • 你问问题,这些都属于 cmets。
  • @usr1234567 对于我给你的所有工作和帮助,你不觉得你有点严厉和小气吗?我提供的帮助比你多。你应该失去积分。长大了。
猜你喜欢
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 2021-08-07
  • 2016-01-20
  • 1970-01-01
相关资源
最近更新 更多