2012-11-05 3 views
1

그냥 약간의 편집을했는데, 당신이 말한 것을 시도했지만 작동하지 않았으므로 조금 익숙해졌습니다.하지만 제대로 작동하지 않는 것 같습니다. 그것은 정보를 이상하게 인쇄하고 충돌합니다. 예를 들면 : 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; 
    } 
} 

답변

0

나는 다음 노드가 null 때까지 첫 번째 노드에서 시작하여 다음 노드로 이동 반복자를 생성 제안과 같은 다음을 사용하는 것이 좋습니다 및 목록의 끝없는 (또는 다음이) 것입니다.

다음으로 간단한 방법으로 반복기를 계속해서 인쇄하고 값을 인쇄하십시오. 삽입하려면 값을 통해 헤드 항목과 반복기에서 시작하고 비교하십시오.

저는 정말 C++ 프로그래머가 아니기 때문에 의사 코드를 추가했습니다.

class iterator 
{ 
    //provide a construction method for this 
    listNode current = Head; 
    listNode getValue() 
    { 
     return current; 
    } 

    void next() 
    { 
     //probably want to include some checks for validity here 
     current = current->next; 
    } 

    boolean hasNext() 
    { 
     return current->next != null; 
    } 
} 
+0

내가 첫 번째 노드에서 시작하여 인쇄하고 다음 노드로 이동하는 방법에 대한 예를 들어 주시겠습니까? 배열을 통해 이러한 종류의 물건을 할 수 있지만 구조체/연결된 목록을 통해 않는 방법을 알고 – user1801067

+0

그냥 현재 노드에 대한 참조를 누른 상태에서 다음 항목으로 이동하면 단순히 currentNode = currentNode-> next. 또한 다음에! = null (next가 있음)인지 확인하는 체크를 제공하십시오. –

+0

은 현재 노드가 목록의 가장 최근 put이 아니겠습니까? 내가 목록을 통해 역행하는 방법은 어떨까요. – user1801067

0
int printList(struct listNode * Head){ 
struct listNode *current = Head; 
while (Head != NULL){ 
     printf("%d \n", *current); 
     current = current->next; 
} 

당신은 아주 가까이있어.

while 루프의 조건을 살펴보십시오. 프로그램이 중단되는 이유는 '헤드'가 업데이트되지 않으므로 조건이 항상 true입니다. 따라서 프로그램은 'current-> next'가 NULL이고 프로그램이 충돌 할 때까지리스트의 끝까지 도달 할 때까지 'current-> next'를 멈추지 않고 'current'를 계속 설정합니다.

'current'가 'Head'대신 NULL인지 확인하기 위해 while 루프를 변경하면 목록의 끝에 도달하면 프로그램이 중단되지 않고 중지됩니다.

편집 : 링크 된 목록을 표시하는 추가 0을 수정하는 데 몇 가지 포인터를 추가하십시오. 프로그램의 시작 부분에서

struct listNode Head = {0, NULL}; 

, 당신은 그래서 당신은 항상 상관없이 귀하의 의견이 무엇인지 적어도 하나의 공을 가지고 값을 0으로 연결된 목록에서 노드를 만드는 것입니다. Head를 NULL로 초기화하는 것이 좋습니다. 그렇게하면 insertNode 함수에서 해당 조건을 확인해야합니다.

전에 루프 상태 ('while (x> 0)')를 확인하기 때문에 약간의 추가 0이 발생합니다. 전에 결정을 내리는 데 사용하는 입력이 있습니다 ('scanf d ", & x); '). 'while'대신 'do ... while'을 사용하여 순서를 변경하는 것이 좋습니다. 예제를 가지고 'do ... while'에 대한 설명을 보려면 http://www.cprogramming.com/tutorial/c/lesson3.html을보십시오.

+0

그것은 충돌의 문제를 해결. 어떻게 문제를 해결할 수 있을까요? – user1801067

+0

내가보고있는 여분의 0을 수정하기위한 몇 가지 제안 사항을 추가했습니다. –