-1
getline()을 사용하여 전체 줄을 가져 와서 연결된 목록에 삽입하는 방법은 무엇입니까? 내 코드는 다음과 같습니다. 나는 전체 줄로 줄을 볼 수 있을지 확신한다. 한 줄만 시도해도 문제없이 프로그램이 작동합니다. 그러나 나는 그것이 나에게 분할 오류 표시 다른 줄을 삽입하려고 할 때 : CUR 이후c의 노드에 전체 행을 삽입하는 방법은 무엇입니까?
while(cur!=NULL){
cur = cur->next;
}
cur->next = newNode;
는 while 루프 후 NULL입니다 : 11
typedef struct Node{
struct Node *next;
char *data;
}Node;
void insert(Node **head, char *input){
Node *newNode = malloc(sizeof(Node));
newNode->data = input;
newNode->next = NULL;
Node *cur = *head;
if(*head == NULL){
*head = newNode;
}
else{
while(cur!=NULL){
cur = cur->next;
}
cur->next = newNode;
}
}
void Pint(Node *head){
Node *cur = head;
while(cur!=NULL){
printf("%s\n", cur->data);
cur = cur->next;
}
printf("\n");
}
int main(){
Node *head = NULL;
char *input = NULL;
size_t len = 0;
while(getline(&input, &len, stdin)!=EOF){
insert(&head, input);
input = NULL;
}
Pint(head);
return 0;
}
이동 시도 cur = cur-> next; 아래 cur-> 다음 = newNode; – user2519193
@ user2519193 이제 무한 루프가있는 것처럼 보입니다 ... – user1234567
while 루프를 if로 변경하십시오. – user2519193