C++ ';a';使用列表向后打印字母表时丢失

C++ ';a';使用列表向后打印字母表时丢失,c++,list,C++,List,我试图用链表倒排字母表,但我无法显示“a”。由于某种原因,它跳过了它,我无法理解它。这是我的密码: int _tmain(int argc, _TCHAR* argv[]) { char s[]="abcdefghijklmnopqrstuvwxyz"; node *head; node *temp; node *current; head = new node; // crea

我试图用链表倒排字母表,但我无法显示“a”。由于某种原因,它跳过了它,我无法理解它。这是我的密码:

    int _tmain(int argc, _TCHAR* argv[])
    {
        char s[]="abcdefghijklmnopqrstuvwxyz";

        node *head;
        node *temp;
        node *current;

        head = new node;          // create the head of the linked list
        head->data = s[25];
        head->next = NULL;
        temp = head;   // get ready for the loop - save the head in temp - you are         going to change temp in the loop

        for(size_t i = 25; i >= 1; i--)      // create the rest of the linked list
        {
            current = new node;    // make a new node
            current->data = s[i];  // set it's data member
            current->next = NULL;
            temp->next = current;  // point to the new node
            temp = current;        // make temp point to current node (for next         time through)
        }

        node *ptr = head;    // set a ptr to head, then you are going to "increment"         the pointer

        while (ptr != NULL)
        {
            cout << ptr->data; // print out the linked list
            ptr = ptr->next;   // increment the linked list
        }

        cout << endl;
        system("pause");
        return 0;
    }
int-tmain(int-argc,_-TCHAR*argv[]
{
字符s[]=“abcdefghijklmnopqrstuvwxyz”;
节点*头;
节点*温度;
节点*电流;
head=new节点;//创建链表的头部
头部->数据=s[25];
head->next=NULL;
temp=head;//为循环做好准备-将头部保存在temp中-您将更改循环中的温度
for(size\u t i=25;i>=1;i--)//创建链接列表的其余部分
{
当前=新节点;//创建新节点
当前->数据=s[i];//设置其数据成员
当前->下一步=空;
temp->next=current;//指向新节点
temp=current;//使temp指向当前节点(下一次通过)
}
node*ptr=head;//将ptr设置为head,然后将“递增”指针
while(ptr!=NULL)
{
cout data;//打印出链表
ptr=ptr->next;//增加链表
}
库特

“a”是字符串中的索引0,您永远无法访问索引0。

问题是您在for循环中忽略了大小写
i=0

for
循环更改为:

    size_t i = 25; // 'z' was already added
    do
    {
        --i;
        current = new node;    // make a new node
        current->data = s[i];  // set it's data member
        current->next = NULL;
        temp->next = current;  // point to the new node
        temp = current;        // make temp point to current node (for next         time through)
    } while ( i != 0 );

你不能简单地为(size_t i=25;i>=0;i--)做
的原因是
i
是无符号的,因此总是
i>=0
的情况,因此循环永远不会终止,或者更可能的情况是你会遇到分段错误。

发生这种情况是因为你犯了一个“off by one”在某个地方有bug。你可以花所有的时间自己找到它。使用调试器单步遍历代码,或者添加打印语句来显示每一步的变量状态。祝你好运。请注意,使用链表向后打印字母表非常容易。从左到右逐步遍历字符串,然后r每个字母,将一个新节点推到包含该字母的堆栈上。然后从堆栈中逐个弹出所有节点并打印每个节点的字母。
for(size_t i=25;i>=1/*应为0而不是1!*/;i--)
数组索引从和中的
0
开始!!你能解释一下为什么这个for循环是必要的,为什么在你的答案中OP的for循环不能正常工作吗?我把for循环修改为for(int I=strlen(s)-2;I>=0;I--)它解决了这个问题。我需要将=添加到>旁边。当我使用0并使用>而不是>=时,出现了错误。谢谢!
    size_t i = 25; // 'z' was already added
    do
    {
        --i;
        current = new node;    // make a new node
        current->data = s[i];  // set it's data member
        current->next = NULL;
        temp->next = current;  // point to the new node
        temp = current;        // make temp point to current node (for next         time through)
    } while ( i != 0 );