mirror of
https://github.com/by777/dataStructureForC.git
synced 2026-02-03 10:03:14 +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"
|
||||
}
|
||||
}
|
||||
100
_01.线性表顺序存储.c
100
_01.线性表顺序存储.c
@@ -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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user