单链表的查找
1.按位查找
1.1单链表的按位查找(带头结点)
LNode *getElemLinklist1(LinkList L, int i){
if(i<0){
return null;
}
LNode *p;
int j = 0;//j表示当前p指向的是第几个结点
p = L;
while(p != null && j < i){
p = p->next;
j++;
}
return p;
}
1.2顺序表的按位查找
int getElemSqlist1(SqList L, int i){
return L.data[i-1];
}
2.按值查找
LNode *findElemLinklist2(Linklist L, int e){
LNode *p = L->next;
while(p != null && p->data != e){
p = p->next;
}
return p;
}
3.求单链表长度(带头结点)
int linklistLen(LinkList L){
LNode *p;
int len = 0;
p = L->next;
while(p != null){
p = p->next;
len++;
}
return len;
}
4.求单链表长度(不带头结点)
int linklistLen(LinkList L){
LNode *p;
int len = 0;
p = L;
while(p != null){
p = p->next;
len++;
}
return len;
}
5.key
5.1三种基本操作的时间复杂度都是O(n)
5.2如何写循环扫描各个结点的代码逻辑(没事就想想)
5.3注意边界条件的处理
5.3.1按位序查找时,位序小于0
5.3.2按位序查找时,位序等于0
5.3.3按位序查找时,位序超过链表长度