学习内容:线性表直接插入排序
#include <stdio.h>
#include "SequenceInsert.h"
#include <stdlib.h>
int SequenceInit(SqList *l)
{
if(NULL==l)
{
return FAILURE;
}
l->length=0;
l->data=(ElemType *)malloc(sizeof(ElemType));
if(NULL==l->data)
{
return FAILURE;
}
return SUCCESS;
}
int SequenceInsert(SqList *l,ElemType e)
{
int i,j,p=1;
for(i = 0; i < l->length; i++)
{
if(l->length == 0)
{
l->data[0] = e;
return SUCCESS;
}
if(e <= l->data[i])
{
l->data[i] = e;
for(j = 0;j<l->length-i-1;j++)
{
l->data[l->length-j]=l->data[l->length-j-1];
}
l->length++;
}
}
if(e > l->data[i-1])
{
l->data[i]=e;
l->length++;
return SUCCESS;
}
}
int SequenceTraverse(SqList l,void (*p)(ElemType))
{
if(p == NULL)
{
return FAILURE;
}
int i;
for(i = 0; i < l.length; i++)
{
p(l.data[i]);
}
return SUCCESS;
}
#include <stdio.h>
#include "SequenceInsert.h"
#include <time.h>
void print(ElemType e)
{
printf("%d",e);
}
int main()
{
int i;
SqList list;
int ret;
ElemType e;
srand(time(NULL));
ret=SequenceInit(&list);
if(ret==SUCCESS)
{
printf("Init SUCCESS!\n");
}
else
{
printf("Init FAILURE!\n");
}
for(i = 0; i < 5; i++)
{
ret=SequenceInsert(&list,rand()%10);
if(FAILURE == ret)
{
printf("Insert Failure!");
}
else
{
printf("Insert Success!");
}
}
printf("%d",e);
ret = SequenceTraverse(list,print);
if(ret == FAILURE)
{
printf("Traverse Failure!\n");
}
else
{
printf("Traverse Success!\n");
}
printf("%d",e);
return 0;
}
未完待续。。。。。还没改出来。。。。