用連結串列實現一個簡單的學生操作管理系統C語言版
阿新 • • 發佈:2019-02-04
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define format "%d\n%s\n%f\n%f\n%f\n"
#define len sizeof(stu)
typedef struct student{
long num;
float score;
struct student *next;
}stu;
int n;
int Tips()
{
int p;
printf("<----------請選擇需要的功能---------->\n");
printf("<----------1.輸入一個新的連結串列-------->\n");
printf("<----------2.刪除一個新的紀錄-------->\n");
printf("<----------3.插入一個新的記錄-------->\n");
printf("<----------4.顯示全部學生成績-------->\n");
printf("<----------0.退出------------------->\n" );
scanf("%d",&p);
return p;
}
stu * creat()
{
stu *p1,*p2,*head;int i,t,k=1;
n=0;
printf("輸入學生資訊的個數\n");
scanf("%d",&t);
head=0;
for(i=0;i<t;i++)
{
p1=(stu *)malloc(len);
printf("輸入第%d個學生的資訊\n",k++);
printf("學號:");
scanf("%ld ",&p1->num);
printf("成績:");
scanf("%f",&p1->score);
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
}
p2->next=0;
return head;
}
stu * del(stu * head,long num)
{
stu *p1,*p2;
if(head==0)
printf("表空!\n");
else
{
p1=head;
while(p1->num!=num && p1->next!=0)
{
p2=p1;p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
printf("刪除學號:%ld成功\n",num);
n=n-1;
}
else
printf("沒有%ld這個學號!\n",num);
}
return head;
}
stu *insert(stu * head, stu * stud)
{
stu *p1,*p2,*p0;
p0=stud;p1=head;
if(head==0)
{
head=p0;stud->next=0;
}
else
{
while(p1->num<p0->num && p1->next!=0)
{
p2=p1;p1=p1->next;
}
if(p1->num>=p0->num)
{
if(p1==head)
{
head=p0;
p0->next=p1;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0,p0->next=0;
}
}
n=n+1;
return head;
}
void print(stu * head)
{
stu * p;
printf("此時的%d個學生資訊是:\n",n);
p=head;
if(head!=0)
{
while(p!=0)
{
printf("學號:%-10ld成績:%f\n",p->num,p->score);
p=p->next;
}
}
}
void main()
{
stu * head=0,*stud;
int m;
long del_num;
int i=1;
while(i>0)
{
system("cls");
i=Tips();
switch(i)
{
case 1:
{ system("cls");
printf("開始輸入學生資訊:\n");
head=creat();
system("cls");
print(head);
printf("請輸入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("請輸入0返回\n");
scanf("%d",&m);
}
break;
}
case 2:
{
system("cls");
printf("輸入要刪除的學號:");
scanf("%ld",&del_num);
head=del(head,del_num);
print(head);
printf("請輸入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("請輸入0返回\n");
scanf("%d",&m);
}
break;
}
case 3:
{
system("cls");
stud=(stu *)malloc(len);
printf("輸入要增加的學生學號:");
scanf("%ld",&stud->num);
printf("輸入要增加的學生成績:");
scanf("%f",&stud->score);
head=insert(head,stud);
print(head);
printf("請輸入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("請輸入0返回\n");
scanf("%d",&m);
}
break;
}
case 4:
{
system("cls");
print(head);
printf("請輸入0返回\n");
scanf("%d",&m);
while(m!=0)
{
printf("請輸入0返回\n");
scanf("%d",&m);
}
break;
}
}
}
}