链表的堆栈实现



我对以下程序有问题,特别是read函数,我从文本文件中读取数据并根据数据构建堆栈。我在函数中有注释,所以你可以看到我的思考过程。当我使用display函数时,程序只是在无限循环中不断重复最后一个条目。感谢大家对我的代码的建议,谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Struct for linked list node
struct produceItem
{
    char produce[20];
    char type[20];
    char soldBy[20];
    float price;
    int quantityInStock;
    struct produceItem *next;
};
// Function to read in data from file to
void read(struct produceItem **top)
{
    struct produceItem *temp = NULL;
    temp = (struct produceItem *)malloc(sizeof(struct produceItem));
    char line[50];
    char *value;
    FILE *data;
    data = fopen("RecitationFiveInput.txt", "r");
    printf("Trying to open file RecitationFiveInput.txtn");
    if (data == NULL)
    {
        printf("Could not open file RecitationFiveInput.txtn");
    }
    else
    {
        while(fgets(line, sizeof(line), data))
        {
            value = strtok(line, ", ");
            strcpy(temp->produce, value);
            value = strtok(NULL, ", ");
            strcpy(temp->type, value);
            value = strtok(NULL, ", ");
            strcpy(temp->soldBy, value);
            value = strtok(NULL, ", ");
            temp->price = atof(value);
            value = strtok(NULL, ", ");
            temp->quantityInStock = atoi(value);
            temp->next = NULL;
            // the stack is empty
            if (*top == NULL)
            {
                *top = (struct produceItem *)malloc(sizeof(struct produceItem));
                *top = temp;
            }
            // there is at least one node on the stack
            else
            {
                // set the new nodes pointer to look at top
                temp->next = *top;
                // reset top to be temp so it is the top of the stack again
                *top = temp;
            }
        }
        printf("Successfully opened file RecitationFiveInput.txtn");
    }
    fclose(data);
    return;
}
// Function to display the nodes of the linked list that contains the data from the data file
void display(struct produceItem *top)
{
    int value = 1;
    struct produceItem *top1;
    top1 = top;
    printf("=============================================================================n");
    printf(" Item #   Produce          Type          Sold By          Price      In Stockn");
    printf("=============================================================================n");
    if(top1 == NULL)
    {
        printf("List is empty.n");
        return;
    }
    else
    {
        while(top1 != NULL)
        {
            printf("   %d      %s            %s        %s            %.2lf         %dn", value, top1->produce, top1->type, top1->soldBy, top1->price, top1->quantityInStock);
            value++;
            top1 = top1->next;
        }
    }
    return;
}
//Main function
int main()
{
    int input = 0;
    struct produceItem *top;
    while(1)
    {
        printf("nList Operationsn");
        printf("=================n");
        printf("1. Stock Produce Departmentn");
        printf("2. Display Produce Inventoryn");
        printf("3. Reverse Order of Produce Inventoryn");
        printf("4. Export Produce Inventoryn");
        printf("5. Exit Programn");
        printf("Enter your choice: ");
        if(scanf("%d", &input) <= 0)
        {
            printf("Enter only an integer.n");
            exit(0);
        }
        else
        {
            switch(input)
            {
                case 1:
                    read(&top);
                    break;
                case 2:
                    display(top);
                    break;
                case 3:
                    //function
                    break;
                case 4:
                    //function
                    break;
                case 5:
                    printf("You have exited the program, Goodbye!n");
                    return 0;
                    break;
                default:
                    printf("Invalid option.n");
            }
        }
    }
    return 0;
}

你需要修改你的read函数如下:void read(struct produceItem **top){struct produceItem *temp = NULL

//temp = (struct produceItem *)malloc(sizeof(struct produceItem));
char line[50];
char *value;
FILE *data;
data = fopen("RecitationFiveInput.txt", "r");
printf("Trying to open file RecitationFiveInput.txtn");
if (data == NULL)
{
    printf("Could not open file RecitationFiveInput.txtn");
}
else
{
    while(fgets(line, sizeof(line), data))
    {
        temp = (struct produceItem *)malloc(sizeof(struct produceItem));
        value = strtok(line, ", ");
        strcpy(temp->produce, value);
        value = strtok(NULL, ", ");
        strcpy(temp->type, value);
        value = strtok(NULL, ", ");
        strcpy(temp->soldBy, value);
        value = strtok(NULL, ", ");
        temp->price = atof(value);
        value = strtok(NULL, ", ");
        temp->quantityInStock = atoi(value);
 if (*top == NULL)
        {
            //*top = (struct produceItem *)malloc(sizeof(struct produceItem));
            *top = temp;
        }
        // there is at least one node on the stack
        else
        {
            // set the new nodes pointer to look at top
            temp->next = *top;
            // reset top to be temp so it is the top of the stack again
            *top = temp;
        }
    }
    printf("Successfully opened file RecitationFiveInput.txtn");
}
fclose(data);
return;

}

并设置struct produceItem *top = NULL;在主。

相关内容

  • 没有找到相关文章

最新更新