[英]Passing a linked list as an argument to a function in C
我正在編寫一個創建鏈接列表的程序。 列表的每個節點存儲 4 個關於患者的數據。 這部分有效。 在用戶決定輸出所有數據后。 將計算數據庫中人的平均身高。 我可以在主函數中執行此操作,但我無法使用外部函數void avr_height(LIST_HEAD *InpList)
。
我的問題是我在這里做錯了什么? 我懷疑,由於我對指針的理解有限,我在遍歷外部函數中的列表時搞砸了一些事情。 或者錯誤地將列表傳遞給外部函數。
那是在主函數中計算平均值的部分
pTemp = DataBase.pFirst_elmt;
while(pTemp != NULL)
{
iCurrent = pTemp->Patient.iHeight;
iSum = iSum + iCurrent;
pTemp = pTemp->next;
iNumb++;
}
fAvrageH = ((float)iSum) / ((float)iNumb);
printf("\n\nThe average height of these patients is: %.2f\n\n", fAvrageH);
這是我的外部功能,應該做同樣的事情。
void avr_height(LIST_HEAD *InpList)
{
int iNumb = 0;
int iSum = 0;
int iCurrent = 0;
float fAvrageH = 0;
NODE *pTemp = NULL;
pTemp = InpList;
while(pTemp != NULL)
{
iCurrent = pTemp->Patient.iHeight;
iSum = iSum + iCurrent;
pTemp = pTemp->next;
iNumb++;
}
fAvrageH = ((float)iSum) / ((float)iNumb);
printf("The average height of these patients is: %f", fAvrageH);
}
對於函數void avr_height(LIST_HEAD *InpList)
我傳遞了一個struct LIST_HEAD Database
作為參數。 這是通過調用avr_height(&DataBase)
完成的
所有代碼如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 80
//List element's data
typedef struct{
int iID;
int iAge;
int iHeight;
//Defining a character array for storing the name
char sName[STRSIZE];
} PATIENT;
//Defining the list elements (German: KNOTEN)
typedef struct NODE{
//Initialising a Patient of the data type PATIENT. Then pointing to the next element
PATIENT Patient;
struct NODE *next;
} NODE;
//Defining the list element's head
typedef struct{
NODE *pFirst_elmt;
int iNumber;
}LIST_HEAD;
//Function for creating the actual list
void add_patient(LIST_HEAD *List)
{
//Allocating memory for list elements
NODE *pTemp;
pTemp = (NODE*) malloc(sizeof(NODE));
//Creating new elements of the linked list (and checking input validity)
printf("\nEnter patient ID:");
if(scanf("%d", &pTemp->Patient.iID) == 0){
printf("Invalid input...");
}
printf("Enter patient's name:");
if(scanf("%s", pTemp->Patient.sName) == 0){
printf("Invalid input...");
}
printf("Enter patient age:");
if(scanf("%d", &pTemp->Patient.iAge) == 0){
printf("Invalid input...");
}
printf("Enter patient height:");
if(scanf("%d", &pTemp->Patient.iHeight) == 0){
printf("Invalid input...");
}
//Setting the first element of the list AS the the next element, in front of the new element
pTemp -> next = List -> pFirst_elmt;
//Designating the new element of the list as the first element of the list. Increase the number of elements in the list
List -> pFirst_elmt = pTemp;
List -> iNumber++;
printf("\n");
}
//Calculating the average height of the patients: i.e. ITTERATING OVER A LINKED LIST
void avr_height(LIST_HEAD *InpList)
{
int iNumb = 0;
int iSum = 0;
int iCurrent = 0;
float fAvrageH = 0;
NODE *pTemp = NULL;
pTemp = InpList;
while(pTemp != NULL)
{
iCurrent = pTemp->Patient.iHeight;
iSum = iSum + iCurrent;
pTemp = pTemp->next;
iNumb++;
}
fAvrageH = ((float)iSum) / ((float)iNumb);
printf("The average height of these patients is: %f", fAvrageH);
}
int main()
{
//Variable initialisation
int i = 0;
LIST_HEAD DataBase;
DataBase.pFirst_elmt = NULL;
DataBase.iNumber = 0;
NODE *pTemp = NULL;
//Variables for the height calculation
int iNumb = 0;
int iSum = 0;
int iCurrent = 0;
float fAvrageH = 0;
//Loop for creating list elements if specified by user:
do
{
printf("[0] Output the stored data [1] Enter data\n");
printf("What do you wish to do?\n");
//Checking input validity
if(scanf("%i", &i) == 0){
printf("\nInvalid input...");
}
if(i == 1){
add_patient(&DataBase);
}
} while(i);
pTemp = DataBase.pFirst_elmt;
printf("\n\n");
//Outputting the linked list: i.e. ITTERATING OVER A LINKED LIST
while(pTemp != NULL)
{
printf("\nPatient ID: %d Patient name: %s Patient age: %d Patient height: %d", pTemp->Patient.iID, pTemp->Patient.sName, pTemp->Patient.iAge, pTemp->Patient.iHeight);
pTemp = pTemp->next;
}
//Calculating the average height
pTemp = DataBase.pFirst_elmt;
while(pTemp != NULL)
{
iCurrent = pTemp->Patient.iHeight;
iSum = iSum + iCurrent;
pTemp = pTemp->next;
iNumb++;
}
fAvrageH = ((float)iSum) / ((float)iNumb);
printf("\n\nThe average height of these patients is: %.2f\n\n", fAvrageH);
//Alternative way of calculating the height with a linked list
avr_height(&DataBase);
printf("\n\n");
return 0;
}
感謝所有幫助,在此先感謝!
編譯器應該為函數發出一條消息
void avr_height(LIST_HEAD *InpList)
{
int iNumb = 0;
int iSum = 0;
int iCurrent = 0;
float fAvrageH = 0;
NODE *pTemp = NULL;
pTemp = InpList;
//...
因為指針 pTemp 和 InpList 具有不同的類型。
你需要寫
pTemp = InpList->pFirst_elmt;
這個變量也是多余的
int iNumb = 0;
因為列表已經有數據成員int iNumber;
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.