【发布时间】:2014-02-04 18:21:43
【问题描述】:
我的双向链表的冒泡排序功能有问题。 当我以单链接方式(仅使用 ->next)对节点进行排序时,它正在工作,但我无法使用 ->prev 指针。 这是我正在使用的代码:
void sort(int count)
{
struct data *tmp,*current,*nextone;
int i,j;
for(i=0;i<count;i++)
{
current = first;
for(j=0;j<count-1-i;j++ )
{
if(current->number > current->next->number)
{
nextone = current->next;
current->next = nextone->next;
nextone->next = current;
if(current == first)
{
first = nextone;
current = nextone;
}
else
{
current = nextone;
tmp->next = nextone;
}
}
tmp = current;
current = current->next;
}
}
}
这是我正在使用的结构(列表的第一个和最后一个元素的全局变量):
struct data
{
int id;
char name[20];
int number;
struct data *next;
struct data *prev;
};
struct data *first = NULL;
struct data *last = NULL;
【问题讨论】:
-
贴出在使用双链表节点移动时给您带来麻烦的代码。
标签: c list sorting bubble-sort doubly-linked-list