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";
}
Looks like it:
The previous link, that pointed to the p you are deleting, still points to p. Bad. It should be something like this:
This is safe to do (assuming memory isn’t corrupted for other reasons) because you’ve already checked if
head==NULLandhead->link == NULLin 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.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.