/*单链表的结点插入:尾插法*/
/*
* SCs in different cases
* author: Whywait
*/
typedef struct node {
Elemtype data;
struct node* next;
} Lnode;
Lnode* insert_in_end1(Linklist& L, Lnode* tail, Lnode* p) {
// if know the end node, the pointer to the end node is tail
// return the head node
tail->next = p;
return L;
}
Lnode* insert_in_end2(Linklist& L, Lnode* p) {
// if don't know the end node
// return the head node
Lnode* tail = L;
while (tail->next)
tail = tail->next;
tail->next = p;
return L;
}
void insert_in_end3(Linklist& L, Lnode* tail, Lnode* p) {
// if know the end node, the pointer to the end node is tail
tail->next = p;
}
void insert_in_end3(Linklist& L, Lnode* p) {
// if don't know the end node
Lnode* tail = L;
while (tail->next)
tail = tail->next;
tail->next = p;
}
链表必学算法(二):尾插法
最新推荐文章于 2022-10-14 22:56:12 发布