gpt4 book ai didi

C 链表打印

转载 作者:行者123 更新时间:2023-11-30 15:33:25 24 4
gpt4 key购买 nike

我正在编写一个程序,该程序从 txt 文件中读取并将内容添加到链接列表中。然后,通过命令提示符中的用户界面,我应该能够查看列表、从列表中添加/删除以及使用部件号搜索列表中的项目以查看其完整详细信息。

目前我遇到两个问题:

1.在主函数的 switch case 1 中,我插入一个节点,tempNODE->item.dataitem = getInfo();该行在 while 循环之外可以正常工作,但是当我将其放入 while 循环内时,它会跳过获取新项目的名称并直接转到零件号。我不明白为什么它会这样做。

2.在主函数的 switch case 3 中,我无法弄清楚如何打印搜索到的节点的内容,由于 NODE 结构内部的并集,DisplayNode 函数无法对其起作用(我想要弄清楚如何在不改变 NODE 结构的情况下打印它)。

这是我当前的代码:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

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 );
void readFromText(FILE *, struct NODE *);
struct inventory *getInfo();

int main(int argc, char* argv[])
{
//create new linked list
struct NODE *header;
header = InitList();

//open text file and read all inputs into the linked list
FILE *fp = fopen("input.txt", "r");
readFromText(fp, header);

//divider
printf("\n--------------------------------------------------\n\n");

int input = 0;
int a = 0;
struct NODE *tempNODE = (struct NODE*)malloc(sizeof NODE);
//tempNODE->item.dataitem = getInfo(); //lets user input info from cmd
//Add2List(header, tempNODE); //inserts the new node into the list

while(a == 0)
{
printf("\n\nEnter a number to select an option: \n");
printf("1 Insert item \n");
printf("2 Delete item \n");
printf("3 Look up item \n");
printf("4 Print current list of items \n");
printf("5 Exit \n");

scanf("%d", &input);
switch(input)
{
case 1:
//insert a node (Add2List function)
tempNODE->item.dataitem = getInfo(); //lets user input info from cmd
Add2List(header, tempNODE); //inserts the new node into the list

break;

case 2:
printf("Enter item number to delete \n");
int iNumber;
scanf("%d", &iNumber);
DeleteNode(header, iNumber);
printf("Item %d deleted", iNumber);
break;

case 3:
printf("Enter item number to search \n");
int searchNumber;
scanf("%d", &searchNumber);
//create temp node to take value from search
//struct NODE *tempNODE = (struct NODE*)malloc(sizeof NODE);
tempNODE = SearchList(header, searchNumber); //returns a node
//display node contents
//DisplayNode(tempNODE->item);
break;

case 4:
DisplayList(header);
break;

case 5:
a = 1; //ends the while loop
break;

default:
a = 1; //ends the while loop
}

}

//divider
printf("\n--------------------------------------------------\n\n");

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)
{
printf("Name: %s\n", stuff->invName);
printf("Part Number: %.5d\n", stuff->invPartNo);//printf(“ %.9d”, x)
printf("Quantity on hand: %d\n", stuff->invQOH);
printf("Unit Cost: %0.2f\n", stuff->invUnitCost);
printf("Price %0.2f\n\n", stuff->invPrice);
}


struct inventory * ReadData(FILE *fptr)
{
struct inventory *temp = (struct inventory *)malloc(sizeof inventory);

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

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

void readFromText(FILE *fp, struct NODE *header)
{
if( fp != NULL )
{
while(!feof(fp))
{
struct NODE *nNode = (struct NODE*)malloc(sizeof NODE);
struct inventory *newNode = (struct inventory*)malloc(sizeof inventory);

fgets(newNode->invName, 100, fp);
fscanf(fp, " %d %d %f %f ", &newNode->invPartNo,&newNode->invQOH,&newNode->invUnitCost,&newNode->invPrice);
nNode->item.dataitem = newNode;
header->item.nodeCounter++;
Add2List(header, nNode);
}
}
}

struct inventory *getInfo()
{
struct inventory *temp = (struct inventory*)malloc(sizeof inventory);

printf("Enter item name: ");
scanf("%99[^\n]", temp->invName); //scans whole line up to 99 characters or until \n

printf("Enter item part number: ");
scanf("%d", &temp->invPartNo);

printf("Enter item quantity on hand: ");
scanf("%d", &temp->invQOH);

printf("Enter item unit cost: ");
scanf("%f", &temp->invUnitCost);

printf("Enter item price: ");
scanf("%f", &temp->invPrice);

return temp;
}

这是我读取的文本文件:

#1 Flat Blade Screwdriver
12489
36
.65
1.75
#2 Flat Blade Screwdriver
12488
24
.70
1.85
#1 Phillips Screwdriver
12456
27
0.67
1.80
#2 Phillips Screwdriver
12455
17
0.81
2.00
Claw Hammer
03448
14
3.27
4.89
Tack Hammer
03442
9
3.55
5.27
Cross Cut Saw
07224
6
6.97
8.25
Rip Saw
07228
5
6.48
7.99
6" Adjustable Wrench
06526
11
3.21
4.50

最佳答案

1.In switch case 1 in the main function, where I insert a node, the tempNODE->item.dataitem = getInfo(); line works correctly outside of the while loop, but when I put it inside the while loop, it skips taking the name of the new item and goes directly to the part number. I cannot figure out why it does this.

这是因为您在第 241 行传递给 scanf() 的格式字符串没有指定读取任何内容。也就是说,你有这个:

scanf("%99[^\n]", temp->invName); //scans whole line up to 99 characters or until \n

什么时候应该是这样:

scanf("%99s[^\n]", temp->invName); //scans whole line up to 99 characters or until \n

2.In switch case 3 in the main function, I cannot figure out how to print the contents of the searched node, the DisplayNode function won't work on it because of the union inside the structure of NODE (I am want to figure out how to print this without changing the struct of NODE).

那么,你可以像这样打印出并集:

printf("tempNODE->item.nodeCounter=%i\n", tempNODE->item.nodeCounter);
printf("tempNODE->item.dataitem=%p\n", tempNODE->item.dataitem);

当然请注意,这两个打印行之一将显示任何特定节点的不正确/不相关的信息,因为 union 一次只能是其组成成员之一——例如,如果已设置特定节点要保存一个nodeCounter(整数)值,那么您不应该真正访问第二行中的dataitem成员值,或者如果该节点已设置为保存一个数据项(指针)值,那么您不应该访问nodeCounter 成员变量。因此,您应该有某种方法来知道这两个 union 成员中哪一个是有效的,哪一个不是。典型的方法是向您设置的结构(在 union 之外)添加一个单独的成员,以指示 union 的设置内容——这称为标记 union 。但如果你不这样做,你就必须想出一些其他的机制。

关于C 链表打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23717781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com