修改插入bug

This commit is contained in:
Xu Bai
2019-06-26 14:49:16 +08:00
parent 4bb540559b
commit ef0ecfc68c
8 changed files with 78 additions and 27 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,3 +1,6 @@
{ {
"python.pythonPath": "C:\\ProgramData\\anaconda\\python.exe" "python.pythonPath": "C:\\ProgramData\\anaconda\\python.exe",
"files.associations": {
"cstdio": "c"
}
} }

View File

@@ -2,8 +2,15 @@
* @Author: Xu Bai * @Author: Xu Bai
* @Date: 2019-06-25 23:10:17 * @Date: 2019-06-25 23:10:17
* @LastEditors: Xu Bai * @LastEditors: Xu Bai
* @LastEditTime: 2019-06-26 14:15:10 * @LastEditTime: 2019-06-26 14:48:07
*/ */
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#define MAXSIZE 20 #define MAXSIZE 20
#define OK 1 #define OK 1
#define ERROR 0 #define ERROR 0
@@ -13,23 +20,37 @@
typedef int Status; typedef int Status;
typedef int ElemType; typedef int ElemType;
Status visit(ElemType c){ Status visit(ElemType c)
printf("%d",c); {
printf("%d", c);
return OK; return OK;
} }
typedef struct typedef struct
{ {
ElemType data[MAXSIZE]; ElemType data[MAXSIZE];
int length; int length;
}SqList; } SqList;
Status InitList(SqList *L){ Status ListTraverse(SqList L){
L ->length = 0; int i = 0;
for ( i = 0; i <= L.length -1; i++)
{
visit(L.data[i]);
}
}
Status InitList(SqList *L)
{
L->length = 0;
return OK; return OK;
} }
Status ListEmpty(SqList L){ Status ListEmpty(SqList L)
{
if (L.length == 0) if (L.length == 0)
{ {
return TRUE; return TRUE;
@@ -40,54 +61,81 @@ Status ListEmpty(SqList L){
} }
} }
Status ClearList(SqList *L){ Status ClearList(SqList *L)
L ->length = 0; {
L->length = 0;
return OK; return OK;
} }
int ListLength(SqList L){ int ListLength(SqList L)
{
return L.length; return L.length;
} }
Status GetElem(SqList L, ElemType *e, int i){ Status GetElem(SqList L, ElemType *e, int i)
{
if (L.length == 0 || i < 1 || i > L.length) if (L.length == 0 || i < 1 || i > L.length)
{ {
return ERROR; return ERROR;
} }
*e = L.data[i-1]; // 返回第i个元素下标为i -1 *e = L.data[i - 1]; // 返回第i个元素下标为i -1
return OK; return OK;
} }
int LocateElem(SqList L, ElemType e){ int LocateElem(SqList L, ElemType e)
{
if (L.length == 0) if (L.length == 0)
{ {
return ERROR; return ERROR;
} }
int i ; int i;
for ( i = 0; i < L.length; i++)// 长度为5下标为0~4 for (i = 0; i < L.length; i++) // 长度为5下标为0~4
{ {
if (L.data[i] == e) if (L.data[i] == e)
{ {
return i + 1; return i + 1;
} }
} }
return 0; return 0;
} }
Status ListInert(SqList *L, ElemType e, int i){ Status ListInert(SqList *L, ElemType e, int i)
{
// 在第i个位置之前插入新元素e // 在第i个位置之前插入新元素e
// 初始条件L存在且不满,且i<=length // 初始条件L存在且不满,且i<=length
if (L ->length == MAXSIZE || i < 1 || i > L ->length) if (L->length == MAXSIZE || i < 1 || i > L->length + 1)
{ {
return ERROR; return ERROR;
} }
int k; int k;
// 插入新元素 // 插入新元素
for (k = L ->length -1; ;) for (k = L->length - 1; k >= i -1; k--)
{ {
/* 测试提交 */ L ->data[k + 1] = L ->data[k];
// 从最后一个位置(length下标)开始移动
} }
L ->data[i-1] = e;
L ->length ++;
}
int main(){
SqList L;
int isInit = InitList(&L);
if (isInit)
{
for (int j = 0; j <= 5 ; j++)
{
int res = ListInert(&L,j,1);
}
}
ListTraverse(L);
getchar();

BIN
a.out

Binary file not shown.