标签:pre fragment str The 开始 初始 def class delete
#define ERROR 0 #define OK 1 #define TRUE 1 #define FALSE 0 #define MAXSIZE 20 typedef int ElemType; typedef struct { ElemType data[MAXSIZE]; int length; }Sqlist; typedef int Status;
//获取元素 Status GetElem(Sqlist *L,int i,ElemType e){ if (i<1 || L->length==0 || i>L->length) { return 0; } e = L->data[i-1]; return 0; } //插入元素 Status ListInsert(Sqlist *L,int i,ElemType e){ int k; if (L->length == MAXSIZE) { return ERROR; } if (i<1 || i> L->length+1) { return ERROR; } if (i<=L->length) {//都需要遍历 for (k = L-> length-1; k>=i-1; k--) { L->data[k+1] =L->data[k]; } } L->data[i-1] = e; L->length++; return FALSE; } //删除元素 Status ListDelete(Sqlist*L,int i,ElemType *e){ int k; if (L->length == 0) { return ERROR; } if (i<1 || i>L->length) { return ERROR; } *e = L->data[i-1]; if (i<L->length) { //都需要遍历 for (k = i; k<L->length; k++) { L->data[k-1] = L->data[k]; } } L->length--; return OK; }
线性表的插入和删除都需要遍历一次,从而时间复杂度是O(n),效率也是比较差的。
标签:pre fragment str The 开始 初始 def class delete
原文地址:https://www.cnblogs.com/yuhui-snail/p/9669644.html