您的位置:首页 > 编程语言 > C语言/C++

c语言使用链表编写一个可以实现班级学生管理系统,增加,删除,修改学生信息

2013-10-23 11:55 1631 查看
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct tagPerson
{

char name[20];

char sex[20];

long tel[20];

struct tagPerson *next;
}Person;
Person * create(int n)
{
Person *head=NULL,*pre=
NULL;

for(int i=0;i<n;i++ )
{
Person *temp=malloc(sizeof(Person));
printf("输入姓名:");
scanf("%s",temp->name);
printf("输入性别:");
scanf("%s",temp->sex);
printf("输入电话:");
scanf("%ld",temp->tel);

if (head==NULL)
{
head=temp;
}

if (pre!=NULL)
{
pre->next=temp;
}
pre=temp;
}
pre->next=NULL;

return head;
}
void show(Person * head)
{
Person *p=head;

while (p)
{
printf("\n%s\t%s\t%ld\n",p->name,p->sex,p->tel);
p=p->next;
}
}
void destroy(Person *head)
{
Person *a=head,*b;

while (a)
{
b=a->next;
free(a);
a=b;
}
}
Person *delete(Person *head ,long *tel)
{
Person *p=head;
Person *before=NULL,*me=NULL,*t=NULL;

while (p)
{

if (strcmp(p->tel, tel)==0)
{
before=t;
me=p;

break;
}t=p;
p=p->next;

}

if (p)
{

if (before==NULL&&me!=NULL)
{
head=me->next;
free(me);
}

else if(before!=NULL&&me->next!=NULL)
{
before->next=me->next;
free(me);
}

else if(before!=NULL&&me->next==NULL)
{
before->next=NULL;
free(me);
}
}return head;
}
int main(int argc,
const char * argv[])
{

int n;
printf("请输入人数");
scanf("%d",&n);
Person *head=create(n);
show(head);
printf("修改后的数据:");

printf("删除后的数据:");
delete(head,
"1234");
show(head);
destroy(head);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐