C

作者在 2010-05-20 23:53:48 发布以下内容
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
 int id;//学号
 char name[20];
 int score;
 struct student *next;
}stu,*spt;
typedef struct
{
 spt head;//头指针
 spt tail;//尾指针
 spt current;
 spt p,q;
 int tot;
}LinkStudent;
void CreatLink(LinkStudent &l)//构造空链表
{
 l.head=l.tail=(stu*)malloc(sizeof(stu));
 l.head=l.tail=NULL;
 l.tot=0;
}
void AddInformation(LinkStudent &l)
{
    l.current=(stu*)malloc(sizeof(stu));
 printf("请输入学生姓名:");
 scanf("%s",l.current->name);
 printf("请输入学生学号:");
 scanf("%d",&l.current->id );
 printf("请输入分数:");
 scanf("%d",&l.current->score );
    if(l.head==NULL)
 {
  l.head=l.tail=l.current;
  l.head->next=l.tail;
  l.tail->next=NULL;
 }
 else
 {
         l.tail->next=l.current;
   l.current->next=NULL;
   l.tail=l.current;
 }
 l.tot++;
 printf("学生信息添加成功!!!\n");
 
}
void InsertInformation(LinkStudent &l)
{
 int location;
 int count=1;
 l.current=(stu*)malloc(sizeof(stu));
 l.p=(stu*)malloc(sizeof(stu));
 printf("输入要插入的位置号:");
 scanf("%d",&location);
 if(location>l.tot||location<1)
  printf("错误\n");
 else
 {
  if(location==l.tot )
 {
       l.current=l.head;
  while(count++<l.tot-1)
       l.current=l.current->next;
    l.p->next=l.tail;
    l.current->next=l.p;
    l.current=l.p;
    printf("插入成功\n");
 }
 else if(location==1)
 {
        l.current->next=l.head;
  l.head=l.current;
  printf("插入成功\n");
 }
 else if(location>1&&location<l.tot)
 {
  l.current=l.head;
    while(count++<=location-2)
          l.current=l.current->next;
         l.p->next=l.current->next;
   l.current->next=l.p;
   l.current=l.p;
   printf("插入成功\n");
 }
 printf("输入下列信息:");
 printf("姓名:");
 scanf("%s",l.current->name);
 printf("学号:");
 scanf("%d",&l.current->id);
 printf("分数:");
 scanf("%d",&l.current->score);
 l.tot++;
 }
}
void SearchInformation(LinkStudent &l)
{
 if(l.tot>0)
 {
 int findnumber,count=0;
 int flag;
 l.p=l.head;
 printf("输入要查找的学生学号:");
 scanf("%d",&findnumber);
    while(count++<l.tot)
 {
  flag=0;
  if(findnumber==l.p->id)
  {
   flag=1;
   break;
  }
  else
     l.p=l.p->next;
 }
 if(flag==1)
 {
            printf("学生信息找到!\n");
   printf("姓名   学号  分数\n");
   printf("%s%8d%8d\n",l.p->name,l.p->id,l.p->score );
 }
 else
       printf("无输入学生学号的信息\n");
 }
 else
     printf("没有任何信息\n");
}
void DisplayInformation(LinkStudent &l)
{
 if(l.tot>0)
 {
  int count=0;
 l.p=l.head;
  printf("姓名   学号  分数\n");
 while(count++<l.tot)
 {
       printf("%s%8d%8d\n",l.p->name,l.p->id,l.p->score );
  l.p=l.p->next;
 }
 }
 else
  printf("没有任何信息\n");
}
void DeleteInformation(LinkStudent &l)
{
 int findid, count=0;
 int flag;
 int selection;
 l.p=l.head;
 l.q=l.p;//记录删除节点的前一个节点
 if(l.tot>0)
 {
  printf("输入要删除的学生学号:");
        scanf("%d",&findid);
  while(count++<l.tot)
 {
  flag=0;
  if(findid==l.p->id)
  {
   flag=1;
   break;
  }
  else
  {
   l.q=l.p;//记录删除节点的前一个节点
   l.p=l.p->next;
  }
 }
 if(flag==1)
 {
      printf("学生信息找到!\n");
   printf("姓名   学号  分数\n");
   printf("%s%8d%8d\n",l.p->name,l.p->id,l.p->score );
   printf("确认删除吗?1删除,2退出\n");
   scanf("%d",&selection);
   if(selection==1)
   {
              if(l.p==l.tail )
     {
      l.q->next=NULL;
      l.tail=l.q;
      free(l.p);
     }
     else if(l.p==l.head)
     {
      l.q=l.p;
      l.p=l.p->next;
      l.head=l.p;
      free(l.q);
     }
     else
     {
    l.q->next=l.p->next;
     free(l.p);
     }
     l.tot--;
   }
   else
    printf("自动退出\n");
 }
 else
  printf("无输入学生学号信息\n");
 }
 else
  printf("没有任何信息\n");
}
void main()
{
 int selection;
 LinkStudent l;
 CreatLink(l);
 printf("学生管理系统\n");
 printf("选择项目\n1)新增信息\n2)插入新信息\n3)删除信息\n4)显示信息\n5)查询信息\n6)退出\n");
 while(scanf("%d",&selection)&&selection!=6)
 {
  switch(selection)
  {
  case 1:AddInformation(l);break;
  case 2:InsertInformation(l);break;
  case 3:DeleteInformation(l);break;
  case 4:DisplayInformation(l);break;
  case 5:SearchInformation(l);break;
 }
 printf("学生管理系统\n");
 printf("选择项目\n1)新增信息\n2)插入新信息\n3)删除信息\n4)显示信息\n5)查询信息\n6)退出\n");
 }
}
默认分类 | 阅读 334 次
文章评论,共0条
游客请输入验证码
文章分类
文章归档
最新评论