void alphaout()
{
nodealpha *curr;
nodealpha *curr2;
curr = first;
curr2 = first;
while (curr != NULL)
{
char al1 = curr2 -> alpha;
char al2 = curr -> alpha;
if (al1 < al2)
{
curr2 = curr;
}
std::cout << "ALPHABET : " << curr -> alpha << endl;
curr = curr -> next;
}
}
我制作了一个简单的程序,用户输入每个字母,然后按顺序显示它,无论用户输入字母的方式如何,但问题是,当它显示时,字母的顺序与用户的顺序相同已给出输入,我不知道如何以 a 到 z 的方式排列它
最佳答案
我可以想到两种不同的方法来解决这个问题,第一种,您可以使用节点的 ascii 代码索引将节点的地址保存在数组中:
// here is a function i made for an old project which will give you the index in an array
//of any char you pass to it (sorry for spanish comments but that's my my teacher
//asked for)
int position(char letter)
{
int valor;
//A-ascii 65 -> posicion 0 a-ascii 97 -> 0
//Z-ascii 90 -> posicion 25 z->ascii 122 - 25
if(letra>='A' && letra <='Z')
valor = letra - 65;
else if(letra >='a' && letra<='z')
valor = letra-97;
else
valor= 26; //
return valor;
}
// this is what i would put in your alphaout function to store the ponters
// here we save the pointers to each letter
nodealpha *alpha[size of your aplhabet];
while(curr != NULL){
alpha[position(curr->alpha)] = curr;
curr = curr->next;
}
// after this you can just print the array
cout << "alphabet: " << endl;
for(int i = 0; i < (size of alphabet); i++){
cout << alpha[i]->alph << ", ";
}
这样做的缺点是它不会考虑重复项,它只会成为每个字母的最后一个实例
至于另一种方法,您可以为字母表中的每个字母扫描一次列表,然后立即打印出来,但这将花费明显更多的时间,而且会导致重复。
关于c++ - 无法弄清楚如何使用 C++ 中的链表按从 a 到 z 的顺序显示字母表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61904372/