我的列表节点的代码是:
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 有任何想法吗 ?
我为swap做了以下代码:
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;
}
答案 0 :(得分:3)
尝试这种方式:
在链接列表中搜索int v
,直到最后一个节点
如果找到并且该节点不是最后一个节点,则交换该节点的数据。
如果是最后一个节点,则无法进行交换。你必须找到另一个案例
如果该节点是唯一的节点,那么您还必须考虑另一个条件,就像它将是最后一个节点一样
如果要交换节点,请尝试使用此代码
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;
}
}
答案 1 :(得分:2)
由于你只有结构中的数字,你应该只能交换它们。
请注意以下例外情况:
在您的代码中:
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
/**
* Created by Shreekrishna on 2/29/2016.
*/
public class PackageTypesDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
}
答案 2 :(得分:0)
在您的示例中,您可以执行以下操作: 在列表中找到包含3的节点。这应该很简单,因为您将指针传递到列表的头部。找到3后,在临时节点指针中保存-5。将节点与" 7"指向与临时指针相同的位置。然后让-5的下一个指针指向3.最后,重新指定3指向4.逻辑与任何交换功能相同。您将使用临时变量来存储您正在交换的内容。然后重新分配值。
更一般的解释:
答案 3 :(得分: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
像往常一样,我的答案是最好的。:)