2014-04-21 60 views
0

我正在處理可以處理結構庫存的節點/鏈接列表的程序。 但是我遇到了一些問題。我不太明白如何解決它。鏈接列表搜索和讀取數據問題

  1. 第一個問題是在搜索列表函數中,while循環參數中的'current'被突出顯示爲紅色。
  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; 
} 
+0

爲SearchList,你不能使用開始,而不是使用當前?那麼只需用開始替換電流?我不知道這是否會有所作爲。 –

+0

這樣做仍然給我同樣的錯誤,雖然 – user3011161

+0

'fscanf_s(fptr,「%s」,&temp-> invName);' - >'fscanf_s(fptr,「%s」,temp-> invName,sizeof temp-> invname);' – BLUEPIXY

回答

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字符。

+0

感謝您的快速回復,我現在就試試這個 – user3011161

+0

即使我將fscanf從&temp修改爲temp – user3011161

+0

@ user3011161,程序仍在崩潰,那麼它會在哪裏崩潰? – Rohan