修改插入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
* @Date: 2019-06-25 23:10:17
* @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 OK 1
#define ERROR 0
@@ -13,23 +20,37 @@
typedef int Status;
typedef int ElemType;
Status visit(ElemType c){
printf("%d",c);
Status visit(ElemType c)
{
printf("%d", c);
return OK;
}
typedef struct
typedef struct
{
ElemType data[MAXSIZE];
int length;
}SqList;
} SqList;
Status InitList(SqList *L){
L ->length = 0;
Status ListTraverse(SqList L){
int i = 0;
for ( i = 0; i <= L.length -1; i++)
{
visit(L.data[i]);
}
}
Status InitList(SqList *L)
{
L->length = 0;
return OK;
}
Status ListEmpty(SqList L){
Status ListEmpty(SqList L)
{
if (L.length == 0)
{
return TRUE;
@@ -37,58 +58,85 @@ Status ListEmpty(SqList L){
else
{
return FALSE;
}
}
}
Status ClearList(SqList *L){
L ->length = 0;
Status ClearList(SqList *L)
{
L->length = 0;
return OK;
}
int ListLength(SqList L){
int ListLength(SqList L)
{
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)
{
return ERROR;
}
*e = L.data[i-1]; // 返回第i个元素下标为i -1
return OK;
*e = L.data[i - 1]; // 返回第i个元素下标为i -1
return OK;
}
int LocateElem(SqList L, ElemType e){
int LocateElem(SqList L, ElemType e)
{
if (L.length == 0)
{
return ERROR;
}
int i ;
for ( i = 0; i < L.length; i++)// 长度为5下标为0~4
int i;
for (i = 0; i < L.length; i++) // 长度为5下标为0~4
{
if (L.data[i] == e)
{
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
// 初始条件L存在且不满,且i<=length
if (L ->length == MAXSIZE || i < 1 || i > L ->length)
if (L->length == MAXSIZE || i < 1 || i > L->length + 1)
{
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.