首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

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

2013-10-24 
c语言使用链表编写一个可以实现班级学生管理系统,增加,删除,修改学生信息#include stdio.h#include std

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

#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;

}


热点排行