求两个程序 须注释(C语言)

来源:百度知道 编辑:UC知道 时间:2024/05/30 17:09:41
在双向链表中删除指定元素

删除链表的倒数第m的元素

1、在双向链表中删除指定元素。
void del(List head, List node){
List pre=new List();
pre.next = head;
List cur = head;
while(cur && cur!=node){
cur=cur.next;
pre=pre.next;
}
if(!cur) return;
List post = cur.next;
pre.next=cur.next;
post.last=cur.last;
return;
}
2、
删除单链表的倒数第 m 个元素。
给定一个单向链表 (长度未知),请设计一个既节省时间又节省空间的算法来找出该链表中的倒数第 m 个元素。实现这个算法,并为可能出现的特例情况安排好处理措施。

#include <stddef.h>

struct listtype
{
int data;
struct listtype * next;
};

typedef struct listtype * list;

/* Find the mth element of the singly linked list sll from the end. */
list find_the_mth_element_from_end(list sll, int m)
{
list fast = sll; /* Used for loop detecting. */
list ahead = sll;
list after = sll;

if (NULL == ahead || m <= 0)
{
return NULL;
}

while (