Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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、 对结构队列进行排序_C_Sorting_Queue_Structure - Fatal编程技术网

C、 对结构队列进行排序

C、 对结构队列进行排序,c,sorting,queue,structure,C,Sorting,Queue,Structure,我需要一点帮助。我正在尝试按年份对结构队列进行排序 这是我的结构: struct element{ int id; int sign; int year; int month; double amount; struct element *next; }; struct queue{ struct element *head; struct element *tail

我需要一点帮助。我正在尝试按年份对结构队列进行排序

这是我的结构:

struct element{

        int id;
        int sign;
        int year;
        int month;
        double amount;

        struct element *next;


    };

struct queue{
    struct element *head;
    struct element *tail;
    struct element *heads;
    struct element *temp;
    struct element *temph;

    int size;
};
这就是我写的函数:

void sort(struct queue* queue){

if (queue->size == 0){
        printf("Struct is empty\n");}
else {

        struct element* head=queue->head;
        struct element* heads=queue->heads;
        struct element* temp=NULL;
        struct element* temph=queue->head;
        int i, size=queue->size;        

        for(i=0;i<size-1;i++){
        heads=head->next;
            if((head->year)>(heads->year)){

                temp=head;
                head=heads;
                heads=temp;         
            }


        head=head->next;
        heads=NULL;
        temp=NULL;
        }

head=temph;
}

}
void排序(结构队列*队列){
如果(队列->大小==0){
printf(“结构为空\n”);}
否则{
结构元素*head=queue->head;
结构元素*头=队列->头;
结构元素*temp=NULL;
结构元素*temph=queue->head;
int i,size=queue->size;
对于(i=0;不精确;
如果((人头->年)>(人头->年)){
温度=水头;
头=头;
水头=温度;
}
头部=头部->下一步;
heads=NULL;
温度=零;
}
头=节拍;
}
}
如果((人头->年)>(人头->年))
的话,它就会崩溃。

我很确定我的问题是由于对
head
(我将其命名为
heads
)旁边的结构的引用不当造成的。

我省略了所有不重要的内容,并将链表bubbele排序简化为这个骨架

void sort(struct queue* queue)
{
struct element **pp, *this;

    if (!queue->head ){
        fprintf(stderr, "OMG Struct is empty\n");
        return;
       }

        for(pp = &queue->head; this = *pp; pp = &(*pp)->next){
        struct element *other = this->next;
            if (!this->next) break;
            if (this->year < other->year) continue;
            /* 
            ** Now, Swap this (b) and other (c) 
            ** old situation: @a -> (b) -> (c) -> (d)
            ** new situation: @a -> (c) -> (b) -> (d) 
            */
            *pp = other;              /* @a  -> (c) */
            this->next = other->next; /* (b) -> (d) */
            other->next = this;       /* (c) -> (b) */
        }
/* Note: when we get here, "this" will contain the last non-NULL node in the
** chain, and can be used to reset the tail-pointer
*/
return;
}
void排序(结构队列*队列)
{
结构元素**pp,*此;
如果(!队列->头){
fprintf(stderr,“OMG Struct为空\n”);
返回;
}
对于(pp=&queue->head;this=*pp;pp=&(*pp)->next){
结构元素*其他=此->下一步;
如果(!this->next)中断;
如果(本->年<其他->年)继续;
/* 
**现在,交换这个(b)和其他(c)
**旧情况:@a->(b)->(c)->(d)
**新形势:@a->(c)->(b)->(d)
*/
*pp=其他;/*@a->(c)*/
本->下一步=其他->下一步;/*(b)->(d)*/
其他->下一步=此;/*(c)->(b)*/
}
/*注意:当我们到达这里时,“this”将包含
**链,并可用于重置尾部指针
*/
返回;
}

“它中断”-请更具描述性。如果我理解正确,您的意图是对链接列表进行冒泡排序?是的,我尝试使用冒泡排序。Than:尝试接受@david schwartz的建议:对链接列表进行排序意味着更改指向项目的指针。这可能是->下一个指针或“外部”指针头部指针,指向第一项。@wildplasser,@david schwartz我不确定我是否完全理解。排序应该如下:
temp->next=head->next;
head->next=heads->next;
heads->next=temp->next;
?非常感谢!它帮了我很多忙!)