我试图通过使用2个 vector 在堆栈中存储和更新指针,在链接列表中排列元音和辅音。
但是,下面的代码片段都没有工作,我在哪里出错?
甚至main中的cout都在安排函数调用之前,并且也没有显示。
using namespace std;
bool r(char a,char b)
{
return a>b;
}
struct node{
char a;
struct node* next;
};
struct node* head;
void push(char data)
{
node* temp=new node;
temp->a=data;
if(head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void arrange()
{
vector<node*> q1;
vector<node*> q2;
node* temp=head;
while(temp!=NULL)
{
if(temp->a=='a'||temp->a=='e')
q1.push_back(temp);
else
q2.push_back(temp);
temp=temp->next;
}
head=q1[0];
cout<<head->next->a<<q1.size();
for(int i=0;i<=q1.size();i++)
{
if(i==q1.size())
q1[i]->next=q2[0];
else
q1[i]->next=q1[i+1];
}
for(int i=0;i<=q2.size();i++)
{
if(i==q2.size())
q2[i]->next=NULL;
else
q2[i]->next=q2[i+1];
}
node* t=head;
while(t!=NULL)
{
cout<<t->a;
t=t->next;
}
}
int main()
{
head=NULL;
push('a');
push('b');
push('c');
push('d');
push('e');
push('f');
node* temp=head;
while(temp!=NULL)
{
cout<<temp->a;
temp=temp->next;
}
arrange();
return 0;
}
只有当我在安排功能中删除for循环时,cout才能正常工作
最佳答案
请测试if (cout)
以查看cout
是否存在问题。如果有问题,请报告给cerr
。
关于c++ - 使用 vector 对元音和辅音排序链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63868436/