- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,该程序从 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/
前言:笔记是参考B站up主尚硅谷,图片、代码都是哦。在blog写笔记~(图片、代码来源尚硅谷,侵权必删!) 尚硅谷数据结构学习路线B站网站:https://www.bilibili.com/video
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我想创建一个没有全局变量的单个链表。我用 NULL 初始化了第一个元素,然后想将第一个元素 node 复制到 list_。它被复制到函数中,但副作用不起作用。在我的主函数中,该值仍然是NULL。如果我
我正在尝试使链表与此处的链表相似: linked list in C 那就是在另一个结构中有“头”,我首先称它为“头”。但是我发现做那个改变。很难向 list_item 结构添加值。我已经尝试了一些东
我正在尝试理解链表的代码。我明白他们是如何工作的。我正在查看一些与动态内存和链表有关的代码,我在此处对其进行了简化: #include #include typedef struct nod
有人可以解释下面的代码吗?我是 C 的新手,正在努力弄清楚。为什么我们最后有 queueNodeT? typedef char queueElementT; typedef struct queueN
场景如下:- 我想反转单链表的方向,换句话说,反转后所有指针现在应该指向后.. 这个算法应该需要线性时间。 我想到的解决方案是使用另一个数据结构 A Stack.. 借助它可以轻松反转单向链表,所有指
在 python 中使用链表最简单的方法是什么?在 scheme 中,链表由 '(1 2 3 4 5) 定义。 Python 的列表 [1, 2, 3, 4, 5] 和元组 (1, 2, 3, 4,
本文首发公众号:小码A梦 一般数据主要存储的形式主要有两种,一种是数组,一种是链表。数组是用来存储固定大小的同类型元素,存储在内存中是 一片连续 的空间。而链表就不同于数组。链表
虽然之前有人问过关于链表与数组的问题,但答案大多归结为我们大多数人在某个时候可能已经学到的东西: 列表擅长插入和删除 数组擅长随机访问 现在像 Bjarne Stroustrup 这样受人尊敬的人有
位置 在堆中,碎片化(每个节点的 malloc) - 在几种不同的方式(缓慢分配,缓慢访问,内存碎片)方面效率低下 在堆中,在一个大块中 - 当需要重新分配 时,数据结构获得的所有灵活性都将丢失 在堆
我完成了泛型的学习,但并不容易。不过,我确实明白了。这是我的理解。我希望您纠正我的错误并回答几个问题:)。 public class LinkedList { //class definition }
我将如何创建一个链接列表来在 OCaml 中保存我的数据?我正在尝试制作一个单链表,但是我遇到了语法问题。我只想制作一个模块来简单地从链表中获取'a,插入'a或删除'a。 有人知道吗? 最佳答案 正如
我在使用这段代码时遇到了问题,我不确定我做错了什么 #include #include #include #include typedef struct flight_struct{
我正在创建一个函数来删除给定列表的最后一个节点(作为参数输入)。该函数本身非常简单,如下所示。 function popBack(list) { var current = list.head
我正在尝试开发一种方法,该方法将在链接列表中的当前节点之前插入传递给它的节点。它有3个条件。对于此实现,不能有任何头节点(仅对列表中第一个节点的引用),并且我无法添加更多变量。 如果列表为空,则将传递
使用 scala,我已将大约 100000 个节点添加到链表中。当我使用函数 length 时,例如 mylist.length。我收到“java.lang.StackOverflowError”错误
所以我正在学习处理链表。我将如何递归地添加节点内的项目。我可以通过执行 sum = h.item +h.next.item+h.next.next.item 添加它们,但这只有在我有小的链接列表时才有
所以我一直在努力理解链表的概念(一直在看一些示例代码,我在互联网上找到了这个。现在如果我能请别人确认我是否正确掌握了一些概念。我将绘制图表,说明我认为每个代码链接的作用。 #include #inc
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是一名优秀的程序员,十分优秀!