Ⅰ 在c語言程序中 ,用鏈表形式輸入和輸出學生信息,求年齡平均值
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
//#include <math.h>
struct student
{
int num;
char name[10];
int age;
struct student *next;
} Mystu;
int n;
struct student *input()
{
int i;
struct student *head=NULL,*p=NULL,*q=NULL;
for(i=0;i<n;i++)
{
//從鍵盤上輸入學生信息
q=p;//保存前一個元素的地址
p=(struct student *)malloc(sizeof(struct student));
printf("第%d個學生的信息(包括學號、名字、年齡): ",i+1);
scanf("%d %s %d",&p->num,p->name,&p->age);
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
}
return head;
}
void average(struct student *head)
{
double agev=0.0;
struct student *p=NULL;
p=head;
while(p!=NULL)
{
agev=agev+p->age; //求總年齡
p=p->next;
}
printf("平均年齡=%2.2f ",agev/n); //求出平均年齡
}
void output(struct student *head)
{
struct student *p=NULL;
p=head;
printf("No. name age ");
while(p!=NULL)
{
printf("%-6d %s %4d ",p->num,p->name,p->age);
p=p->next;
}
}
void Destroy(struct student **head)
{
struct student *tmp;
tmp = *head;
while(*head)
{
tmp = (*head)->next;
printf("destroy:%p ", *head);
free(*head);
*head=tmp;
}
}
int main()
{
struct student *head=NULL;
printf("please input student number(n): ");
scanf("%d",&n);
head=input();
printf("main:%p ", head);
average(head);
output(head);
Destroy(&head);
return 0;
}
修改了一下
voidaverage(structstudent*head)
和主函數中的調用
另外增加了動態內存的釋放,不釋放會造成內存泄露的!!
Ⅱ 鏈表——學生信息——C語言
1.LinkNode L 指針的指針 L存儲著一個地址, L指向該地址,被指向的地址中又存儲著一個地址,*L指向LinkNode。
2.在case 中定義變數時候,如果報錯,可以在定義變數的語句前面添加一個空語句。
Ⅲ C語言鏈表學生信息輸入
scanf("%c",&x);
你輸入的時候,肯定是按個y再按個回車,這樣下一輪循環時就會讀入回車字元了。
可以改成
scanf("%c%*c",&x);
Ⅳ C語言:鏈表,錄入學生信息:學號、姓名、性別、年齡、宿舍號碼、電話號碼。保存在文件里。這個哪裡錯了
新建鏈表裡面有一個很明顯的錯誤,fscanf的name參數輸出格式不應該為%c,而應該是%s
Ⅳ c語言!!!程序設計:建立一個學生信息鏈表,包括學號,姓名,成績.(實現添加,刪除,查詢,排序,平均)
代碼如下:
/*用c語言鏈表編寫一個學生信息系統程序,要求輸出學生的學號,姓名,性別,學號,姓名,成績(實現添加,刪除,查詢,排序,平均)*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int n=5;
/*
* nodeEntry : 節點數據類型
* nodeADT : 節點結構
* linkADT : 鏈表結構
*/
typedef struct Student
{
int num;
char name[30];
char sex;
float score1;//語文
float score2;//數學
float score3;//英語
//struct Student *next;
}Student;
typedef struct linkCDT {
nodeADT head;
}*linkADT;
/*
* InitLink : 初始化鏈表
* CreateNode : 創建節點
* AppendLink : 添加數據
*/
nodeADT CreateNode(Student entry) {
nodeADT p=(nodeADT)malloc(sizeof*p);
p->entry=entry,p->next=0;
return p;
}
/*
SortLink : 排序鏈表
//按學號排序
void SortLinkID(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.num>=p->entry.num)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
//按英語成績排序
void SortLinkEnglish(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.score3>=p->entry.score3)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的字典序進行排序
void SortLinkName(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.name[0]>=p->entry.name[0])
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的長度進行排序
void SortLinkNameLength(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (strlen(pHead->entry.name)>=strlen(p->entry.name))
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
循環鏈表是與單鏈表一樣
是一種鏈式的存儲結構,所不同的是,循環鏈表的最後一個結點的指針是指向該循環鏈表的第一個結點或者表頭結點,從而構成一個環形的鏈。
循環鏈表的運算與單鏈表的運算基本一致。所不同的有以下幾點:
1、在建立一個循環鏈表時,必須使其最後一個結點的指針指向表頭結點,而不是象單鏈表那樣置為NULL。此種情況還使用於在最後一個結點後插入一個新的結點。
2、在判斷是否到表尾時,是判斷該結點鏈域的值是否是表頭結點,當鏈域值等於表頭指針時,說明已到表尾。而非象單鏈表那樣判斷鏈域值是否為NULL。
以上內容參考:網路-鏈表
Ⅵ 用C語言編寫一個程序:用鏈表輸入很多和學生的信息,要求實現增、刪和查等功能
僅僅給你實現一個最基本的鏈表和功能,許多細節你自己添加,比如沒有判斷有沒有重復的學生信息輸入,如id重復。
還有刪除是依靠姓名刪除的,因為你的題目含糊不清,很多學生的信息具體是什麼你也沒有給出,只能給你個最基本的studentid 如果還有學科成績什麼的 自己在結構體中添加吧
後面你自己改了 最基本的給你寫好
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<malloc.h>
typedef struct student student;
struct student{
int sid;//student id
char sname[30];
struct student *next;
};
//鏈表
student *linklist;
int len;
void init_linklist()
{
linklist=NULL;
len=0;
}
bool addstudent(int studentid,char *name)
{
student *p;
p=(student*)malloc(sizeof(student));
strcpy(p->sname,name);
p->sid=studentid;
len++;
p->next=linklist;
linklist=p;
return 1;
}
bool removestudent(char *name)
{
student *p=linklist;
student *tmp=p;
while(p!=NULL&&strcmp(p->sname,name)!=0)
{
tmp=p;
p=p->next;
}
if(p==NULL)//no find
return 0;
if(tmp==p)//說明是表頭
{
linklist=p->next;
free(p);
len--;
return 1;
}
//非表頭
tmp->next=p->next;
free(p);
len--;
return 1;
}
student* findstudent(char *name)
{
student *p=linklist;
while(p!=NULL&&strcmp(p->sname,name)!=0)
p=p->next;
if(p==NULL)
return 0;
else
return p;
}
void printlist()
{
student *p=linklist;
while(p!=NULL)
{
printf("name:%s studentID:%d\n",p->sname,p->sid);
p=p->next;
}
}
int main(void)
{
init_linklist();
addstudent(111,"limin");
addstudent(222,"xiaoc");
printlist();
removestudent("xiaoc");
printlist();
char na[30];
int si;
printf("input the student you want to add:\n");
scanf("%s %d",na,&si);
addstudent(si,na);
printlist();
return 0;
}
Ⅶ c語言用鏈表添加學生信息
#include<stdio.h>
#include<string.h>
#include"stdlib.h"
#defineLENsizeof(structstu)
structstu
{
longnum;
charname[20];
charsex;
intage;
charaddr[30];
structstu*next;
};
intmain()
{
intn;
structstu*head;
structstu*p1,*p2;
head=(structstu*)malloc(LEN);
head->next=NULL;
p1=head;
p2=(structstu*)malloc(LEN);
p2->next=NULL;
printf("學號 姓名 性別 年齡 住址 ");
scanf("%ld, %s, %c, %d, %s",&p2->num,&p2->name,&p2->sex,&p2->age,&p2->addr);
while(p2->num!=0)
{
p1->next=p2;
p1=p2;
fflush(stdin);
p2=(structstu*)malloc(LEN);
printf("學號 姓名 性別 年齡 住址 ");
scanf("%ld,%s,%c,%d,%s",&p2->num,&p2->name,&p2->sex,&p2->age,&p2->addr);
}
}
純手打,希望採納。
Ⅷ 要求用C語言並結合數據結構鏈表編寫一個學生信息管理系統
//xieai999
#include <stdio.h>
typedef struct study
{
int a;
char b[10];
int c[5][1];
int total;
double pg;
}LT;
typedef struct e
{
LT *e[10];
int top;
}LY;
void creat(LY **s)
{
(*s)=(LY *)malloc(sizeof(LY));
(*s)->top=-1;
}
void pop(LT **s)
{
(*s)=(LT *)malloc(sizeof(LT));
printf("學號:");
int s1;
scanf("%d",&s1);
(*s)->a=s1;
printf("姓名:");
char s2[10];
scanf("%s",&(*s)->b);
int i=0,m=0;
for(i;i<5;i++)
{
printf("科%d:",i+1);
int x;
scanf("%d",&x);
(*s)->c[i][0]=x;
m=m+x;
}
(*s)->total=m;
(*s)->pg=(*s)->total/5.0;
}
void print(LT *s)
{
printf("學號:%d ",s->a);
printf("姓名: %s ",s->b);
int i=0;
for(i;i<5;i++)
printf("科%d:%d ",i+1,s->c[i][0]);
printf("\n總分: %d",s->total);
printf("平均分: %.01f\n",s->pg);
}
void IN(LY *s,LT *a)
{
pop(&a);
s->top++;
s->e[s->top]=a;
}
void Delete(LY *s,int a)
{
int i=0;
for(i;i<=s->top;i++)
{
if(s->e[i]->a==a)
{
LT *x;
if(i!=s->top)
{
x=s->e[i];
s->e[i]=s->e[s->top];
s->e[s->top]=x;
}
s->top--;
break;
}
}
}
void paishu(LY *s)
{
printf("1,按學好升序\n2,按總分降序\n");
int x1;
int i,j;
scanf("%d",&x1);
switch(x1)
{
case 1:
for(i=0;i<=s->top;i++)
{
for(j=0;j<s->top-i;j++)
{
LT *x;
if(s->e[j]->a<s->e[j+1]->a)
{
x=s->e[j];
s->e[j]=s->e[j+1];
s->e[j+1]=x;
}
}
}
break;
case 2:
for(i=0;i<=s->top;i++)
{
for(j=0;j<s->top-i;j++)
{
LT *x;
if(s->e[j]->total>s->e[j+1]->total)
{
x=s->e[j];
s->e[j]=s->e[j+1];
s->e[j+1]=x;
}
}
}
break;
}
}
main()
{
int k=0,j=0;
LY *A;
creat(&A);
LT *r[100]={NULL};
for(k; ;k++)
{
printf("1,新建學生數據\n2,輸出學生數據\n3,增加學生數據\n4,刪除學生數據\n5,排序\n6,查詢\n7,按其他數字鍵退出\n");
int x,i=0,n,m=0;
scanf("%d",&x);
switch(x)
{
case 1:
IN(A,r[j]);
j++;
break;
case 2:
for(i=A->top;i>-1;i--)
print(A->e[A->top]);
break;
case 3:
IN(A,r[j]);
j++;
break;
case 4:
scanf("%d",&n);
Delete(A,n);
break;
case 5:
paishu(A);
break;
case 6:
printf("暫時無此功能\n");
break;
default:
m=1;
break;
}
if(m==1)
break;
}
return 0;
}
第六項功能你自己加吧 我有事情要做了
Ⅸ 如何用C語言鏈表實現學生信息管理系統
你來我們學校里吧,我這里有,濟南市,文化東路55號,
這里同學學C語主的每人都做了
Ⅹ c語言編寫 學生信息鏈表
float average(struct node * head)
{
int i=0;
float sum=0;
for(;head!=NULL;head=head->next)
{
sum=sum+(float)(head->s);
i++;
}
return (sum/i);
}