2016-11-09 79 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

要交換第一個元素和頭尾元素,您必須通過以下過程。 首先,我們必須讓尾部和頭部的下一個節點一個節點在一些臨時變量和交換的頭和尾的next和prev指針。

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函數的末尾添加這些命令?因爲如果我這樣做,我得到「更改列表:1」([這裏](http://ideone.com/YouUbd)) – user3120283