2016-02-29 39 views
-1

我的列表中的節點的代碼是:改變一個鏈表的節點

struct list{           
    int value;          
    struct list *next;        
}; 

我想打一個交換功能是這樣的:

void swap(struct list *head , int v) 

用戶給出了一些v和程序在列表中搜索它並使用下一個節點進行更改。 例如,如果用戶給出3並且列表包含:2 -1 7 3 -5 4,交換功能將使列表如下:2 -1 7 -5 3 4 任何想法?

我爲交換下面的代碼:

void swap(struct list *head, int v){ 
     struct list *before=NULL; 
     struct list *found=NULL; 
     struct list *after=NULL; 


if(head==NULL){ 
    printf("Case of empty list !\n); 
} 

before=head; 
found=head; 
while(found->next !=NULL){ 
     if (found->value==v){ 
       after = before->next; 
       before = found->next; 
     } 
     before = found; 
     found = found->next; 
     after = found->next; 
} 
return; 
} 
+2

你到現在爲止寫了哪些在交換功能中不起作用的東西? – reshad

+0

我在函數中使用了3個用head初始化的指針,然後花了一段時間找到所需值的節點。第一個指針位於具有值的節點之前的節點,具有值的節點之後的第三個指針以及第三個指針。 –

+1

請使用編輯按鈕將其添加到您的原始問題。 – reshad

回答

3

試試這個方法:

  1. 搜索int v的鏈接列表直到最後一個節點

  2. 如果發現並且該節點不是最後一個節點,則交換該節點的數據。

  3. 如果它是最後一個節點,那麼你不能交換。你必須找到另一種情況

  4. 如果該節點是唯一的節點,則還必須考慮其他條件一樣,這會不會是最後一個節點

如果你想交換節點,然後試試這個代碼

void swap(node *head, int v) { 

node * prev,*curr,*NEXT,*temp 

curr=head; 

prev=curr; 

NEXT=curr->next; 

while(curr!=NULL){ 

if(curr->data==v){ 

    if(curr->next!=NULL){ 
    prev->next=NEXT; 
    temp=NEXT->next; 
    NEXT->next=curr; 
    curr->next=temp; 

    break; 
    } 

    else{ 
    printf("\nThere is no further node to swap "); 
    } 
} 

prev = curr; 
curr = curr->next; 
NEXT = curr->next; 

} 

}  
0

好工作過的例子,你可以做類似如下: 在列表中找到包含3個節點。這應該很容易,因爲您將指針傳遞給列表頭部。一旦找到3,將-5保存在臨時節點指針中。讓具有「7」的節點指向與臨時指針相同的位置。然後讓-5的下一個指針指向3.最後,重新指派3指向4.邏輯與任何交換函數相同。您將使用一個臨時變量來存儲您正在交換的內容。然後重新分配值。

更普遍的解釋是:

  1. 運行到節點含釩
  2. 然後,保存下一個節點在一個臨時指針
  3. 將以前節點的下一個指針等於暫時指針
  4. 。將包含「v」的節點的下一個指針設置爲等於臨時節點的下一個指針
  5. 將臨時指針的「下一個」指針設置爲等於包含「v」的節點
  6. 考慮極端情況,如:節點不能被換的原因是在列表中的最後一個節點,等等
2

由於您只有結構中的數字,您應該能夠交換它們。

注意以下情況除外:

  • 當沒有要素交換(即列表的末尾)。
  • 當元素不存在於列表中時。

在您的代碼:

void swap(struct list *head, int v){ 

    int temporary_number; 
    struct list *found=NULL; 

    if(head==NULL){ 
     printf("%s", "Case of empty list !\n"); 
    } 

    found = head; 

    while(found->next != NULL){ 
     if (found->value == v){ 
       temporary_value = found->next->value 
       found->next->value = found->value 
       found->value = temporary_value 
     } 
     found = found->next 
    } 

    return; 
} 
0

不要着急。 :) 正如你所說,你需要交換節點本身,而不是交換隻是他們的價值觀,那麼你在這裏。 :)

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

struct list 
{           
    int value;          
    struct list *next;        
}; 

void push_front(struct list **head, int value) 
{ 
    struct list *tmp = malloc(sizeof(struct list)); 

    if (tmp) 
    { 
     tmp->value = value; 
     tmp->next = *head; 
     *head = tmp; 
    } 
} 

void display(struct list *head) 
{ 
    for (struct list *tmp = head; tmp; tmp = tmp->next) 
    { 
     printf("%d ", tmp->value); 
    } 
    printf("\n"); 
}  

void swap(struct list **head, int value) 
{ 
    while (*head && (*head)->value != value) 
    { 
     head = &(*head)->next; 
    } 

    if (*head && (*head)->next) 
    { 
     struct list *next = (*head)->next->next; 
     (*head)->next->next = *head; 
     *head = (*head)->next; 
     (*head)->next->next = next; 
    }   
} 

int main(void) 
{ 
    struct list *head = NULL; 

    push_front(&head, 4); 
    push_front(&head, -5); 
    push_front(&head, 3); 
    push_front(&head, 7); 
    push_front(&head, -1); 
    push_front(&head, 2); 

    display(head); 

    swap(&head, 2); 
    display(head); 

    swap(&head, 2); 
    display(head); 

    swap(&head, 2); 
    display(head); 

    swap(&head, 2); 
    display(head); 

    swap(&head, 2); 
    display(head); 

    swap(&head, 2); 
    display(head); 

    return 0; 
} 

程序輸出是

2 -1 7 3 -5 4 
-1 2 7 3 -5 4 
-1 7 2 3 -5 4 
-1 7 3 2 -5 4 
-1 7 3 -5 2 4 
-1 7 3 -5 4 2 
-1 7 3 -5 4 2 

或者一個更有趣的例子

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

struct list 
{           
    int value;          
    struct list *next;        
}; 

void push_front(struct list **head, int value) 
{ 
    struct list *tmp = malloc(sizeof(struct list)); 

    if (tmp) 
    { 
     tmp->value = value; 
     tmp->next = *head; 
     *head = tmp; 
    } 
} 

void display(struct list *head) 
{ 
    for (struct list *tmp = head; tmp; tmp = tmp->next) 
    { 
     printf("%d ", tmp->value); 
    } 
    printf("\n"); 
}  

void swap(struct list **head, int value) 
{ 
    while (*head && (*head)->value != value) 
    { 
     head = &(*head)->next; 
    } 

    if (*head && (*head)->next) 
    { 
     struct list *next = (*head)->next->next; 
     (*head)->next->next = *head; 
     *head = (*head)->next; 
     (*head)->next->next = next; 
    }   
} 

int main(void) 
{ 
    struct list *head = NULL; 
    int a[] = { 2, -1, 7, 3, -5, 4 }; 

    for (size_t i = 0; i < sizeof(a)/sizeof(*a); i++) 
    { 
     push_front(&head, a[i]); 
     display(head); 
     for (size_t j = 0; j < i; j++) 
     {    
      swap(&head, a[i]); 
      display(head); 
     } 
     printf("\n"); 
    } 

    display(head); 

    return 0; 
} 

程序輸出是

2 

-1 2 
2 -1 

7 2 -1 
2 7 -1 
2 -1 7 

3 2 -1 7 
2 3 -1 7 
2 -1 3 7 
2 -1 7 3 

-5 2 -1 7 3 
2 -5 -1 7 3 
2 -1 -5 7 3 
2 -1 7 -5 3 
2 -1 7 3 -5 

4 2 -1 7 3 -5 
2 4 -1 7 3 -5 
2 -1 4 7 3 -5 
2 -1 7 4 3 -5 
2 -1 7 3 4 -5 
2 -1 7 3 -5 4 

2 -1 7 3 -5 4 

像往常一樣,我的回答是最好的。:)