| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4498 人关注过本帖, 1 人收藏
标题:学生管理系统链表的实现
只看楼主 加入收藏
浩凡儿
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:101
专家分:394
注 册:2010-10-30
结帖率:100%
收藏(1)
已结贴  问题点数:10 回复次数:5 
学生管理系统链表的实现
#include<stdio.h>
#include<string.h>
#include<malloc.h>
 typedef struct
 {
 char name[20];
 char key[15];
 char Class[10];
 int age;

 }DATA;
 typedef struct node
 {
 
 DATA data;
 struct node *next;
 }ChainListType;

 ChainListType *ChainListAddFirst(ChainListType *head,DATA data)
 {
 ChainListType *node;
if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
{
printf("为保存数据申请内存失败! \n");
return NULL;
}
node->data=data;
node->next=head;
head=node;
return head;
 }

 ChainListType *ChainListFind(ChainListType *head,char *findkey)
 {
 ChainListType *h;
 h=head;
 while(h)
 {
 if(strcmp(h->data.name,findkey)==0)
     return h;
 h=h->next;
 }
 return NULL;
 }


 int ChainListDelete(ChainListType *head,char *key)
 {
   ChainListType *node,*h;
   node=h=head;
   while(h)
   {
       if(strcmp(h->data.name,key)==0)
       {
       node->next=h->next;
       free(h);
      return 1;
       }else {
       node=h;
       h=h->next;
       }
   }
 return 0;
 }

 //显示所有学生信息。
  void ChainListAll(ChainListType *head)
  {
      int i=1;
  ChainListType *h=head;
  DATA data;
  while(h)
  {
  data=h->data;
  printf("第%d个同学的信息为:姓名:%s学号:%s班级:%s年龄:%d\n",i++,data.name,data.key,data.Class,data.age);
  h=h->next;
  }

  }
 

  //添加学生的信息.
   
   ChainListType *Input(ChainListType *head)
   {
       DATA data ;
   printf ("请输入添加学生的信息:");
   printf("请输入姓名:\t");
   scanf("%s",data.name);
   printf("请输入学号: \t");
   scanf("%S",data.key);
   printf("请输入班级: \t");
   scanf("%S",data.Class);
   printf("请输入年龄:\t");
   scanf("%d",&data.age);
   
   return ChainListAddFirst(head,data);
   }


   //查找学生的信息.
     
    void Find(ChainListType *head)
    {
    DATA data;
    char name[20];
    ChainListType *h;
    printf("请输入要查找的学生的姓名:\n");
        scanf("%s",name);
         h=ChainListFind(head,name) ;
             if(h)
             {
             data=h->data;
             printf("要查找的学生的信息为:姓名:%s\t学号:%s\t班级:%s\t年龄:%d\t",data.name,data.key,data.Class,data.age);
             }
    }

    //删除学生信息。


    void Delete(ChainListType *head)
    {
    ChainListType *h=head;
    char name[20];
    printf("请输入要删除的学生的姓名:\n");
    scanf("%s",name);
    ChainListDelete(head,name);

   
    }

    int main()
    {
     ChainListType *head=NULL;
     char select;
     do
     {
     printf("\n*******************************\n");
     printf("a.添加学生的信息。\n") ;
     printf("b.显示学生的所有信息。\n");
     printf("c.查找学生的信息。\n");
     printf("d.删除学生的信息。\n");
     printf("*********************************\n");

     select=getchar();
     switch(select)
     {
     case'a':
              head=Input(head);
         break;
     case'b':
              ChainListAll(head);
         break;
     case'c':
         Find(head);
         break;
     case'd':
              Delete(head);
         break;
     case'e':
              break;
     
     }
     
     }while (select!='e');
     return 0;
    }
搜索更多相关主题的帖子: 链表 系统 学生 管理 
2010-11-03 13:06
浩凡儿
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:101
专家分:394
注 册:2010-10-30
收藏
得分:0 
有点小问题请给看下
2010-11-03 13:06
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:10 
#include <stdio.h>
#include <string.h>
#include <malloc.h>

typedef struct
{
    char name[20];
    char key[15];
    char Class[10];
    int age;

}DATA;

typedef struct node
{
    DATA data;
    struct node *next;
}ChainListType;

ChainListType *ChainListAddFirst( ChainListType *head, DATA data )
{
    ChainListType *node;
    node = (ChainListType *) malloc (sizeof(ChainListType));
    if( !node )
    {
        printf("为保存数据申请内存失败 \n");
        return NULL;
    }
    node->data.age = data.age;
    strcpy(node->data.Class, data.Class);
    strcpy(node->data.key, data.key);
    strcpy(node->data.name, data.name);
    //判断是否是第一个结点
    if( head == NULL )
    {
        head = node;
        head->next = NULL;
    }
    else
    {
        node->next = head->next;
        head->next = node;
    }
    return head;
}

ChainListType *ChainListFind( ChainListType *head, char *findkey )
{
    ChainListType *h;
    h = head;
    while( h )
    {
        if( strcmp(h->data.name,findkey) == 0 )
             return h;
        h = h->next;
    }
    return NULL;
}


int ChainListDelete( ChainListType *head, char *key )
{
    ChainListType *node, *h, *temp;
    int flag = 1;
    node = h = head;
    while( h )   
    {
        if( strcmp(h->data.name, key) == 0 )   
        {
            if(flag == 1)
            {
                temp = h->next;
                free(h);
                h = temp;
                return 1;
            }
            else
            {
                flag = 0;
                temp = h->next;
                free(h);
                node->next = temp;
                h = temp;
                return 1;
            }
        }
        else
        {
            if( flag == 1)
            {
                h = h->next;
                flag = 0;
            }
            else
            {
                node = h;
                h = h->next;
            }
        }
    }
    printf("\t没有找到姓名为%s的学生信息!\n", key);
    return 0;
}

//显示所有学生信息。
void ChainListAll(ChainListType *head)
{
    int i=1;
    ChainListType *h=head;
    DATA data;
    while(h)
    {
        data=h->data;
        printf("第%d个同学的信息为:姓名:%s学号:%s班级:%s年龄:%d\n",i++,data.name,data.key,data.Class,data.age);
        h=h->next;
    }

}


  //添加学生的信息.
ChainListType *Input(ChainListType *head)
{
    DATA data ;
    printf ("请输入添加学生的信息:");
    printf("请输入姓名:\t");
    scanf("%s",data.name);
    printf("请输入学号: \t");
    scanf("%s",data.key);
    printf("请输入班级: \t");
    scanf("%s",data.Class);
    printf("请输入年龄:\t");
    scanf("%d",&data.age);
   
    return ChainListAddFirst(head,data);
}


   //查找学生的信息.
void Find(ChainListType *head)
{
    DATA data;
    char name[20];
    ChainListType *h;
    printf("请输入要查找的学生的姓名:");
    scanf("%s",name);
    h = ChainListFind(head, name);
    if( h )
    {
        data = h->data;
        printf("要查找的学生的信息为:姓名:%s\t学号:%s\t班级:%s\t年龄:%d\t",data.name,data.key,data.Class,data.age);
    }
    else
    {
        printf("\t没有找到姓名为%s的学生信息!\n", name);
    }
}

    //删除学生信息。
void Delete(ChainListType *head)
{
    ChainListType *h = head;
    char name[20];
    printf("请输入要删除的学生的姓名:");
    scanf("%s",name);
    ChainListDelete(head, name);
}

int main()
{
    ChainListType *head = NULL;
    char select;
    do
    {
        printf("\n*******************************\n");
        printf("a.添加学生的信息。\n") ;
        printf("b.显示学生的所有信息。\n");
        printf("c.查找学生的信息。\n");
        printf("d.删除学生的信息。\n");
        printf("*********************************\n");

        select = getchar();
        //
        switch( select )
        {
        case'a':
              head = Input(head);
            break;
        case'b':
              ChainListAll(head);
            break;
        case'c':
            Find(head);
            break;
        case'd':
              Delete(head);
            break;
        case'e':
              break;
        }
        getchar();
     }while (select!='e');

     return 0;
}
2010-11-03 21:40
思思CC
Rank: 1
来 自:湖北武汉
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-11-9
收藏
得分:0 
很好啊

呵呵···我是执子之手
2010-11-09 19:57
o_O燊
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-11-23
收藏
得分:0 
我也学习一下
2011-05-07 13:04
leech58
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:18
专家分:58
注 册:2011-8-29
收藏
得分:0 
呵呵,虽然没有运行  但是看到楼主代码有一个地方有点小小的错误  %S 应该是小写的%s
  //添加学生的信息.
   
   ChainListType *Input(ChainListType *head)
   {
       DATA data ;
   printf ("请输入添加学生的信息:");
   printf("请输入姓名:\t");
   scanf("%s",data.name);
   printf("请输入学号: \t");
   scanf("%S",data.key);   <---------这里
   printf("请输入班级: \t");
   scanf("%S",data.Class);     <---------这里
   printf("请输入年龄:\t");
   scanf("%d",&data.age);
   
   return ChainListAddFirst(head,data);
   }


诚心交流!
2011-08-29 23:31
快速回复:学生管理系统链表的实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019510 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved