Brian
Brian

Reputation: 187

Linked List Search and Read Data problems

I am working on a program that can process nodes/linked lists for the struct inventory. However I have encountered some problems. I do not quite understand how to solve it.

  1. The first problem is in Search List function, where my 'current' in the while loop parameter gets highlighted red.
  2. The second problem is in my Read Data function, the commented out section, where when I run the program and type in the item name, it will crash and stop working.

.

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

Upvotes: 0

Views: 66

Answers (1)

Rohan
Rohan

Reputation: 53366

For 1)

current->link->item.dataitem is void *. So you need to convert it to appropriate data type pointer before using to current->link->item.dataitem.invPartNo. Also as dataitem is pointer, you want it using -> as current->link->item.dataitem->invPartNo.

May be like this:

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

For 2)

Update line

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

to

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

And make sure you do not input more than 35 chars.

Upvotes: 0

Related Questions