指向c中链表的第一个元素
我希望你能帮助我,这真的很重要。请 我有一个程序,我需要打印一个名称23倍,每字符使用链表。我已经让它工作打印它一次,即使我有一个for循环,我不能让它打印文本23次。请帮忙。我不知道从哪里开始 你回答这个问题真的会帮我很大的忙,我已经试了很多次了,但我还是不知道怎么做。提前谢谢指向c中链表的第一个元素,c,linked-list,C,Linked List,我希望你能帮助我,这真的很重要。请 我有一个程序,我需要打印一个名称23倍,每字符使用链表。我已经让它工作打印它一次,即使我有一个for循环,我不能让它打印文本23次。请帮忙。我不知道从哪里开始 你回答这个问题真的会帮我很大的忙,我已经试了很多次了,但我还是不知道怎么做。提前谢谢 #include <conio.h> #include <locale.h> #include <stdio.h> #include <stdlib.h> typede
#include <conio.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct L {
char c;
struct L *next;
}List;
List *l = NULL; // list head, we'll prepend nodes here
int c; // variable for current read character
List *getInput(void)
{
while ((c = getchar()) != '\n') { // read until enter
List *n = calloc(1, sizeof(List)); // create new list node
n->c = c; // store read character in that node
n->next = l; // prepend newly created node to our list
l = n; // store newly created node as head of list
}
return l;
}
int main ( void ) {
printf("Ingresa tu nombre.\n");
getInput();
printf("\n");
int i = 0;
for(i;i<23;
//I tried something like l=1; here bit it didn't work.
while (l != NULL) { // while we have not reached end of list
putchar(l->c); // print character stored in list
printf("\n");
l = l->next; // and advance to next list node
}
}
return 0;
}
#包括
#包括
#包括
#包括
类型定义结构L{
字符c;
结构L*下一步;
}名单;
列表*l=NULL;//列表头,我们将在此处前置节点
int c;//当前读取字符的变量
列表*getInput(无效)
{
而((c=getchar())!='\n'){//一直读到回车
List*n=calloc(1,sizeof(List));//创建新的列表节点
n->c=c;//在该节点中存储读取字符
n->next=l;//将新创建的节点添加到列表中
l=n;//将新创建的节点存储为列表头
}
返回l;
}
内部主(空){
printf(“Ingrea tu nombre.\n”);
getInput();
printf(“\n”);
int i=0;
对于(i;i,您每次都需要通过for
循环从列表的开头开始。这意味着您不能覆盖指向列表开头的变量l
。相反,使用不同的变量进行迭代
int main ( void ) {
printf("Ingresa tu nombre.\n");
getInput();
printf("\n");
int i = 0;
for(i;i<23;i++){
List *cur = l;
while (cur != NULL) { // while we have not reached end of list
putchar(cur->c); // print character stored in list
printf("\n");
cur = cur->next; // and advance to next list node
}
}
return 0;
}
int main(无效){
printf(“Ingrea tu nombre.\n”);
getInput();
printf(“\n”);
int i=0;
对于(i;ic);//打印存储在列表中的字符
printf(“\n”);
cur=cur->next;//并前进到下一个列表节点
}
}
返回0;
}
您每次都需要通过for
循环从列表的开头开始。这意味着您不能覆盖指向列表开头的变量l
。相反,请使用不同的变量进行迭代
int main ( void ) {
printf("Ingresa tu nombre.\n");
getInput();
printf("\n");
int i = 0;
for(i;i<23;i++){
List *cur = l;
while (cur != NULL) { // while we have not reached end of list
putchar(cur->c); // print character stored in list
printf("\n");
cur = cur->next; // and advance to next list node
}
}
return 0;
}
int main(无效){
printf(“Ingrea tu nombre.\n”);
getInput();
printf(“\n”);
int i=0;
对于(i;ic);//打印存储在列表中的字符
printf(“\n”);
cur=cur->next;//并前进到下一个列表节点
}
}
返回0;
}
删除for循环,如果列表有23个节点,那么while循环将迭代23次。我需要while中的代码重复23次,无论我有多少个节点。哦,我们可以移动printf(“\n”)
在while循环之外和之后。删除for循环,如果列表有23个节点,则while循环将迭代23次。我需要while中的代码重复23次,无论我有多少个节点。哦,我们可以移动printf(“\n”)
在while循环之外和之后。谢谢,这正是我需要的。你真的救了我。谢谢,这正是我需要的。你真的救了我。