2016-11-09 3 views
0

이중 연결 목록의 첫 번째 요소와 마지막 요소를 교체하려고합니다. 지금까지 나는 목록을 만들고 그것에 숫자를 추가하는 다음 코드를 가지고있다. 그러나 출력은 두 번 모두 같은 목록입니다.C - 이중 연결 목록의 첫 번째 요소와 마지막 요소 교환

#include <stdio.h> 
#include <stdlib.h> 

struct node2 { 
    int number; 
    struct node2 *next, *prev; 
}; 

void addNodeDouble(struct node2 **head, struct node2 **tail, int num, int thesi) { 
    if (*head == NULL) { 
     struct node2 * current; 
     current = (struct node2 *)malloc(1 * sizeof(struct node2)); 
     current->number = num; 
     current->prev = NULL; 
     current->next = NULL; 
     *head = current; 
     *tail = current; 
    } else { 
     if (thesi == 1) { 
      struct node2 *current, *temp; 
      current = (struct node2 *)malloc(1 * sizeof(struct node2)); 
      current->number = num; 
      temp = *head; 
      while (temp->next != NULL) 
       temp = temp->next; 

      temp->next = current; 
      current->prev = *tail; 
      current->next = NULL; 
      (*tail)->next = current; 
      *tail = current; 
     } else { 
      struct node2 *current; 
      current = (struct node2 *)malloc(1 * sizeof(struct node2)); 
      current->number = num; 
      current->next = *head; 
      (*head)->prev = current; 
      *head = current; 
     } 
    } 
} 

void ReversedisplayList(struct node2 **head, struct node2 **tail) { 
    struct node2 *current; 
    if (*head == NULL) 
     printf("I lista einai adeia!\n"); 
    else { 
     current = *tail; 
     while (current != NULL) { 
      printf("%d ", current->number); 
      current = current->prev; 
     } 
    } 
} 

void swapElements2(struct node2 **head, struct node2 **tail) { 
    struct node2 *current, *temp; 

    temp = (*tail)->prev; 
    current = *tail; 

    temp->next = *head; 
    current->next = (*head)->next; 
    (*head)->next = NULL; 
    *head = current; 
} 

int main() { 
    struct node2 *head, *tail; 
    head = tail = NULL; 

    addNodeDouble(&head, &tail, 4, 1); 
    addNodeDouble(&head, &tail, 8, 1); 
    addNodeDouble(&head, &tail, 3, 0); 
    addNodeDouble(&head, &tail, 1, 1); 
    addNodeDouble(&head, &tail, 7, 0); 

    printf("\n\nDoubly linked list (reversed): "); 
    ReversedisplayList(&head, &tail); 

    swapElements2(&head, &tail); 
    printf("\nChanged list: "); 
    ReversedisplayList(&head, &tail); 
} 

내가 얻을 :

Doubly linked list (reversed): 1 8 4 3 7 
Changed list: 1 8 4 3 7 

하지만 내가 원하는 :

Changed list: 7 8 4 3 1 
+1

노드 자체 대신 노드의 데이터를 교환하는 것이 좋습니다. 이 경우 물건을 훨씬 쉽게 만들 수 있습니다. – Hypino

+0

@Hypino 아니요, 실제로 노드를 바꾸고 싶습니다. – user3120283

답변

1

첫 번째 요소와 head 및 tail 요소를 바꾸려면 다음 절차를 수행해야합니다. 먼저 임시 변수에서 머리의 꼬리와 다음 노드의 이전 노드를 가져와 머리와 꼬리의 다음 및 이전 포인터를 교환해야합니다.

void swapElements2(struct node2 **head, struct node2 **tail) { 
    struct node2 *ttail, *thead; 

    ttail = (*tail) -> prev; 
    thead = (*head) -> next; 

    (*head) -> next = NULL; 
    (*tail) -> prev = NULL; 

    (*head) -> prev = ttail; 
    (*tail) -> next = thead; 

    ttail -> next = (*head); 
    thead -> prev = (*tail); 

    (*tail) = ttail -> next; 
    (*head) = thead -> next; 
} 
+0

들여 쓰기가 정확하지 않고 코드 덤프가 아닌 간단한 설명 만 있어야합니다. . –

+0

감사합니다. 지금 설명을 추가했습니다. – jafarbtech

+0

코드를 올바르게 들여 씁니다. 그러면 더 읽기 쉽고 쉽게 따라 할 수 있습니다. –

0

당신은 (*head) -> prev(*tail) -> prev을 변경하는 것을 잊지.

(*head)->prev = temp; 
(*tail)->prev = NULL; 
+0

흠, swapElements2 함수 끝에이 명령을 추가 하시겠습니까? 왜냐하면 내가 그렇게하면 "Changed list : 1"([here] (http://ideone.com/YouUbd)) – user3120283

관련 문제