自己动手写一遍链表的的一些常见的操作有利于自己对链表这种数据结构的掌握。
一下是我自己码的,进作为参考。
/* 链表 */
struct note {
int count;
struct note *next;
};
#include <stdio.h>
#include <stdlib.h>
#define NUM 10
void creat_list_head(struct note *); /* 创建一个单链表(前插) */
void creat_list_foot(struct note *); /* 创建一个单链表(后插)*/
void print_list(struct note *); /* 打印一个单链表 */
int search_order(struct note *, int); /* 按序号查找单链表 */
struct note *search_value(struct note *, int); /* 按值查找单链表 */
void delete_list(struct note *, int); /* 删除单链表某个元素 */
void insert_list(struct note *, int, int); /* 单链表中插入某个元素 */
void total_delete(struct note *); /* 删除整个个链表 */
bool circle_list(struct note *); /* 判断一个链中是否有环 */
int main()
{
struct note *head;
head = (struct note *)(malloc(sizeof(struct note)));
head->count = NUM; /* 建立一个带头结点的空链表 */
head->next = NULL;
// creat_list_head(head); /* 建立链表 */
creat_list_foot(head);
// delete_list(head, 4); /* 删除第4个元素 */
insert_list(head, 4, 22);
total_delete(head);
print_list(head); /* 打印链表 */
// printf ("%d", search_order(head, 4)); /* 查找链表第4个元素 */
getchar();
return 0;
}
/*
* 创建一个单链表 前插法
*/
void creat_list_head(struct note *head)
{
int i;
struct note *p;
for (i = NUM; i > 0; i--) {
p = (struct note *)(malloc(sizeof(struct note)));
p->count = i-1;
p->next = head->next;
head->next = p;
}
}
/*
* 创建一个单链表 后插法
*/
void creat_list_foot(struct note *head)
{
int i;
struct note *p, *foot;
foot = head;
for (i = NUM; i > 0; i--) {
p = (struct note *)(malloc(sizeof(struct note)));
p->count = i-1;
p->next = NULL;
foot->next = p; /* 新加入的地址给上一个的尾指针 */
foot = p; /* 尾指针指向最后一个 */
}
}
/*
* 打印单链表
*/
void print_list(struct note *head)
{
struct note *p = head->next;
while (p) {
printf ("%d\n", p->count);
p = p->next;
}
}
/*
* 链表的查找 序号查找 查找第 i 个元素
*/
int search_order(struct note *head, int i)
{
int j = 1;
struct note *p = head->next;
while (p && j++ < i) {
p = p->next;
}
return !p ? -1 : p->count; /* 如果 p 是空指针 */
}
/*
* 链表的查找 按值查找 查找值 n
*/
struct note *search_value(struct note *head, int n)
{
struct note *p;
p = head->next;
while (p && p->count != n)
p = p->next;
return p;
}
/*
* 链表的删除 删除第 i 个元素 注意: 首先要找到第 i-1 个元素
*/
void delete_list(struct note *head, int i)
{
int j = 1, temp_count;
struct note *p, *temp;
p = head; /* 与查找不同的是 p 直接指向 head */
while (p && j++ < i) /* 先找到第 i-1 个元素 */
p = p->next; /* p 存储的是第 j 个元素的首地址 */
if (!p) /* p 指向的是第 i-1 个元素的首地址 */
printf("Error");
temp = p->next; /* 保存指向第 i 个元素的指针、数据以备释放 */
temp_count = temp->count;
p->next = temp->next;
free (temp);
}
/*
* 链表的插入 将 n 插到第 i-1 个元素的后面 注意: 首先要找到第 i-1 个元素
*/
void insert_list(struct note *head, int i, int n)
{
int j = 1;
struct note *p, *new_note;
p = head;
new_note = (struct note *)(malloc(sizeof(struct note))); /* 新建一个节点 */
new_note->count = n;
new_note->next = NULL;
while (p && j++ < i)
p = p->next;
if (!p)
printf ("Error");
new_note->next = p->next;
p->next = new_note;
}
/*
* 删除整个链表
*/
void total_delete(struct note *head)
{
struct note *p, *temp;
p = head->next;
head->next = NULL; /* 将表头置为空表 */
while (p) {
temp = p->next;
free(p);
p = temp;
}
}
/*
* 判断一个链表中是否有环
*/
bool circle_list(struct note *head)
{
struct note *p, *temp1, *temp2;
p = head->next;
if (p = NULL) /* 无节点 */
return false;
if (p->next == p) /* 自环 */
return true;
temp1 = p; /* 定义两个指针,一个运动快,一个慢,如果慢的能赶上快的,那就是有环 */
temp2 = p->next;
while (temp1 != temp2 && temp2 != NULL && temp2->next != NULL) {
temp1 = temp1->next;
temp2 = temp2->next->next;
}
if (temp1 == temp2)
return true;
return false;
}
以上只是单链表的操作,我觉得其他的链表都差不多就没有再花时间去边,以后遇到了在慢慢摸索,其实原理都差不多。
希望互相学习,共同进步。