程序测试有问题 Insert函数好像不能正确的插入元素 。。。。
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
#define Extend 20
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
//this code is boring, and to implement basic operations of linear data structure by sequence list
//本代码很无聊,本代码用顺序表实现线性结构的基本操作集
typedef int ElementType;
typedef int Status;
typedef int Position;
//typedef struct Sqlist *PtrList;
typedef struct{
ElementType *data;
int length;//用于保存当前数组中元素个数
}Sqlist,*List;
Status Init_Sqlist( List L);//动态分配一个内存块
Status Destory_Sqlist( List L);
Status Clear_Sqlist(List L);
int GetLength(List L);
bool IsEmpty(List L);
//---------------------------分割线 下面是重要算法
int Get_ith(List L,ElementType *E, int i);//找到位于第i个位置的元素并返回内容
Position Find(List L,ElementType E);//找到并返回第一个表中第一个等于E的元素的位置(不是下标)从头查找
Status Insert(List L, Position i,ElementType E);//在第i个位置插入元素E
Status Delete(List L,Position i,ElementType *E);//删除第i个位置上的元素并用指针带回
//----------------主程序分割线~
int main()
{
Sqlist L;
Init_Sqlist(&L);
IsEmpty(&L);
printf("当前表长=%d\n",GetLength( &L));
printf("插入一个元素123在位置1后\n");
Insert(&L,1,123);
printf("当前表长=%d\n",GetLength( &L));
printf("插入456在位置2后\n");
Insert(&L,2,456);
printf("当前表长=%d\n",GetLength( &L));
printf("删除位于位置1的元素\n");
int E = 0;
printf("用来接收被删除的值的E此时等于%d\n",E);
Delete(&L,1,&E);
printf("用来接收被删除的值的E在调用DELETE函数后的等于%d\n",E);
printf("当前表长=%d\n",GetLength( &L));
//Destory_Sqlist(&L);
free(L.data);
return 0;
}
Status Init_Sqlist( List L)
{
L->data = (ElementType*)malloc(sizeof(ElementType)*MAXSIZE);
if(!L->data)
exit(OVERFLOW);
L->length = 0;
return OK;
}
Status Destory_Sqlsit(List L)
{
if(L->data)
{
free(L->data);
L->data = NULL;
L->length = 0;
return OK;
}
}
Status Clear_Sqlist(List L)
{
L->length = 0;
return OK;
}
int GetLength(List L)
{
return (L->length);
}
bool IsEmpty (List L)
{
if(L->length==0)
return TRUE;
return FALSE;
}
//--------------------------
int Get_ith(List L,ElementType *E, int i)
{
if(i<1||i>L->length)//注意这里是位置,而不是下标 ,不存在位置是0的元素
{
printf("Invalid Position!\n");
return ERROR;
}
*E = L->data[i-1];//这里用指针带回第i个位置的内容 这里的*E中的*是取内容
return OK;
}
Position Find(List L,ElementType E)
{
int i,len;
len = L->length;
for(i =0; i<len;i++)
{
if(L->data[i]==E)
return i+1;//同样 这里是返回的位置
}
printf("%d Not Found\n");
return ERROR;//没找到返回0
/*老师给的用while 实现的代码
i = 0 ;
while(i<L->length&&L->data[i]!=E)
i++;
if(i<L->length)
return i+1;
return ERROR;
*/
}
Status Insert(List L, Position i,ElementType E)
{
int j;
if(i<1||i>L->length+1)
{
printf("Invalid Position\n");
return FALSE;
}
if(L->length==MAXSIZE-1)
{
printf("List FULL\n");
return FALSE;
}
for(j = L->length;j>=i;j--)
L->data[j] = L->data[j-1];
L->data[i]=E;
//printf("%d\n",L->data[i-1]);
L->length++;
return TRUE;
}
Status Delete(List L,Position i,ElementType *E)
{
int j;
if(i<i||i>L->length)
{
printf("Invaild Position\n");
return FALSE;
}
if(L->length==0)
{
printf("Empty List\n");
}
printf("%d",L->data[i-1]);
*E = L->data[i-1];
printf("函数内E=%d\n",E);
for(j = i;j<L->length;j++)
L->data[j-1]=L->data[j];
L->length--;
return TRUE;
}