#include"stdio.h"
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
struct student
{
long num;
char name[20];
char sex;
int age;
struct student *next;
};void print(void);
void display(struct student *head)
{
struct student *p;
if(head==NULL)
printf("這是一個空鏈表!/n");
else
{
printf("學 號 姓 名 性 別 年 齡/n");
p=head;
while(p!=NULL)
{
printf("%-4ld %-8s %-1c %-2d/n",p->num,p->name,p->sex,p->age);
p=p->next;
}
}
}
struct student *creat()
{
struct student *head,*p,*q=NULL;
int n,m;
printf("請輸入學生人數:");
scanf("%d",&m);
head=NULL;
n=1;
while(n<=m)
{
p=(struct student*)malloc(sizeof(struct student));
printf("/n請輸入學號:");
scanf("%ld",&p->num);
getchar();
printf("請輸入姓名:");
scanf("%s",p->name);
getchar();
printf("請輸入性別:");
scanf("%c",&p->sex);
getchar();
printf("請輸入年齡:");
scanf("%d",&p->age);
system("cls");print();
if(n==1)
head=p;
else
q->next=p;
q=p;
n++;
}
q->next=NULL;
display(head);
return head;
}
void found(struct student *head)
{
struct student *p;int n;
printf("請輸入學號:");
scanf("%d",&n);system("cls");print();
p=head;
if(head==NULL)
{printf("沒有找到,/n這是一個空鏈表!/n"); }
else
{while(p->num!=n&&p->next!=NULL)
{
p=p->next;
}
if(p->num==n)
printf("%-4ld %-8s %-1c %-2d/n",p->num,p->name,p->sex,p->age);
else
printf("沒有找到!");
}
}
struct student *del(struct student *head)
{
struct student *p,*q;int n;
printf("輸入學生的學號:");
scanf("%d",&n);system("cls");print();
if(head==NULL)
{printf("這是一個空鏈表!");return head;}
p=head;
while(p->num!=n&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->num==n)
{
if(head==p)
head=p->next;
else
q->next=p->next;
free(p);
}
else
printf("沒有找到!/n");
return head;
}
struct student *input(void)
{struct student *p;
p=(struct student *)malloc(sizeof(struct student ));
printf("/n請輸入學號:");
scanf("%ld",&p->num);
getchar();
printf("請輸入姓名:");
scanf("%s",p->name);
getchar();
printf("請輸入性別:");
scanf("%c",&p->sex);
getchar();
printf("請輸入年齡:");
scanf("%d",&p->age);
system("cls");print();
return p;
}
struct student *insert(struct student *head,struct student *r)
{
struct student *p,*q;
p=head;
if(head==NULL)
{
head=r;
r->next=NULL;
}
else
{while(p->num>r->num&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->num<=r->num)
{
if(head==p)
head=r;
else
q->next=r;
r->next=p;
}
else
{
p->next=r;
r->next=NULL;
}
}
return head;
}
void print(void)
{
printf("學生管理系統:/n");
printf("1、創建列表/n");
printf("2、查找/n");
printf("3、增加/n");
printf("4、刪除/n");
printf("5、顯示/n");
printf("6、退出系統/n/n/n/n");
}
int main(void)
{
struct student *r,*head=NULL;
int m;int i;
print();
for(i=0;;i++)
{
scanf("%d",&m);system("cls");print();
switch(m)
{
case 1: head=creat();break;
case 2: found(head);break;
case 3: {r=input();head=insert(head,r);}break;
case 4: head=del(head);break;
case 5: display(head);break;
case 6: exit(0);
default: printf("輸入錯誤,請再次輸入!");
} }
return 0;
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。