我刚刚开始学习链表并且正在搞乱它但后来我遇到了一个问题。我不知道如何访问数据成员来实际比较它。在我的代码中,我提示用户输入成绩,当他们输入-1时,它表示他们已完成。我的第一个想法是让指针指向节点以获取数据,就像我在scanf中所做的那样,但是我无法将指针与整数进行比较。有没有办法让链接列表中的数据成员进行比较?此外,指出其他错误也将受到赞赏,因为我不太了解链接列表。我有以下代码:
<ion-header>
<ion-navbar>
<ion-title>
mapa
</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<agm-map [latitude]="lat" [longitude]="lng" [scrollwheel]="false"
[zoom]="zoom" [styles]="mapCustomStyles">
<agm-marker [latitude]="lat" [longitude]="lng" [iconUrl]="iconurl">
<agm-info-window>
<strong>InfoWindow content</strong>
</agm-info-window>
</agm-marker>
<agm-map-directions [origin]="origin" [destination]="destination"></agm-map-directions>
</agm-map>
<br>
<br>
<br>
<br>
<br>
<div class="form-group">
<input placeholder="Enter source location" autocorrect="off" autocapitalize="off" spellcheck="off"
type="text" class="form-control" #pickupInput [formControl]="destinationInput">
<input placeholder="Enter destination" autocorrect="off" autocapitalize="off" spellcheck="off"
type="text" class="form-control" #pickupOutput [formControl]="destinationOutput">
</div>
</ion-content>
答案 0 :(得分:0)
您的代码存在许多问题:
1)不要直接扫描到列表中 - 使用临时变量
2)始终检查返回值
3)确保初始化变量,即头
尝试类似:
struct Node
{
int grade;
struct Node *next;
};
int main() {
struct Node *head = NULL;
struct Node *temp;
int data;
while (1)
{
printf("Enter the grade: \n ");
if (scanf("%d", &data) != 1)
{
// Illegal input
exit(1);
}
if (data == -1) break; // Stop the loop
temp = malloc(sizeof *temp); // Allocate new element
if (temp == NULL)
{
// Out of mem
exit(1);
}
temp -> next = head; // Insert new element in the front of list
temp -> grade = data;
head = temp; // Move the front (aka head) to the new element
}
// .... add code that uses the list
return 0;
}