2013-10-17 2 views
0

배우기 C를 배우기 위해 스플릿 함수를 구현하고 싶다. 그렇지만 모든 노드가 인덱스 번호를 정규로 가지고 있어야한다. 목록, 배열 있습니다. 목록 끝에 새 노드를 추가하는 '밀어 넣기'기능을 수행하면 모든 항목이 정상적으로 작동하지만 목록의 맨 위에 노드를 추가하는 "Unshift"를 수행하면 각 노드를 적절한 색인으로 유지하는 데 문제가 있습니다. 0부터 시작하는 숫자입니다.이 함수의 끝에는 각 노드를 거쳐 첫 번째 0, 두 번째 1 등을주는 반복자를 만들고 싶습니다. 지금까지 모든 노드 인덱스가 다음과 같이 변경되었습니다. 0을 증가시키지 않고. 사람들이 나를 도와 줄 수 있기를 바랍니다.더블 링크 된 목록 - 노드에 적절한 인덱스 번호를 제공하는 이터레이터를 쓸 수 없다

'list.h'  
#ifndef lcthw_List_h 
#define lcthw_List_h 

#include <stdlib.h> 

struct ListNode; 

// ListNode contains value, next, and prev struct. Each ListNode is another 
// chain in structure, they are linked 
typedef struct ListNode { 
    struct ListNode *next; 
    struct ListNode *prev; 
    void *value; 
    int track_num; 
} ListNode; 

// List is an guardian angel of ListNodes, it keeps tracking them by count 
// and knows which Node is first and last 
typedef struct List { 
    int count; 
    ListNode *first; 
    ListNode *last; 
} List; 


// Some standard functions for operate lists 
List *List_create(); 
void List_destroy(List *list); 
void List_clear(List *list); 
void List_clear_destroy(List *list); 

// These Macros return count, first and last element of the list 
#define List_count(A) ((A)->count) 
#define List_first(A) ((A)->first != NULL ? (A)->first->value : NULL) 
#define List_last(A) ((A)->last != NULL ? (A)->last->value : NULL) 

// List_push adds a new element to the end of the list 
void List_push(List *list, void *value); 
//List_pop takes the last element of the list and returns it 
void *List_pop(List *list); 

// Unshift adds element to the top of the list 
void List_unshift(List *list, void *value); 
// Shift retuns and remove first element from the list 
void *List_shift(List *list); 

// Removes list 
void *List_remove(List *list, ListNode *node); 

// This Macro is very useful, It Iterates through elements in the list 
#define LIST_FOREACH(L, S, M, V) ListNode *_node = NULL;\ 
    ListNode *V = NULL;\ 
    for(V = _node = L->S; _node != NULL; V = _node = _node->M) 

#endif 


'list.c sample with subject function' 
void List_unshift(List *list, void *value) 
{ 
    int i; 
    assert(list != NULL); 
    assert(list->count >= 0); 
    if(list->count > 0) { 
     assert(list->first != NULL); 
    } 

    ListNode *node = calloc(1, sizeof(ListNode)); 
    check_mem(node); 

    node->value = value; 

    if(list->first == NULL) { 
     list->first = node; 
     list->last = node; 


    } else { 
     node->next = list->first; 
     list->first->prev = node; 
     list->first = node; 


    } 

    list->count++; 

     LIST_FOREACH(list, first, next, cur) { 
      for(i = 0;i < list->count -1;i++) { 
       cur->track_num = i; 
      } 
     } 
error: 
    return; 
} 

답변

0

는 "CUR"는 용 루프

for(i = 0;i < list->count -1;i++) { 
    cur->track_num = i; 
} 

내부 불변하고있다 "(I = 0; I < 목록 -> 셀 -1 ''의해 오프 '는이고 (i = 0, i < 0; i ++)는 아무 것도하지 않습니다.

+0

그래, 네가 맞아. 루프에서이 조건을 가지고 노는 중이고, 여전히이 함수의 최종 결과가 잘못되었습니다.이 문제를 해결하는 데 도움이 될만한 팁을 줄 수 있습니까? –