数据结构-单向链表笔试题2

假设该链表只给出了头指针 list。 *在不改变链表的前提下,请设计一个尽可能高效的算法,查找链表中倒数第k个位置上的结点(k为正整数)。 *若查找成功,算法输出该结点的 data 域的值,删除该结点,并返回 1; *否则,只返回 0。
typedef int Elemtype_t;
typedef struct LNode{
	ELemtype_t data;
    struct LNode *next;
}LNode_t;
int LLink_Searchz_k(LNode_t *list,int k){
    LNode_t *Fast=list->next;
    LNode_t *Slow=list->next;
    int cnt=0;
    while(Fast_Pointer->next!=NULL)
    {
        if(cnt<k)  cnt++;
        else Slow=Slow->next;
        Fast=Fast->next;
    }
    if(cnt<k)  return 0;
    else
    {
		printf("%d",Slow->data);
        return 0;
    }
}
posted @ 2025-04-13 20:05  w1888  阅读(8)  评论(0)    收藏  举报