2017-12-07 15 views
0

링크 된 목록을 만들려고하는데 괜찮습니다. 그러나 비어 있지 않은 목록에 새 노드를 삽입하고 표시 할 때 새로운 노드 만 표시하려고합니다. 삽입 된 노드가 존재합니다. 여기 코드는 다음과 같습니다 파이프를 통해 내가 입력 "3 10 20 30"이 정당하게 3 개 노드를 표시하는 경우 링크 된 목록에 새 노드를 삽입하면 새 노드가 생성됩니다.

struct patient{ 
    char name[NAME]; 
    int ttime; 
    int atime; 
    int priority; 
    struct patient *next; 
}; 

struct wr{ 
    struct patient *fnode; 
    struct patient *lnode; 
}*wr; 

void wr_insert(struct patient *node){ 
    if(wr->fnode == NULL){ 
    wr->fnode = node; 
    wr->lnode = node; 
    } 

    else{ 

    wr->lnode->next = node; 

    wr->lnode = node; 

    } 

} 
void display(){ 
    struct patient *tmp = wr->fnode; 
    while(tmp != NULL){ 
    printf("%s %d %d %d\n",tmp->name,tmp->ttime,tmp->atime,tmp->priority); 
    tmp = tmp->next; 
    } 
} 
    void new_patient(char *line){ 
     int i,check; 
     listsem = sem_open(lISTSEM, 0); 
     char *token; 
     token = strtok(line," "); 
     check = atoi(token); 
     if(check == 0){ 
     struct patient *node = malloc(sizeof(struct patient)); 
     strcpy(node->name,token); 
     node->ttime = atoi(strtok(NULL," ")); 
     node->atime = atoi(strtok(NULL," ")); 
     node->priority = atoi(strtok(NULL," ")); 
     node->next = NULL; 
     sem_wait(listsem); 
     wr_insert(node); 
     sem_post(listsem); 
     } 
     else{ 
     struct patient *group = malloc(sizeof(struct patient)); 
     sprintf(group->name,"20171201-%d",groupnum); 
     group->ttime = atoi(strtok(NULL," ")); 
     group->atime = atoi(strtok(NULL," ")); 
     group->priority = atoi(strtok(NULL," ")); 
     group->next = NULL; 
     groupnum++; 
     sem_wait(listsem); 
     wr_insert(group); 
     sem_post(listsem); 
     for(i=1;i<check;i++){ 
      struct patient *node = malloc(sizeof(struct patient)); 
      sprintf(node->name,"20171201-%d",groupnum); 
      node->ttime = group->ttime; 
      node->atime = group->atime; 
      node->priority = group->priority; 
      node->next = NULL; 
      groupnum++; 
      sem_wait(listsem); 
      wr_insert(node); 
      sem_post(listsem); 
     } 
     display(); 
     } 
     get_patient(); 
    } 

은 그래서 예를 들어, 내가

을 만들었습니다. 그러나 내가 다시 할 경우, 6 노드를 인쇄해야 할 때, 그것은 3을 다시 인쇄하지만 새 노드는 인쇄합니다.

+0

귀하의 목록의 헤드가 통화간에 누락되었습니다. 귀하의 코드가 너무 지저분 해/나는 더 자주 집중적 인 답을 줄만큼 자주 C를 사용하지 않습니다. 교정은 전화 사이에 머리를 보존하는 것입니다. –

+0

@TimBiegeleisen 당신은 머리가 꺼져있는 곳을 지적 할 수 있습니까? 또한 링크 된 목록 부분 또는 new_patient 함수 때문에 코드가 더러워 졌습니까? 귀하의 답변에 감사드립니다. – jonelearn

+0

디버거를 사용하십시오. 그것은 당신이 당신의 생각 (그리고 당신의 프로그램의 버그)에서 오류를 만든 곳을 알려줄 것입니다. –

답변

0

이 구조체 정의/예 : 루트 포인터가 미세하기 때문에

struct wr 
{ 
    struct patient *fnode; 
    struct patient *lnode; 
} *wr; 

는 정확하지 않습니다,하지만 wr 요구의 인스턴스는 인스턴스에 대한 포인터 구조체의 인스턴스가 아닌 것으로 구조체.

는 제안 :

struct wr 
{ 
    struct patient *fnode; 
    struct patient *lnode; 
} wr; 

다음 코드의 나머지 부분이 그래서 구조체로 구조체의 인스턴스와하지로 포인터를 wr을 치료하는 데 필요 :

void wr_insert(struct patient *node) 
{ 
    if(wr->fnode == NULL) 
    { 
     wr->fnode = node; 
     wr->lnode = node; 
    } 

    else 
    { 
     wr->lnode->next = node; 
     wr->lnode = node; 
    } 

가 있어야한다 :

void wr_insert(struct patient *node) 
{ 
    if(wr.fnode == NULL) 
    { 
     wr.fnode = node; 
     wr.lnode = node; 
    } 

    else 
    { 
     wr.lnode->next = node; 
     wr.lnode = node; 
    }