学生平均值的链接列表程序

时间:2015-03-14 11:04:18

标签: c linked-list

我是C编程的新手,特别是链表。有人可以帮我解决我的代码吗?当我编译程序时没有错误但是当我尝试运行程序时它的输出是错误的。我不知道存储是错误还是打印。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* self-referential structure */
struct listNode {
    char name[80];
    char subject[80]; /* each listNode contains a character */
    float unit;
    float grade;
    struct listNode *nextPtr; /* pointer to next node */
}; /* end structure listNode */

typedef struct listNode ListNode; /* synonym for struct listNode */
typedef ListNode *ListNodePtr; /* synonym for ListNode* */

/* prototypes */
void insert(ListNodePtr *sPtr, char SUB[80], float grade, float unit, char NAME[80]);
int isEmpty(ListNodePtr sPtr);
void printList(ListNodePtr currentPtr, ListNodePtr *sPtr);
void instructions(void);

int main(void)
{
    ListNodePtr startPtr = NULL; /* initially there are no nodes */
    int choice; /* user's choice */
    float grades, units;
    char subn[80], stdn[80];
    int i, j, std = 0, sub = 0;

    instructions(); /* display the menu */
    printf("? ");
    scanf("%d", &choice);

    /* loop while user does not choose 3 */
    while (choice != 3) {
        switch (choice) {
        case 1: /* input data */
            printf("Number of Students: (at least 5 students)\n");
            scanf("%d", &std);

            for (i = 0; i < std; i++) {
                printf("\nStudent name: ");
                scanf("%s", &stdn);
                printf("How many subjects?\n");
                scanf("%d", &sub);
                for (j = 0; j < sub; j++) {
                    printf("\nSub­ject: ");
                    scanf("%s", &subn);
                    printf("Units­: ");
                    scanf("%f", &units);
                    printf("Final­ Grade: ");
                    scanf("%f", &grades);
                    insert(&startPtr, subn, grades, units, stdn);
                }
            }
            break;

        case 2:
            printList(startPtr, &startPtr);
            break;

        default:
            printf("Invalid choice.\n\n");
            instructions();
            break;
        } /* end switch */

        printf("\n\n? ");
        scanf("%d", &choice);
    } /* end while */

    printf("End of run.\n");
    return 0; /* indicates successful termination */
} /* end main */

/* display program instructions to user */
void instructions(void)
{
    printf("Enter your choice:\n"
        " 1 to Input Student Data.\n"
        " 2 to Display Student Data.\n"
        " 3 to end.\n");
} /* end function instructions */

/* for series */
void insert(ListNodePtr *sPtr, char SUB[80], float grade, float unit, char NAME[80])
{
    ListNodePtr newPtr; /* pointer to new node */
    ListNodePtr previousPtr; /* pointer to previous node in list */
    ListNodePtr currentPtr; /* pointer to current node in list */

    newPtr = malloc(sizeof(ListNode)); /* create node */

    if (newPtr != NULL) { /* is space available */
        strcpy(newPtr->na­me, NAME);
        strcpy(newPtr->subject, SUB); /* place value in node */
        newPtr->grade = grade;
        newPtr->unit = unit;
        newPtr->nextPtr = NULL; /* node does not link to another node */

        previousPtr = NULL;
        currentPtr = *sPtr;

        /* insert new node at beginning of list */
        if (previousPtr == NULL) {
            newPtr->nextPtr = *sPtr;
            *sPtr = newPtr;
        } /* end if */
        else { /* insert new node between previousPtr and currentPtr */
            previousPtr->nex­tPtr = newPtr;
            newPtr->nextPtr = currentPtr;
        } /* end else */
    } /* end if */
    else {
        printf("Student data not inserted. No memory available.\n");
    } /* end else */
} /* end function insert */

int isEmpty(ListNodePtr sPtr)
{
    return sPtr == NULL;
} /* end function isEmpty */

/* Print the list */
void printList(ListNodePtr currentPtr, ListNodePtr *sPtr)
{
    float total = 0.00, total2 = 0.00;
    /* if list is empty */
    if (currentPtr == NULL) {
        printf("List is empty.\n\n");
    } /* end if */
    else {
        printf("\n");
        printf("%-20s\t%s\­t%s\n", "Subject", "Units", "Final Grade");
        /* while not the end of the list */
        while (currentPtr != NULL) {
            printf("Student­ Name: %s\n", currentPtr->name);
            printf("%-20s\t%.2f\t%.2f\n­", currentPtr->subject, ­ currentPtr->unit, currentPtr->grade);
            total += currentPtr->unit;
            total2 += currentPtr->unit*cur­rentPtr->grade;
            currentPtr = currentPtr->nextPtr;
        } /* end while */

        printf("\nWeight:­ %.2f", total2 / total);
        printf("\n///////­/////////////////////­/////////////////////­///");
    } /* end else */
} /* end function printList */

1 个答案:

答案 0 :(得分:1)

标题

您好, 我没有彻底查看代码。但是,快速浏览一下,我发现了以下基本问题

nextPtr应该是一个指针,您正在创建一个实例并复制下一个结构的内容。这效率不高。 我将声明如下

struct listNode * nextPtr; /指向下一个节点的指针* /

再次,我将更改以下的typedef ListNode ListNodePtr; typedef ListNode ListNodePtr;

要 typedef ListNode * ListNodePtr;

尝试用此重构代码,看看它是如何进行的。如果没有帮助,我可以在晚上详细了解。