Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the problem is that while loop does not terminate, I don't know why

#include <iostream>
using namespace std;


class linkedList
{
private:

    struct node
    {
        int data;
        node *next;
    };
    node *headA;
    node *headB;
public:
    linkedList();
    ~linkedList();
    int CompareLists();
    void pushA(int m_data);
    void pushB(int m_data);
};

linkedList::linkedList()
{
    headA=NULL;
    headB=NULL;
}
linkedList::~linkedList()
{
    delete(headA);
    delete(headB);
}

void linkedList::pushA(int m_data)
{
    node *newNode=new node;
    if(headA==NULL)
    {
        headA=newNode;
    }
    newNode->data=m_data;
    newNode->next=headA;
    headA=newNode;
}

void linkedList::pushB(int m_data)
{
    node *newNode=new node;
    if(headB==NULL)
    {
        headB=newNode;
    }
    newNode->data=m_data;
    newNode->next=headB;
    headB=newNode;
}

int linkedList::CompareLists()
{
    if(headA==NULL || headB==NULL)
    {
        return 0;
    }


    while(headA!=NULL && headB!=NULL)
    {
        if(headA->data!=headB->data)
        {
            return 0;
        }
        headA=headA->next;
        headB=headB->next;


    }
    if(headA==NULL && headB!=NULL)
    {
        return 0;
    }
    if(headA!=NULL && headB==NULL)
    {
        return 0;
    }
    return 1;

    // This is a "method-only" submission.
    // You only need to complete this method
}



int main()
{

    linkedList l;
    l.pushA(7);
    l.pushA(9);
    l.pushA(11);
    l.pushA(5);
    l.pushA(3);
    l.pushA(1);

    l.pushB(7);
    l.pushB(9);
    l.pushB(11);
    l.pushB(5);
    l.pushB(3);
    l.pushB(1);


    cout<<l.CompareLists();
}


What I have tried:

I tried solving the problem with debugger but it did not help
Posted
Updated 9-Dec-17 11:29am
Comments
PIEBALDconsult 9-Dec-17 15:27pm    
I'm not sure, but I think you have circular lists.
Member 13277493 9-Dec-17 15:33pm    
I don't think so, I have not connected the last node to the first node why it must be circular
PIEBALDconsult 9-Dec-17 15:36pm    
I could be wrong, but re-read your push methods and see if you see what I see.
Member 13277493 9-Dec-17 15:42pm    
oh thanks, I have forgotten to write else before the statements
PIEBALDconsult 9-Dec-17 15:45pm    
Glad to be of service.

There are several errors in your code. The push functions are wrong and creates (as Piebald noted) circular links. The destructor is wrong as well.
Try:
#include <iostream>
using namespace std;

class LinkedList
{
private:
  struct Node
  {
    int data;
    Node * next;
  };

  Node * headA;
  Node * headB;

  void _push( Node * & head, int data); // workhorse for pushA and pushB
  void _cleanup( Node * head); // workhorse for headA and headB cleanup, used by destructor

public:
  LinkedList():headA(nullptr), headB(nullptr){}
  ~LinkedList(){ _cleanup(headA); _cleanup(headB);}
  bool compare();
  void pushA(int data){ _push(headA, data); }
  void pushB(int data){ _push(headB, data); }
};


void LinkedList::_push( Node * & head, int data)
{
  Node * newNode = new Node();
  newNode->data = data;
  newNode->next = head;
  head = newNode;
}

void LinkedList::_cleanup(Node * head)
{
  while (head)
  {
    Node * tmp = head;
    head = head->next;
    delete tmp;
  }
}

bool LinkedList::compare()
{
  Node * na = headA;
  Node * nb = headB;

  if ( !na  || !nb ) return false;

  while ( na && nb )
  {
    if ( na->data != nb->data) return false;
    na = na->next;
    nb = nb->next;
  }
  return ( (!na) && (!nb));
}

int main()
{
  LinkedList l;
  l.pushA(7);
  l.pushA(9);
  l.pushA(11);
  l.pushA(5);
  l.pushA(3);
  l.pushA(1);

  l.pushB(7);
  l.pushB(9);
  l.pushB(11);
  l.pushB(5);
  l.pushB(3);
  l.pushB(1);

  cout << l.compare() << endl;;
}
 
Share this answer
 
Comments
Member 13277493 9-Dec-17 16:40pm    
thanks, it really helped
CPallini 9-Dec-17 16:48pm    
You are welcome.
Quote:
the problem is that while loop does not terminate, I don't know why

The reason is that the ending condition is never met.
C++
while(headA!=NULL && headB!=NULL)

Think about the logic. When the variables are suposed to be equal to NULL ?
Use the debugger to see if last element of linked list have a pointer to next that is NULL or not.
Further debugging:
try to replace:
C++
cout<<l.CompareLists();

by:
C++
cout<<l.CompareLists();
cout<<l.CompareLists();

you may get some surprise.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900