正值新春佳节,在此祝大家新春快乐,龙行龘龘,万事顺意~~~
所以让我看看是谁大年初一还在卷数据结构捏~~
最后给链表收个尾,这些小操作好像不是那么难了,所以就不做图了,直接上代码
首先是查找,分根据节点里面的值进行查找和根据下表进行查找,都是返回节点。
1.根据值进行查找
LNODE SearchLinkedListByValue(LINKED_LIST *list,int value){
LINK_NODE current=list->head;
for(int i=0;i<list->size;i++){
if(current->data==value){
return current;
}
current=current->next;
}
return NULL;
}
就是注意一下current前移的语句要放在判断的后面,注意一下顺序,否则第一个节点就没法顾及到了。
2.根据下标进行查找并修改其中的值
LNODE SearchandReplaceLinkedListByIndex(LINKED_LIST *list,int index,int element){
LINK_NODE current=list->head;
for(int i=0;i<index;i++){
current=current->next;
}
current->data=element;
return current;
}
3.销毁链表
void destorylinkedlist(LINKED_LIST *list){
for(int i=0;i<list->size;i++){
LINK_NODE current=list->head;
list->haed=list->head->next;
free(current);
}
list->size=0;
}
注意这里要先用一个新变量来存储list->head,让这个变量作为每次被free掉的变量,然后让head变量不断地向前遍历,不能直接使用head做free的对象,否则一旦释放掉就不能再使用了。