-2
リンクリストがpalindromeでないかどうかをチェックするために、C++でコードを書いています。コードは以下の通りです。リンクリストpalindromeで間違った答えを得る
プログラムを実行するたびに、値が1として返されます。与えられた例では、私は回文以外の文字列を使用しています。そして、私もその回文と同じ答えを得ています。
私は構文エラーがありません。このコード
#include<iostream>
#include<stack>
using namespace std;
struct node{
char data;
node *next;
};
void push(struct node** head,char data)
{
struct node *newnode= new node;
newnode->data=data;
newnode->next=(*head);
(*head)=newnode;
}
void print(struct node *ptr)
{
while(ptr!=NULL)
{
cout<<" " <<ptr->data;
ptr=ptr->next;
}
}
//this is the function to check palindrome
int checkpalindrome(node *head)
{
stack<char>s;
node *current=head;
while(current!=NULL)
{
s.push(current->data);
current=current->next;
}
while(current!=NULL && !s.empty())
{
int ele=s.top();
s.pop();
if(ele==current->data)
current=current->next;
else
{
return 0;
break;
}
}
}
int main(){
node *first=NULL;
push(&first,'a');
push(&first,'b');
push(&first,'b');
push(&first,'d');
int c=checkpalindrome(first);
if(c==0){cout<<"not a palindrome";
}
else
{cout<<"its a palindrome";
}
}
を実行するためのC++標準テンプレートライブラリを使用していました。
ランダムな観察:なぜあなたは#includeですが、それを自分で実装する代わりに使用していますか? (完全に修辞的) –
Assimilater
@Assimilater多分彼はそれを使用しようと計画していたが、彼の心を変えた、私は知りませんでした – ifma
@Assimilater私も自分の実装で試してみます –