Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1498590
  • 博文数量: 329
  • 博客积分: 2773
  • 博客等级: 少校
  • 技术积分: 4219
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:17
个人简介

淡定从容,宁静致远

文章分类

全部博文(329)

文章存档

2016年(4)

2015年(50)

2014年(68)

2013年(45)

2012年(162)

分类: LINUX

2012-12-18 21:55:58

#include
#include
typedef struct linkdata
{
 int ID;
 char name[10];
 char ***[5];
}Data;
typedef struct linknode
{
 Data data;
 struct linknode *next;
 
}linnode;
linnode *head;
int n;
void CreateList()
{
 n=0;
 linnode *p,*s;
 char x;
 head = malloc(sizeof(linnode));
 if(head==NULL)
 {
  printf("malloc is fail\n");
  return;
 }
 p=head;
 printf("\n\t\t请逐个输入信息,以“x”为结束标记!\n");
 printf("\n");
 while(1)
 {
  printf("******************\n");
  Data stu;
  printf("请输入学号:\n");
  scanf(" %d",&stu.ID);
  printf("请输入姓名:\n");
  scanf(" %s",stu.name);
  printf("请输入性别:\n");
  scanf(" %s",stu.***);
   s=malloc(sizeof(linnode));
   n++;
   s->data=stu;
   p->next = s;
   s->next = NULL;
   p=s;
  printf("退出请输入(x)继续输入(c):\n");
  scanf(" %c",&x);
  if(x=='x')
   break;
 }
 
}
void InsList(int i,Data studata)
{
 linnode *s,*p;
 p = head;
 int j = 0;
 while(p!=NULL&&j {
  j++;
  p=p->next;
 }
 if(p!=NULL)
 {
  s=malloc(sizeof(linnode));
  s->data=studata;
  s->next = p->next;
  p->next = s;
  n++;
 }
 else 
  printf("\n\t\t线性表为空或者插入位置超出\n");
}
void DelList(int id)
{
 linnode *p,*q;
 if(head == NULL)
 {
  printf("\n\t\t链表下溢!");
  return;
 }
 if(head->next == NULL)
 {
  printf("链表已经为空!"); 
  return;
 }
 q = head;
 p = head->next;
 while(p->next !=NULL&&p->data.ID!=id)
 {
  q=p;
  p = p->next;
 }
 if(p!=NULL)
 {
  q->next = p->next;
  free(p);
  n--;
  printf("\n\t\t学号:%d此已经被删除!",id);
 }
 else
  printf("\n\t\t抱歉,没有找到你要删除的数据!");
}
void ShowList()
{
 linnode *p=head;
 printf("\n\t\t显示线性表的所有元素:");
 if(p->next==NULL&&p==NULL)
  printf("\n\t\t链表为空!");
 else
 {
  printf("\n\n");
  while(p->next!=NULL)
  {
   printf("\t\t%-5d\t %-10s\t%-5s\n",p->next->data.ID,p->next->data.name,p->next->data.***); 
   p=p->next;   
  }  
 }
}
void SearchList(int id)
{
 linnode *p;
 int i=1;
 if(head ==NULL)
 {
  printf("\n\t\t链表下溢!");
  return;
 }
 if(head->next ==NULL)
 {
  printf("\n\t\t链表为空,没有任何节点!");
  return;
 }
 p = head->next;
 for(;p->next==NULL;i++,p = p->next)
 {
  if(p->data.ID==id)
  break;
 }
 if(p!=NULL)
 {
  printf("\n\t\t在表的第%d位置!",i);
  printf("\n\t\t%d-----%s-----%s\n",p->next->data.ID,p->next->data.name,p->next->data.***);
 }
 else
  printf("\n\t\t抱歉没有找到学号为%d的学生\n",id);
}
int main()
{
 head = NULL;
 int choice,i,j=1;
 while(j)
 {
  printf("\n\t\t      学生管理系统      "); 
  printf("\n\t\t**********************************"); 
  printf("\n\t\t**        1-------建  表        **"); 
  printf("\n\t\t**        2-------插  入        **"); 
  printf("\n\t\t**        3-------删  除        **"); 
  printf("\n\t\t**        4-------显  示        **"); 
  printf("\n\t\t**        5-------查  找        **"); 
  printf("\n\t\t**        6-------求表长        **"); 
  printf("\n\t\t**        0-------返  回        **"); 
  printf("\n\t\t**********************************"); 
  printf("\n\t\t请选择菜单号(1-6):"); 
  scanf("%d",&choice);
  getchar();
  if(choice==1)
   CreateList();
  else if(choice==2)
  {
   Data studata;
   printf("\n\t\t请输入插入的位置:");
   scanf(" %d",&i);
   printf("\n\t\t请输入插入的学生的学号:");
   scanf(" %d",&studata.ID);
   printf("\n\t\t请输入学生的姓名:");
   scanf(" %s",studata.name);
   printf("\n\t\t请输入学生的性别:");
   scanf(" %s",studata.***);
   InsList(i,studata);
  }
  else if(choice == 3)
  { 
   int x;
   printf("\n\t\t请输入要删除的学号:"); 
   scanf("%d",&x);
   DelList(x);
  }
  else if(choice ==4)
   if(head == NULL) 
   printf("请先建立线性表!");
   else
    ShowList();
   else if(choice==5)
   { 
    int x;
    printf("\n\t\t请输入要查找的学号:");
    scanf("%d",&x);
    SearchList(x);
   }
   else if(choice==6)
   {
    printf("\n\t\t线性表长度为:%d",n); 
   }
   else if(choice == 0)
    j=0;
   else
    printf("\n\t\t输入错误!请重新输入!"); 
 } 
}
阅读(1129) | 评论(0) | 转发(0) |
0

上一篇:约瑟夫

下一篇:成员变量隐藏

给主人留下些什么吧!~~