Infinite loop while traversing through linked list : <p>Deleting and viewing entire queue causes the problem- I'm assuming it is an infinite loop case for traversing. It works perfectly fine for adding the element but execution stops right after 2 or 3 are selected by the user.</p>

<p>I'm just adding some lines of random text before the code because stack isn't allowing such a big code with a small explanation.</p>

<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>

<pre><code>#include&lt;iostream&gt;
using namespace std;
class linkedlist
{
    struct node
    {
        int number;
        node *next;
    }*HEAD;
public:

    void addelement(int num);
    void exitelement();
    void displayqueue();

};

void linkedlist::addelement(int num)
{
    node *t;
    t= new node;
    t -&gt; number=num;
    t -&gt; next=HEAD;
    HEAD=t;
    cout&lt;&lt;num&lt;&lt;"has successfully been added to the queue /n";
}

void linkedlist::exitelement()
{
    node *t;
    t=HEAD;
    while(t -&gt; next !=NULL)
    {
        t=t -&gt; next;
    }
    cout&lt;&lt;t-&gt;number;
    delete t;
}

void linkedlist::displayqueue()
{
    node *t;
    t=HEAD;
    while(t-&gt;next !=NULL)
    {
        cout&lt;&lt;t -&gt; number&lt;&lt;"\t";
        t=t-&gt;next;
    }
    cout&lt;&lt;" \n that's the end of the list \n";
}

int main()
{
    int lol;
    linkedlist m;
    int rpt=1;
    while(rpt==1)
    {
        int c;
        cout&lt;&lt;"\n please select 1 to add an element, 2 to remove an element from the queue and 3 to display the entire queue \n";
        cin&gt;&gt;c;
        cout&lt;&lt;"/n";
        if(c==1)
        {
            cout&lt;&lt;"enter the number: ";
            cin&gt;&gt;lol;
            m.addelement(lol);
        }
        else if(c==2)
        {
            m.exitelement();
        }
        else if(c==3)
        {
            m.displayqueue();
        }
        else
        {
            cout&lt;&lt;"you have entered an invalid input. Sorry \n";
        }
        cout&lt;&lt;"\n Do you wish to start the queue again??? \n";
        cin&gt;&gt;rpt;
    }

}
</code></pre>
