mirror of
https://github.com/by777/dataStructureForC.git
synced 2026-04-14 10:20:54 +08:00
修改插入bug
This commit is contained in:
BIN
.vscode/ipch/63c0e544b57316e2/1.ipch
vendored
BIN
.vscode/ipch/63c0e544b57316e2/1.ipch
vendored
Binary file not shown.
BIN
.vscode/ipch/63c0e544b57316e2/mmap_address.bin
vendored
BIN
.vscode/ipch/63c0e544b57316e2/mmap_address.bin
vendored
Binary file not shown.
BIN
.vscode/ipch/73223f58bc31aa68/mmap_address.bin
vendored
BIN
.vscode/ipch/73223f58bc31aa68/mmap_address.bin
vendored
Binary file not shown.
BIN
.vscode/ipch/e211ad9ea5ec3980/01线性表顺序存储_LIST.ipch
vendored
BIN
.vscode/ipch/e211ad9ea5ec3980/01线性表顺序存储_LIST.ipch
vendored
Binary file not shown.
BIN
.vscode/ipch/e211ad9ea5ec3980/mmap_address.bin
vendored
BIN
.vscode/ipch/e211ad9ea5ec3980/mmap_address.bin
vendored
Binary file not shown.
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,3 +1,6 @@
|
|||||||
{
|
{
|
||||||
"python.pythonPath": "C:\\ProgramData\\anaconda\\python.exe"
|
"python.pythonPath": "C:\\ProgramData\\anaconda\\python.exe",
|
||||||
|
"files.associations": {
|
||||||
|
"cstdio": "c"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user