我在以下程序(尤其是读取功能)中遇到问题,我正在从文本文件中读取数据并根据数据构建堆栈。我在函数中有注释,因此您可以查看我的思考过程。发生了什么事,当我使用显示功能时,程序只是无限循环地重复最后一个条目。感谢您对我的代码提出的任何建议,谢谢。
#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;
}
最佳答案
您需要修改您的读取功能,如下所示:
无效的读取(结构ProduceItem **顶部)
{
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;主要。
一切正常。
关于c - 链表的堆栈实现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31443853/