我正在开发一个程序,该程序可以处理结构清单的节点/链接列表。 但是我遇到了一些问题。 我不太了解如何解决。

  1. 第一个问题是搜索列表功能,其中while循环参数中的“当前”以红色突出显示。
  2. 第二个问题是在我的读取数据功能的注释部分中,当我运行该程序并键入项目名称时,它将崩溃并停止工作。

#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;
}

===============>>#1 票数:0 已采纳

对于1)

current->link->item.dataitemvoid * 因此,在使用current->link->item.dataitem.invPartNo之前,需要将其转换为适当的数据类型指针。 另外,由于dataitem是指针,因此您希望使用->作为当前-> current->link->item.dataitem->invPartNo

可能是这样的:

struct NODE* SearchList(struct NODE *start, int oldData)
{
    struct NODE* current = start;
    struct inventory * st = (struct inventory *)current->link->item.dataitem;

    while (st->invPartNo != oldData && current != NULL)
    {
        current = current->link;
        if(current->link)
            st = (struct inventory *)current->link->item.dataitem;
    }
    return current;
}

对于2)

更新行

fscanf_s(fptr, "%s", &temp->invName);

fscanf_s(fptr, "%s", temp->invName);

并确保输入的字符数不超过35个字符。

  ask by Brian Chen translate from so

未解决问题?本站智能推荐: