真心求教,顺序表中插入和删除的程序,小弟实在不会啊!
用如下程序建立一个顺序表,用来存储五位同学的信息!接着删除第二个人的信息,并且在第四个学生前面插入一个学生的信息,具体如下;#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -2;
#define OK 1;
typedef int status;
typedef int Elemtype;
#define LIST_INIT_SIZE 1000
#define Listcrement 100
typedef struct{
Elemtype *elem;
int length;
int listsize;
}sqlist;
//顺序表动态分配存储结构
struct date
{
int year;
int month;
int day;
}bir[5];
//定义时间结构组
struct student
{
long int num;
char name[10];
struct date birthday;
}stu[5];
//定义学生信息结构组
status initList_sq(sqlist &L)
{
L.elem=(Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
if(!L.elem)
exit(-2);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
//初始化线性表
void main()
{
printf("Please write down the information:\n");
int i, j;
for(i=0;i<5;i++)
{scanf("%d,%d,%d,%d,%s",&stu[i].num,&stu[i].birthday.year,&stu[i].birthday.month,&stu[i].birthday.day,&stu[i].name);}
for(j=0;j<i;j++)
{printf("Name:%5s Num:%5d Birthday:%d年%d月%d日\n",stu[j].name,stu[j].num,stu[j].birthday.year,stu[j].birthday.month,stu[j].birthday.day);}
}
这是一开始的准备工作,就是说先建立个顺序表来存储!接下来的步骤我就实在不懂……要删除和插入,我只能写出如下:
status ListDelete_sq(sqlist &L, int k, Elemtype &e)
{
if((k<1)||(k>L.length))
return ERROR;
p=&(L.elem[k-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1) = *p;
--L.length;
return OK;
}//删除第二个人的信息
具体的主程序我实在不会,诚心求指导!!!接下来插入的步骤也是一样,只能写出:
status ListInsert_sq(sqList &L,int m, Elemtype a);
{
if(m<1||m>L.length+1) return ERROR;
if(L.length>=L.listsize)
{
newbase=(Elemtype *)realloc(L.elem,(L.listsize+Listcrement)*sizeof(Elemtype));
if(!newbase) exit(OVERFLOW);
L.elem=newbase;
L.listsize+=Listcrement;
}
q=&(L.elem[m-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=a;
++L.length;
return OK;
}
真心求教高人指导,删除和插入的步骤如何完成!由于是新人,实在没多少分,凑合着给了!