2013-09-28 1 views
0

그래서 아이디어는 내가 구조체링크리스트의 인쇄 및 추가 요소

struct Node 
{ 
    struct Node *next; 
    struct Node *prev; 
    char value[5]; 
}; 

struct DoubleLinkedList 
{ 
    int size; 
    struct Node *head; 
    struct Node *tail; 
}; 

와 나는 삽입 정렬 기능을 사용하여 목록에 삽입 해요로 정의 된 이중 연결리스트를 가지고있다. 필자는 포인터를 내 Doubly Linked 목록에 매개 변수로 전달하고 새로운 4 자 문자열 노드를 목록에 추가하여 수정합니다 (사전 식 정렬 된 Linked 목록). 그런 다음 각 문자열 노드를 추가하여 링크 된 목록을 인쇄합니다.

인쇄가 문제가되는 것으로 증명되었습니다. 지금, 아래의 코드로, 출력은 항상 같은 (문자열이 AAAA, BBBB, CCCC는 모든 단계에서 삽입되는 가정 ...)

AAAA입니다

BBBB ->

BBBB cccc -> cccc -> cccc

어떤 이유로 연결 목록 구조가 삽입되는 새 문자열 값으로 모든 노드를 변경하고 있습니다. 나는 이유를 모른다! 또한 인쇄 블록을 주 기능으로 이동 시키려고하면 횡설수설이 인쇄됩니다.

int main() 
{ 
    struct DoubleLinkedList strings; 
    while (1) 
{ 
    sleep(1); 
    char s[5]; 
    GenerateRandomString(s,4); 
    InsertionSort(&strings, s); 
} 
    return 0; 
} 

void InsertionSort(struct DoubleLinkedList *sorted, char *randomstring) 
{ 
struct Node new; 
strcpy(new.value,randomstring); 
printf("Newvalue %s\n", new.value); 
if ((*sorted).size == 0) 
{ 
    new.next = NULL; 
    new.prev = NULL; 
    (*sorted).head = &(new); 
    (*sorted).tail = &(new); 
} 
else 
{ 
    printf("TEST %s\n", (*(*sorted).head).value); 
    struct Node *current; 
    current = (*sorted).head; 
    printf("CURRENT %s\n", (*current).value); 
    while (strcmp(randomstring,(*current).value) > 0) 
    { 
     current = (*current).next; 
     if (current = NULL) 
     { 
      break; 
     } 
    } 
    new.next = current; 
    if (current != NULL) 
    { 
     new.prev = (*current).prev; 
     if ((*current).prev != NULL) 
     { 
      (*(*current).prev).next = &(new); 
     } 
     else 
     { 
      (*sorted).head = &(new); 
     } 
     (*current).prev = &(new); 
    } 
    else 
    { 
     new.prev = (*sorted).tail; 
     (*((*sorted).tail)).next = &(new); 
     (*sorted).tail = &(new); 
    } 
} 
(*sorted).size++; 
struct Node *printing; 
printing = (*sorted).head; 
int i; 
for (i = 0; i < (*sorted).size - 1; i++) 
{ 
    printf("%s -> ", (*printing).value); 
    printing = (*printing).next; 
} 
printf("%s\n",(*printing).value); 
} 
+0

왜'(* sorted) .size' 대신'sorted-> size'를 쓰지 않으시겠습니까? 내말은, 그것은 더 일반적입니다. 변수 이름을'new'로 지정하지 마십시오. – pzaenger

+0

'(* 정렬 됨).head = &(new);' 로컬 자동 변수의 주소는 범위 밖에서 사용할 수 없습니다. – BLUEPIXY

답변

0

하기는 (randomstring, new.value) 의 strcpy의 값에 대한 메모리를 할당하지 않은; 후속 printf가 작동됩니다.

당신은 예를

new.value = strdup(randomstring); 

(무료 (new.value와 메모리를 해제하는 것을 잊지 마세요) 이렇게하면에서는 StrDup malloc을 호출하기 때문에 당신이 당신의 노드를 삭제할 때)을 위해 할 수 있습니다.

+0

정말 죄송합니다. 질문을 게시하기 전에 실제로 작은 프로그램을 편집했습니다. 나는 구조체의 일부로 메모리를 할당하려고합니다. krandiash

0

어쨌든 새로운 메모리를 할당하지 않으므로 InsertionSort를 종료 할 때 노드가 매달려 있습니다.

new = (struct Node *)malloc(sizeof(struct Node)); 

삽입 정렬

에 있어야 다음 포인터를 사용하는 모든 조정 (즉, 새로운 -> 대신 new.stuff의 물건을 대신 & 새의 새). 초기화되지 않은

strings.size = 0; 

주요 strings.size에서 또한

는없는 것 같다.

마지막 하나는, 당신이

if (current = NULL) 

을 쓸 때 나는 당신이 (일부 C의 전통에서, 당신은 (경우에 쓸 것! 현재)) 다음으로

if (current == NULL) 

을 의미 생각 수정, 그것은 작동하는 것 같습니다.