我正在處理可以處理結構庫存的節點/鏈接列表的程序。 但是我遇到了一些問題。我不太明白如何解決它。鏈接列表搜索和讀取數據問題
- 第一個問題是在搜索列表函數中,while循環參數中的'current'被突出顯示爲紅色。
- 第二個問題是在我的讀取數據功能,註釋掉的部分,當我運行程序並鍵入項目名稱時,它會崩潰並停止工作。
。
#include "stdafx.h"
#include <stdlib.h>
struct InventoryItem
{
int ItemNum;
double Cost;
double Price;
int QOH;
};
typedef struct inventory
{
char invName[36];
int invPartNo;
int invQOH;
float invUnitCost;
float invPrice;
}stock;
struct NODE
{
union
{
int nodeCounter;
void *dataitem;
}item;
struct NODE *link;
};
struct NODE *InitList();
void DisplayNode(struct inventory *);
struct inventory * ReadData(FILE *);
void DisplayList(struct NODE *);
struct NODE* GetNode(FILE *);
void Add2List(struct NODE *, struct NODE *);
//struct NODE* SearchList(struct NODE *, int);
//void DeleteNode(struct NODE *, int);
int _tmain(int argc, _TCHAR* argv[])
{
struct NODE *header;
int PCounter = 2;
header = InitList();
while (PCounter--)
{
Add2List(header,GetNode(stdin));
}
DisplayList(header);
return 0;
}
struct NODE *InitList()
{
struct NODE *temp = (struct NODE*)malloc(sizeof NODE);
temp->item.nodeCounter = 0;
temp->link = NULL;
return temp;
}
void Add2List(struct NODE *start, struct NODE *NewNode)
{
struct NODE *current = start;
while (current->link != NULL)
current = current->link;
current->link = NewNode;
NewNode->link = NULL;
start->item.nodeCounter++;
}
struct NODE* GetNode(FILE *fptr)
{
struct NODE *temp = (struct NODE*)malloc(sizeof NODE);
temp->item.dataitem = ReadData(fptr);
temp->link = NULL;
return temp;
}
void DisplayList(struct NODE *start)
{
struct NODE *current = start->link;
while (current != NULL)
{
DisplayNode((struct inventory *)current->item.dataitem);
current = current->link;
}
}
void DisplayNode(struct inventory *stuff)
{
/*
char invName[36];
int invPartNo;
int invQOH;
float invUnitCost;
float invPrice;
*/
printf("Name: %s", stuff->invName);
printf("Part Number: %d", stuff->invPartNo);
printf("Quantity on hand: %d", stuff->invQOH);
printf("Unit Cost: %0.2f", stuff->invUnitCost);
printf("Price %0.2f", stuff->invPrice);
}
struct inventory * ReadData(FILE *fptr)
{
struct inventory *temp = (struct inventory *)malloc(sizeof inventory);
//problem here, when i type in the name during run, program will stop working
//if(fptr==stdin)
// printf("Enter item name: ");
//fscanf_s(fptr, "%s", &temp->invName);
if(fptr==stdin)
printf("Enter item part number: ");
fscanf_s(fptr, "%d", &temp->invPartNo);
if(fptr==stdin)
printf("Enter item quantity on hand: ");
fscanf_s(fptr, "%d", &temp->invQOH);
if(fptr==stdin)
printf("Enter item unit cost: ");
fscanf_s(fptr, "%f", &temp->invUnitCost);
if(fptr==stdin)
printf("Enter item price: ");
fscanf_s(fptr, "%f", &temp->invPrice);
return temp;
}
struct NODE* SearchList(struct NODE *start, int oldData)
{
struct NODE* current = start;
//error here, says 'expression must have a class type' for current
while (current->link->item.dataitem.invPartNo != oldData)
current = current->link;
return current;
}
void DeleteNode(struct NODE *start, int oldData)
{
struct NODE *current, *oldNode;
current = SearchList(start, oldData);
oldNode = current->link;
current->link = oldNode->link;
free(oldNode);
start->item.nodeCounter -= 1;
}
爲SearchList,你不能使用開始,而不是使用當前?那麼只需用開始替換電流?我不知道這是否會有所作爲。 –
這樣做仍然給我同樣的錯誤,雖然 – user3011161
'fscanf_s(fptr,「%s」,&temp-> invName);' - >'fscanf_s(fptr,「%s」,temp-> invName,sizeof temp-> invname);' – BLUEPIXY