(3)、插入
单链表的插入运算最主要的操作是获得插入结点,例子代码通过多分支选择结构同时支持按序号查找和按值查找。
参考代码如下:
#define FIND_BY_NUM 0
#define FIND_BY_KEY 1
void insert_node_with_headnode(list_node_p head, list_node_p tmp, unsigned int cmd, void *data)
{
list_node_p p;
int *i;
unsigned long *key;
switch (cmd)
{
case FIND_BY_NUM:
i = data;
p = get_node_with_headnode(head, *i - 1);
break;
case FIND_BY_KEY:
key = data;
p = locate_node_with_headnode(head, *key);
}
if (p == NULL) {
fprintf(stderr, "position error\n");
exit(-1);
}
tmp -> next = p -> next;
p -> next = tmp;
}
(4)、删除
单链表的删除运算最主要的操作也是获得删除结点,例子代码也能支持按序号查找和按值查找(这里的按值查找是先获得此值所在结点在链表中的序号,然后再根据序号查找它的直接前趋)。
参考代码如下:
void delete_node_with_headnode(list_node_p head, unsigned int cmd, void *data)
{
list_node_p p, work;
int *i, j = 0;
unsigned long *key;
switch (cmd)
{
case FIND_BY_NUM:
i = data;
p = get_node_with_headnode(head, *i - 1);
break;
case FIND_BY_KEY:
key = data;
list_node_p tmp = head -> next;
while (tmp && tmp->data.num != *key) {
tmp = tmp -> next;
j++;
}
p = get_node_with_headnode(head, j);
}
if (p == NULL || p -> next == NULL) {
fprintf(stderr, "position error\n");
exit(-1);
}
work = p -> next;
p -> next = p -> next -> next; //p -> next = work -> next
free(work);
}
(5)、销毁链表
static void destroy_list(list_node_p head)
{
list_node_p p = head;
while(p) {
head = p -> next;
free(p);
p = head;
}
}
(6)、打印链表每个结点的数据域
static void print_list(list_node_p head)
{
list_node_p p = head -> next;
printf("%-15s %-12s %-5s %-5s %-6s\n", "name", "num", "sex", "age", "marks");
while (p) {
printf("%-15s %-12lu %-5c %-5hu %-6hu\n", p->data.name, p->data.num,
p->data.sex, p->data.age, p->data.marks);
p = p -> next;
}
printf("\n");
}
(7)、链表长
static int list_length(list_node_p head)
{
int len = 0;
list_node_p p = head -> next; //Does not include the headnode
while (p) {
p = p -> next;
len++;
}
return len;
}
综合测试上面所讲的函数:
int main(void)
{
list_node_p head, tmp, p;
head = create_list_at_tail_with_headnode();
printf("list_len = %d\n", list_length(head));
print_list(head);
tmp = (list_node_t *)malloc(sizeof(list_node_t));
if (!tmp)
perror("allocate dynamic memory");
strcpy(tmp -> data.name, "Emily Zhang");
tmp -> data.num = 201201010;
tmp -> data.sex = 'F';
tmp -> data.age = 17;
tmp -> data.marks = 98;
tmp -> next = NULL;
int a = 4;
insert_node_with_headnode(head, tmp, FIND_BY_NUM, &a);
printf("list_len = %d\n", list_length(head));
print_list(head);
unsigned long key = 201201008;
delete_node_with_headnode(head, FIND_BY_KEY, &key);
printf("list_len = %d\n", list_length(head));
print_list(head);
destroy_list(head);
return 0;
}
首先,执行程序,依次输入以下五位学生的相关信息,然后按quit退出。
输入结果:
其次,在链表的第4位插入一位学生的相关信息,插入成功后打印的信息如下:
再次,删除学号为201201008学生的信息,删除成功后打印的信息如下:
最后,销毁所创建的链表。
3、单循环链表
单循环链表的特点是表中终止结点的指针域指向头结点(或者开始结点),整个链表形成一个环,如下图所示:
单循环链表的操作和单链表基本一致,差别仅在于算法中的循环条件不是p或p->next是否为空,而是它们是否等于头指针。
(1)、仅设尾指针的单循环链表
用尾指针表示的单循环链表对开始结点和终止结点的查找时间都是O(1),而表的操作常常是在表的首尾位置上进行,因此,实用中多采用尾指针表示单循环链表,如下图所示:
(2)两个单循环链表(尾指针)的合并
若在单链表或头指针表示的单循环链表上做这种合并操作,都需要遍历第一个链表,找到终止结点,而在尾指针表示的单循环链表上,仅须要修改相应的指针即可,如下图所示:
参考代码如下:
list_node_p merge_list(list_node_p a, list_node_p b)
{
list_node_p p = a->next;
a->next = b->next->next;
free(b->next);
b->next = p;
return b;
}