【问题标题】:Linked list program for student average学生平均水平的链表程序
【发布时间】:2015-03-14 11:04:17
【问题描述】:

我是 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 */

【问题讨论】:

  • 发布代码最简单的方法是每行缩进四个字符,这在你的IDE中应该很容易做到,然后不要在每行之间包含空白行。但是,这个问题不太适合 StackOverflow:您要求我们找出您的程序为什么不工作而没有告诉我们预期的输入和输出,为什么它不工作。即便如此,“调试我的程序”在这里仍然不够具体。
  • 我投票结束这个问题作为题外话,因为太没有格式化;没读过。
  • 对不起,我是 stackoverflow 的新手。下次会做得更好。总之谢谢

标签: c linked-list


【解决方案1】:

标题

嗨, 我没有仔细查看代码。但是,快速浏览一下,我发现了以下基本问题

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

结构列表节点 *nextPtr; / 指向下一个节点的指针 */

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

到 typedef ListNode *ListNodePtr;

试着用这个来重构你的代码,看看它是如何进行的。如果没有帮助,我晚上可以详细看看。

【讨论】:

  • 非常感谢。我没有检查stackoverflow,因为我已经解决了它,但发现你回答了解决它的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多