gpt4 book ai didi

c - 在 C 中传递链表时如何解决这些冲突类型错误?

转载 作者:行者123 更新时间:2023-11-30 16:09:31 25 4
gpt4 key购买 nike

我对 C 语言很陌生,对链表没有太多经验。我正在尝试在 FCFS 中创建一个磁盘调度程序,并将轨道请求添加到队列(链接列表)中进行分配,但在尝试编译时收到大量警告和几个错误,我无法弄清楚原因。我收到的错误位于第 86 行和第 101 行:“AddToList”和“RemoveFromList”的类型冲突。如果有人可以帮助我找出问题所在,并且如何解决它,我们将不胜感激。谢谢!

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <values.h>
#include <time.h>
#include <stdbool.h>

/* function declarations */
int trackReqs(void);
int numTrack(void);
void AddToList(struct Queue_Struct *req_queue, int trackNum);
int RemoveFromList(struct Queue_Struct *req_queue);

// global variable declarations/initializations
unsigned seed;
int fileReqs;
bool first = true;

struct Queue_Node{
int trackNumber;
struct Queue_Node *next;
};

struct Queue_Struct{
struct Queue_Node *q_head;
struct Queue_Node *q_tail;
};

struct Queue_Struct fcfs;

void main(){
fcfs.q_head = NULL;
fcfs.q_tail = NULL;
printf("Seed for the random number generator: ");
scanf("%d", &seed);
srand(seed);
//printf("\n");
printf("Number of file requests: ");
scanf("%d", &fileReqs);
//printf("\n");

// local variable declarations/initializations
int totalReqs = 0;
int numFileReqs = 0;
float totalHeadMove = 0;
int currTrack = 0;
float diff;
float average;

do { // do this...
int numTrackReqs = trackReqs(); // call function to get a random number between 1 and 5 to represent the number of track requests for the current file request
for (int i = 0; i < numTrackReqs; i++) { // for each track request for the current file request...
int trackNum = numTrack(); // call function to get a random number between 0 and 799 to represent the number of the track requested
AddToList(&fcfs, trackNum); // call function to add the track request to the queue
first = false;
}
int nextTrack = RemoveFromList(&fcfs); // call function to remove the next (first) track request from the queue (signifying that the disk head will be moved to that track) and have that track returned
diff = abs((float)nextTrack - (float)currTrack); // calculate the head movement for the current file request
totalHeadMove += diff; // add the head movement for the current file request to the total head movement
totalReqs++; // increase number of total requests by 1
currTrack = nextTrack; // make the current track now the next track
numFileReqs++; // increase number of file requests by 1
} while(numFileReqs <= fileReqs); // ...for each file request
average = totalHeadMove / (float) numFileReqs; // calculate the average total head movement for each file request and print the result
printf("Average head movement: %5.2f\n", average);
}

int trackReqs(void){
int rand_num = (rand() % (5 - 1 + 1)) + 1; // generate random number from 1 to 5 representing number of track requests for the current file request
return rand_num;
}

int numTrack(void){
int rand_num = rand() % 800; // generate random number from 0 to 799 representing
return rand_num;
}

void AddToList(struct Queue_Struct *req_queue, int trackNum){
struct Queue_Node *newnode;
newnode = (struct Queue_Node *) malloc(sizeof(struct Queue_Node));
newnode->next = NULL;
newnode->trackNumber = trackNum;
if(req_queue->q_tail == NULL){
req_queue->q_head = newnode;
req_queue->q_tail = newnode;
return;
}
req_queue->q_tail->next = newnode;
req_queue->q_tail = newnode;
return;
}

int RemoveFromList(struct Queue_Struct *req_queue){
struct Queue_Node *loc;
int first_req;
if(req_queue->q_head == NULL){
printf("***Error - Queue is Empty***\n");
return(NULL);
}
loc = req_queue->q_head;
first_req = loc->trackNumber;
if(req_queue->q_head == req_queue->q_tail){
req_queue->q_tail = NULL;
req_queue->q_head = NULL;
return first_req;
}
req_queue->q_head = loc->next;
free(loc);
return first_req;
}

最佳答案

发布评论作为答案:问题是相关函数的前向声明引用了定义这些类型之前的结构类型。

我不记得具体的规则,有时您确实需要在定义结构类型之前提及它,但这不是其中之一。

关于c - 在 C 中传递链表时如何解决这些冲突类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59122168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com