これは私にとって初心者で、単なるリンクリストで練習しています...しかし、どこかで私は物事を混乱させる必要があります。私のコードはかなりまっすぐ進む..あなたが期待するBool関数は常にtrue&シングルリンクリストのテールノードの削除は無限ループを作成
問題のすべての一般的な手順を含むされています。私は、リストにない数字で入力しても、
私のブール関数が常に真であると
はここに私のコードでは、主要な機能を見て、起こる順序を知ることができます。大丈夫、助けてくれてありがとう! :)
#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";
}
絶対に正しいジェイソン入力のおかげで!私はそれを見つけ出し、あなたの時間を節約するために私のポストを編集しましたが、あなたが速すぎるように見えます:) –
しかし、私のブール関数はまだ動作していません.... –
それは今うまく動いています.. :) –