【发布时间】:2019-10-09 13:45:59
【问题描述】:
我正在做一个登机队列,我正在使用一个简单的冒泡排序算法对我拥有的队列进行排序,我使用 2 个指针来存储我正在比较的值,并使用 2 个双指针来修改我在每个节点内的值想换。我的问题是,当我想更改双指针指向的位置以遍历所有队列时,它永远不会编译,我已经尝试过:
这个:**r = &(r->next),
这个:**r = &(*r->next),
或者这个:**r = &(**r->next)
当我尝试更改双指针“a”指向的位置时也会发生同样的情况。这是完整的方法,以防万一您需要查看它,
int sortBoardingQueue(BoardingQueue *qPtr){
int size = calculateSize(qPtr);
Passenger *t = qPtr->head;
Passenger *a = qPtr->head->next;
Passenger **r = &(qPtr->head);
Passenger **q = &(qPtr->next);
for (int i = size-1; i <=0; i--)
{
while(t != NULL){
if (t->seatNumber>a->seatNumber)
{
**r = *a;
**q = *t;
}
t = t->next;
a = a->next;
**r = &(r->next);
**a = &(a->next);
}
}
}
//the declarations of the structs I am using in my header file
typedef struct boardingQueue {
Passenger* head; // points to the Passenger at the front/head of the queue
Passenger* tail; // points to the Passenger at the end/tail of the queue
} BoardingQueue;
typedef struct passenger {
char name[30]; // the passenger's name
double passportNumber; // their passport number
int seatNumber; // their seat number on the plane
struct passenger* next; // a pointer to the next passenger in the queue after this one
} Passenger;
【问题讨论】:
-
@Andrés Álvarez 显示乘客的定义。
-
a不是双指针,**r是Passenger,不是指针。如果要更改r指向的位置,请为r分配一个新值。不要随意插入和号和星号,试图偶然发现正确的代码(即使它可以编译,也很可能不会)。 -
是C语言:
-
编辑:结构体乘客已经在代码中
-
struct boardingQueue 应该包含 "next" 用于初始化 q (Passenger **q = &(qPtr->next);)