그냥 약간의 편집을했는데, 당신이 말한 것을 시도했지만 작동하지 않았으므로 조금 익숙해졌습니다.하지만 제대로 작동하지 않는 것 같습니다. 그것은 정보를 이상하게 인쇄하고 충돌합니다. 예를 들면 : 9-8-7-6-5-4-3-2-1을 입력 한 다음 인쇄하려면 0을 입력하면 0-0-0-9가 다시 인쇄됩니다. -1-2-3-4-5-6-7-8 다음 충돌이 발생합니까? 내가 입력 할 때 1-2-3-4-5-6-7-8-9 그 다음에는 0으로 출력하고, 나에게 다시 출력한다. 0-0-0-1-2-3-4-5-6-7 -8-9 다음 충돌합니다.주문 링크 된 목록 인쇄
#include <stdio.h>
#include <stdlib.h>
struct listNode{
int data; //ordered field
struct listNode *next;
};
//prototypes
void insertNode(struct listNode *Head, int x);
int printList(struct listNode *Head);
int freeList(struct listNode *Head, int x);
//main
int main(){
struct listNode Head = {0, NULL};
int x = 1;
int ret = 0;
printf("This program will create an odered linked list of numbers greater"
" than 0 until the user inputs 0 or a negative number.\n");
while (x > 0){
printf("Please input a value to store into the list.\n");
scanf("%d", &x);
insertNode(&Head, x);
}
ret = printList(&Head);
}
void insertNode(struct listNode * Head, int x){
struct listNode *newNode, *current;
newNode = malloc(sizeof(struct listNode));
newNode->data = x;
newNode->next = NULL;
current = Head;
while (current->next != NULL && current->data < x)
{
current = current->next;
}
if(current->next == NULL){
current->next = newNode;
}
else{
newNode->next = current->next;
current->next = newNode;
}
}
int printList(struct listNode * Head){
struct listNode *current = Head;
while (Head != NULL){
printf("%d \n", *current);
current = current->next;
}
}
내가 첫 번째 노드에서 시작하여 인쇄하고 다음 노드로 이동하는 방법에 대한 예를 들어 주시겠습니까? 배열을 통해 이러한 종류의 물건을 할 수 있지만 구조체/연결된 목록을 통해 않는 방법을 알고 – user1801067
그냥 현재 노드에 대한 참조를 누른 상태에서 다음 항목으로 이동하면 단순히 currentNode = currentNode-> next. 또한 다음에! = null (next가 있음)인지 확인하는 체크를 제공하십시오. –
은 현재 노드가 목록의 가장 최근 put이 아니겠습니까? 내가 목록을 통해 역행하는 방법은 어떨까요. – user1801067