2016-02-29 36 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 

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