简体   繁体   中英

C LinkedList printing

I am writing a program that reads from a txt file and adds the contents to a linked list. Then, with user interface in command prompt, I should be able to view the list, add/delete from the list, and search for an item in the list using the part number to see its full detail.

Currently I am encountering two problems:

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.

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).

This is my current code:

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

And this is the text file I read from:

#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.

It's because the format string you passed to scanf() on line 241 didn't specify to read anything. That is, you have this:

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

when it should be this:

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).

Well, you can print out the union like this:

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

Note of course that one of these two print lines will display incorrect/irrelevant information for any particular node, since a union can only be one of its constituent members at a time -- for example, if the particular node has been set to hold a nodeCounter (integer) value, then you shouldn't really be accessing the dataitem member value in the second line, or if the node has been set to hold a data item (pointer) value, then you shouldn't be accessing the nodeCounter member variable. So you should have some way to know which of those two union-members is the valid one and which is not. The typical way is to add a separate member to the struct (outside of the union) that you set to indicate what the union is set to -- that's known as a tagged union. But if you don't do it that way, you'll have to come up with some other mechanism instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM