堆栈实现链接列表

时间:2015-07-16 01:16:16

标签: c linked-list stack

我遇到了以下程序的问题,特别是读取功能,我正在读取文本文件中的数据并根据数据构建堆栈。我在函数中有评论,所以你可以看到我的思考过程。当我使用显示功能时,程序只是在无限循环中重复最后一个条目。感谢您对我的代码提出的任何建议,谢谢。

#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.txt\n");

    if (data == NULL)
    {
        printf("Could not open file RecitationFiveInput.txt\n");
    }
    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.txt\n");
    }

    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 Stock\n");
    printf("=============================================================================\n");

    if(top1 == NULL)
    {
        printf("List is empty.\n");
        return;
    }
    else
    {
        while(top1 != NULL)
        {
            printf("   %d      %s            %s        %s            %.2lf         %d\n", 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 Operations\n");
        printf("=================\n");
        printf("1. Stock Produce Department\n");
        printf("2. Display Produce Inventory\n");
        printf("3. Reverse Order of Produce Inventory\n");
        printf("4. Export Produce Inventory\n");
        printf("5. Exit Program\n");
        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;
}

1 个答案:

答案 0 :(得分:0)

您需要修改您的阅读功能,如下所示: 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.txt\n");

if (data == NULL)
{
    printf("Could not open file RecitationFiveInput.txt\n");
}
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.txt\n");
}

fclose(data);

return;

}

并设置struct produceItem * top = NULL;在主要。 它工作正常。

相关问题