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

时间:2019-12-01 02:57:03

标签: c compiler-errors linked-list parameter-passing typeerror

我对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;
}

1 个答案:

答案 0 :(得分:0)

发表评论作为答案:问题在于在定义这些类型的函数之前, 这些结构的正向声明。

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