网站建设辅助,设计网站公司 露 联湖南岚鸿,电子商务网站主要面向,西宁百姓网// LinkTable.cpp : Defines the entry point for the console application.
// 程序运行#xff0c;用户输入学生数#xff0c;根据用户输入学生数#xff0c;动态创建链表#xff0c;提示输入学生
// 姓名、成绩#xff0c;最后输出所有学生数据以及平均成绩
#include 用户输入学生数根据用户输入学生数动态创建链表提示输入学生
// 姓名、成绩最后输出所有学生数据以及平均成绩
#include stdafx.h
#include
#include
struct Student{char name[30];int score;struct Student *p_next;
};
int main(int argc, char* argv[])
{int i,n;char c,t;struct Student* pHead NULL;struct Student* pLast NULL; i 0;while(1){printf(输入学生信息请按 y 其它键退出...\n); c getchar(); if(!(c y || c Y)) // 输入不是 y 或 Y 退出break;struct Student* pCurr (struct Student*)malloc(sizeof(struct Student));printf(请输入学生 %d 姓名:,i1);scanf(%s,pCurr-name);printf(请输入学生%d成绩:,i1);scanf(%d,pCurr-score);pCurr-p_next NULL; // 很重要用于判断是否为最后一个节点 if(i 0){pHead pCurr;pLast pCurr;}else{pLast-p_next pCurr;pLast pCurr;}i ;// 清空键盘缓冲区while((t getchar()) ! \n t ! EOF);}// 链表遍历访问每个节点一次pLast pHead;i 1;n 0;int sum 0;while(pLast){n;printf(学生 %d 姓名%s 成绩%d\n,i,pLast-name,pLast-score);sum pLast-score;pLast pLast-p_next; // 指针指向下一个节点 i;}double avg sum/(double)n;printf(平均成绩 %lf\n,avg);// 释放内存 while(pHead){pLast pHead-p_next;free(pHead);pHead pLast;}return 0;
}