Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7196121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:43:04+00:00 2026-05-28T20:43:04+00:00

ok this is me training on singly-linked lists, being a newbie… However, somewhere I

  • 0

ok this is me training on singly-linked lists, being a newbie… However, somewhere I must be messing things up. My code is pretty straight forward containing all the typical procedures you would expect..

Problems:

My boolean function is always true even when I type in numbers that are not in the list

Here’s my code, look at the main function as well to get an idea of the order in which things happen. Ooh and thank you for your help!! 🙂

#include <string>
#include <iostream>

using namespace std;

class Node
{
    public:
         int n;
         Node* link;
};

void display(Node* head)
{

    cout<<head->n<<" ";

    while(head->link!=NULL)
    {
        head=head->link;
        cout<<head->n<<" ";
    }
    cout<<endl;

}

void addnode(Node*& head, int x) 
{
    if(head==NULL)
    {
        head=new Node;
        head->n=x;
        head->link=NULL; // Necessary? Why?
    }

    else
    {
        Node* p=new Node;
        p->n=x;
        p->link=head;
        head=p;
    }
}

bool found(Node* head, int x)
{                            
    if(head->n==x) return true;

    while(head->link!=NULL)
    {
        head=head->link;
        if(head->n==x) return true;
    }

    return false;
}


void addtail(Node*& head, int x) 
{                                
    if(head==NULL)          
    {                        
        head=new Node;  
        head->n=x;
        head->link=NULL;
    }

    else
    {
        Node* q=NULL; 
        q=head;       
        while(q->link!=NULL) q=q->link;

        Node* r=new Node;
        r->n=x;
        r->link=NULL;
        q->link=r;
    }
}

int removehead(Node*& head) 
{
    if(head==NULL)
    {
        cout<<"The list is empty";
        return 0; 
    }             

    int x;

    if(head->link==NULL)
    {
        x=head->n;
        head=NULL;
        return x;%0stackoverflow.com 
    Node* p=NULL;    
    p=head;
    head=head->link;
    x=p->n;
    delete p;
    return x;
}

int removetail(Node*& head) 
{
    if(head==NULL)
    {
        cout<<"The list is empty";
        return 0;
    }

    int x;

    if(head->link==NULL)
    {
        x=head->n;
        delete head;
        Node* head=NULL;
        return x;
    }

    Node* p=NULL; 
    p=head;
    while(p->link!=NULL) p=p->link;

    x=p->n;
    delete p;
    return x;
}



int main()
{

    int y; int z;

    Node* p=NULL;

    while(cin>>y)
    {
        addnode(p,y);
    }

    cin.clear(); cin.ignore(); 


    cout<<endl;

    display(p);

    cout<<endl;

    cout<<removehead(p)<<" ";

    cout<<removetail(p)<<endl;

    display(p);

    cout<<endl<<"give me a number:";

    cin>>z;

    if(found) cout<<endl<<"found"; 

    else cout<<endl<<"not found";

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:43:05+00:00Added an answer on May 28, 2026 at 8:43 pm

    .(when I remove the tail node, is the link of the previous one now just pointing at some random part of the memory? Is that the problem? And why the infinite loop?

    Looks like it:

    int removetail(Node*& head) 
    {
        // base cases elided
    
        Node* p=NULL; 
        p=head;
        while(p->link!=NULL) p=p->link;
    
        x=p->n;
        delete p;
        return x;
    }
    

    The previous link, that pointed to the p you are deleting, still points to p. Bad. It should be something like this:

    int removetail(Node*& head) 
    {
        // base cases elided
    
        Node* p=NULL; 
        p=head;
        while(p->link->link!=NULL) p=p->link;
    
        x=p->link->n;
        delete p->link;
        p->link = NULL;     // maintain linked list integrity
        return x;
    }
    

    This is safe to do (assuming memory isn’t corrupted for other reasons) because you’ve already checked if head==NULL and head->link == NULL in one of the base cases, so an initial call to p->link->link = head->link->link will not give you any improper pointer access. If head->link->link == NULL, that’s ok.


    And why the infinite loop?

    An interesting question.

    For a slightly flawed philosophical explanation: assuming you don’t get an illegal memory access error caused by accessing a bad pointer, you’re talking about a pointer value that randomly points somewhere. Real memory is finite, so any sequence of pointer references in a finite set have to repeat at some point in a cycle (otherwise the set would not be finite). Of course, that could include a NULL which would stop the infinite loop.

    More likely you’re hitting some bad memory pattern reserved by the OS memory manager, like 0xcdcdcdcd which points to 0xcdcdcdcd. In which case it is a bad choice: default memory patterns should probably be designed so that if they show up in a pointer, they are likely to cause a bad memory exception.

    You could stop the program in a debugger and tell us what the pointer value is, and that would answer that part of the question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm training code problems like UvA and I have this one in which I
I'm finally learning regexps and training with ack . I believe this uses Perl
I currently use this piece of code to reduce a given text to a
Background: this is running on a Windows 2008 Server. https://www.mercurial-scm.org/wiki/Workflows#Feature_separation_through_named_branches I'm a Mercurial newbie,
I use this line in my .htaccess file to automatically add a trailing slash
This is a bit of a long shot, but if anyone can figure it
This is starting to vex me. I recently decided to clear out my FTP,
This is kinda oddball, but I was poking around with the GNU assembler today
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.