|
发表于 2011-6-18 10:52:31
|
显示全部楼层
- #include<stdio.h>
- #include<stdlib.h>
- #define ERROR 0
- #define OK 1
- /*************************************/
- /*********定义学生信息的结构******/
- /*************************************/
- struct STU
- {
- char name[20]; //姓名
- char stuno[10]; //学号
- int age; //年龄
- int score; //分数
- }stu[50];
- /*************************************/
- /*******定义顺序存储的线性表******/
- /*************************************/
- struct LIST
- {
- struct STU stu[50]; //学生信息
- int length; //学生的个数
- }L;
- /*************************************/
- /*********输出所有学生的信息*******/
- /*************************************/
- int printlist(struct LIST L)
- {
- int i;
- printf("name stuno age score\n");
- for(i=0;i<L.length;i++)
- printf("%s %s\t%d\t%d\n",L.stu[i].name,L.stu[i].stuno,L.stu[i].age,L.stu[i].score);
- printf("\n");
- }
- /*********************************************/
- /*在线性表第i个位置上插入一个学生信息e*/
- /********************************************/
- int listinsert(struct LIST *L,int i,struct STU e)
- {
- struct STU *p,*q; //定义两个指向STU结构的指针变量
- if(i<1||i>L->length+1) //非法情况的处理
- return ERROR;
- q=&(L->stu[i-1]); //将线性表中第i位置上的地址赋给指针变量q
-
- /*将线性表中第i位置之后的所有学生向后移动一位*/
- for(p=&L->stu[L->length-1];p>=q;--p)
- *(p+1)=*p;
- *q=e;
- ++L->length;
- return OK;
- } /*ListInsert Before i*/
- main()
- {
- struct STU e; //定义一个学生信息e
- L.length=0; //初始化线性表
- /****************/
- /*给学生e赋值*/
- /***************/
- strcpy(e.name,"zmofun");
- strcpy(e.stuno,"100001");
- e.age=80;
- e.score=1000;
- listinsert(&L,1,e); //将学生e插入到线性表的第1个位置
- printlist(L); //输出线性表中的内容
- printf("List length now is %d.\n\n",L.length); //线性表的长度
-
- /****************/
- /*给学生e赋值*/
- /***************/
- strcpy(e.name,"bobjin");
- strcpy(e.stuno,"100002");
- e.age=80;
- e.score=1000;
- listinsert(&L,1,e); //将学生e插入到线性表的第1个位置
- printlist(L); //输出线性表中的内容
- printf("List length now is %d.\n\n",L.length); //线性表的长度
- getch();
- }
复制代码 |
|