[英]C LinkedList printing
我正在編寫一個從txt文件讀取並將內容添加到鏈接列表的程序。 然后,在命令提示符下的用戶界面下,我應該能夠查看列表,從列表中添加/刪除,以及使用零件號在列表中搜索項目以查看其詳細信息。
目前,我遇到兩個問題:
1,在主函數的開關案例1中,我在其中插入一個節點,tempNODE-> item.dataitem = getInfo(); 該行在while循環外正常工作,但是當我將其放入while循環內時,它將跳過采用新項的名稱並直接轉到部件號。 我不知道為什么要這么做。
2,在主功能的開關情況3中,我無法弄清楚如何打印搜索到的節點的內容,由於節點結構內部的並集,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,在主函數的開關案例1中,我在其中插入一個節點,tempNODE-> item.dataitem = getInfo(); 該行在while循環外正常工作,但是當我將其放入while循環內時,它將跳過采用新項的名稱並直接轉到部件號。 我不知道為什么要這么做。
這是因為您在第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,在主功能的開關情況3中,我無法弄清楚如何打印搜索到的節點的內容,由於節點結構內部的並集,DisplayNode函數無法在其上工作(我想弄清楚如何在不更改NODE結構的情況下進行打印)。
好了,您可以像這樣打印出聯合:
printf("tempNODE->item.nodeCounter=%i\n", tempNODE->item.nodeCounter);
printf("tempNODE->item.dataitem=%p\n", tempNODE->item.dataitem);
當然,請注意,這兩個打印行之一將顯示任何特定節點不正確/不相關的信息,因為一個聯合一次只能是其組成成員之一-例如,如果特定節點已設置為保留一個nodeCounter(整數)值,那么您實際上不應訪問第二行中的dataitem成員值,或者如果已將節點設置為保存數據項(指針)值,那么您不應訪問nodeCounter成員變量。 因此,您應該以某種方式知道這兩個工會成員中的哪個是有效成員,哪個不是。 典型的方法是將一個單獨的成員添加到您設置的結構中(在聯合之外),以指示該聯合設置為什么-這稱為標記聯合。 但是,如果您不這樣做,則必須提出其他一些機制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.