我正在执行登机队列,并且我使用一种简单的冒泡排序算法对我拥有的队列进行排序,我正在使用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;
答案 0 :(得分:0)
如果指针Private Sub Command30_Click()
'set variables
Dim oWord As Word.Application
Dim oWdoc As Word.Document
Dim wdInputName As String
Dim wdOutputName As String
Dim outFileName As String
' Set Template Path
'------------------------------------------------
wdInputName = CurrentProject.Path & "X:\PM\PM-O\PM-OQ\PM-OQRA\PROCESSES\03_Pigments\98_ Database\05_Inventory\02_Bulk_letter\word_template.docx"
' Create unique save filename with minutes
' and seconds to prevent overwrite
'------------------------------------------------
outFileName = "Merck_InvStatements_" & Format(Now(), "yyyymmddmms")
' Output File Path w/outFileName
'------------------------------------------------
wdOutputName = CurrentProject.Path & "\desktop\completed\" & outFileName
Set oWord = New Word.Application
Set oWdoc = oWord.Documents.Open(wdInputName)
' Start mail merge
'------------------------------------------------
With oWord
oWord.Visible = True
With ActiveDocument.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource _
Name:=CurrentProject.FullName, _
AddToRecentFiles:=False, _
LinkToSource:=True, _
Connection:="Q_SERIAL_INVENTORY_A02", _
SQLStatement:="SELECT * FROM [Q_SERIAL_INVENTORY_A02]"
.Destination = wdSendToNewDocument
.Execute Pause:=False
End With
End With
' Hide Word During Merge
'------------------------------------------------
oWord.Visible = False
' Save file as PDF
' Uncomment the line below and comment out
' the line below "Save file as Word Document"
'------------------------------------------------
'oWord.ActiveDocument.SaveAs2 wdOutputName & ".pdf", 17
' Save file as Word Document
'------------------------------------------------
oWord.ActiveDocument.SaveAs2 wdOutputName & ".docx", 16
' Quit Word to Save Memory
'------------------------------------------------
oWord.Quit savechanges:=False
' Clean up memory
'------------------------------------------------
Set oWord = Nothing
Set oWdoc = Nothing
End Sub`
这样声明和初始化
r
然后在此声明
Passenger **r = &(qPtr->head);
**r = &(r->next);
的类型为**r
。此外,r指向指针。此表达式Passenger
不正确,因为指针的下一个没有数据成员(指针不是结构)。
所以这三个表达式
r->next
不正确。例如表达式
**r = &(r->next),
**r = &(*r->next),
**r = &(**r->next)
等同于
&(**r->next)
您的意思似乎是以下
&(** ( r->next ) )
考虑到此声明
r = &( *r )->next;
也是无效的,因为结构Passenger **q = &(qPtr->next);
没有数据成员BoardingQueue
。
也是for循环中的条件
next
无效。仅在for (int i = size-1; i <=0; i--)
小于或等于1的情况下有效。:)
您必须至少使用有效的C表达式重新编写函数size
。之后,您可以提出一个问题:为什么我的排序功能不起作用。:)
答案 1 :(得分:0)
-下次,如果发布的代码段恰好是您要向编译器提供的内容,并且如果您将编译器错误准确地包含在其中,并且包含以下内容,则会更容易得到答案:您的问题。
我重组了示例代码,并添加了一个主要功能来显示一些编译器错误,以显示我的意思:
#define NULL 0
//the declarations of the structs I am using in my header file
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;
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;
int sortBoardingQueue(BoardingQueue *qPtr){
int size = 3; // func undefined?? 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);
}
}
}
int main(){
// make passengers
Passenger a = {"p1", 111.0, 1, NULL};
Passenger b = {"p2", 222.0, 2, NULL};
Passenger c = {"p2", 222.0, 3, NULL};
// make boarding queue a->c->b
a.next = &c;
c.next = &b;
BoardingQueue q = {&a, &b};
sortBoardingQueue(&q);
}
dp.c:在“ sortBoardingQueue”函数中:dp.c:22:27:错误:
“ BoardingQueue {aka struct boardingQueue}”没有名为“ next”的成员
Passenger **q = &(qPtr->next);
这是因为boardingQueue没有成员next
,只有head
和tail
。 (也许您想让它指向乘客?我无法从上下文中看出您的意图)
dp.c:33:22: error: request for member ‘next’ in something not a structure or union
`**r = &(r->next);`
为此,r的类型为Passenger **
,因此它指向一个乘客结构。如果您确实要访问该结构的下一个字段,则必须使用(*r)->next
dp.c:34:13: error: invalid type argument of unary ‘*’ (have
‘Passenger {aka struct passenger}’)
**a = &(a->next);
为此,a
是单个指针。您只能取消引用一次。
所以,我希望这对您的任务有所帮助!下次,如果您发布一个最小的,可复制的示例https://stackoverflow.com/help/minimal-reproducible-example,则势必会更快地获得更好的答案。