Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为链表实现shell排序?_C - Fatal编程技术网

C 为链表实现shell排序?

C 为链表实现shell排序?,c,C,我试图在单链接列表上实现shell排序。我试图做的是根据inc值获取相应的元素并对它们进行排序。但是,我只熟悉数组上的shell排序实现,而不熟悉链表。感谢您的帮助 struct node { int data; struct node *next; };

我试图在单链接列表上实现shell排序。我试图做的是根据
inc
值获取相应的元素并对它们进行排序。但是,我只熟悉数组上的shell排序实现,而不熟悉链表。感谢您的帮助

struct node
  { int data;
    struct node *next;
  };                                                                                                                                

typedef struct node n;

void add(int a);
void shellsort(int size);
void display();
void moveptr(n*a, int distance);

n* head=NULL;

void main()
  { int i,x,size;
    printf("How many elements do you want to enter: ");
    scanf("%d",&size);
    printf("Enter the data: ");
    for(i=0;i<size;i++)
      { scanf("%d",&x);
        add(x);
      }
     
    printf("List currently is: ");
    display();
    shellsort(size);
    printf("\nafter sorting list is: ");
    display();
  }

void add(int a)
  { n *temp = (n*)malloc(sizeof(n));
    temp->data=a;
    temp->next=head;
    head=temp;
  } 

void display()
 { n* temp=head;
   while(temp!=NULL)
     { printf("%d-->",temp->data);
          temp=temp->next;
      }
  }

void moveptr(n *ptr, int distance) // this moves a temp pointer to required location
  { int i=0;
    while(i<distance)
       {
           ptr=ptr->next;
           i=i+1;
       }   
  }

void shellsort(int size)
  { int i,j,temp,inc=size/2;
    n *a=head,*b=head;
    do{
        for(i=0;i<size;i++)
          { 
            for(j=i+inc;j<size;j+=inc)
             { b=head;
               moveptr(b,j);
               if(a->data > b->data)
                 { temp=b->data;
                   b->data=a->data;
                   a->data=temp;
                 }
             }
             a=a->next;
            }
        inc=inc/2;
     }while(inc>=1);
 }
struct节点
{int数据;
结构节点*下一步;
};                                                                                                                                
typedef结构节点n;
无效添加(INTA);
空壳排序(整数大小);
void display();
void moveptr(n*a,int距离);
n*head=NULL;
void main()
{int i,x,大小;
printf(“要输入多少元素:”);
scanf(“%d”,大小(&S);
printf(“输入数据:”);
对于(i=0;idata=a;
温度->下一步=头部;
压头=温度;
} 
无效显示()
{n*temp=头部;
while(temp!=NULL)
{printf(“%d-->”,temp->data);
温度=温度->下一步;
}
}
void moveptr(n*ptr,int distance)//将临时指针移动到所需位置
{int i=0;
而(不精确;
i=i+1;
}   
}
空壳排序(整数大小)
{int i,j,temp,inc=size/2;
n*a=水头,*b=水头;
做{
对于(i=0;i b->data)
{temp=b->数据;
b->data=a->data;
a->data=temp;
}
}
a=a->next;
}
inc=inc/2;
}而(inc>=1);
}

如果您坚持使用shell排序:

  • 为列表项分配一个指针数组
  • 初始化此数组以指向列表中的各个项目
  • 使用shell排序对数组进行排序
  • 按数组元素的顺序重新链接列表
  • 释放列表

为什么您认为链表上的shell排序是个好主意?这似乎是一个奇怪的要求。shell排序基本上只用于对数组进行排序;它在数组中比较的元素之间使用可变间隙,这会变得非常缓慢(最多)如果你必须单步遍历链表而不是索引数组。你可能需要将列表转换为数组,然后对数组进行排序并将结果转换回链表。是的,我完全同意你的看法。也许我应该问问我的教授他的推理。我认为根本不存在这种算法。Shell排序需要快速-寻找数据结构。