#include <stdio.h>
#include <stdlib.h>
// 双向循环链表数据节点
typedef struct node
{
int data; // 数据域
struct node *prev, *next; // 指针域(2个指针,前后指针)
}node;
// 添加新数据(头插法)
void link_list_add(int new_data, node *head);
// 添加新数据(尾插法)
void link_list_add_tail(int new_data, node *head);
// 初始化一个节点
node *link_list_init(void);
// 链表遍历
void link_list_show(node *head);
// 链表遍历(前序遍历)
void link_list_show_prev(node *head);
// 删除指定节点
void link_list_del(int del_data, node *head);
int main()
{
// 1.初始化一条空链表
node *head = link_list_init();
// 2.数据操作(正数新增,负数删除)
int cmd;
while(1)
{
printf("Pls Input: ");
scanf("%d", &cmd); while(getchar()!='\n');
if(cmd > 0)
// link_list_add(cmd, head); // 头插
link_list_add_tail(cmd, head); // 尾插
else if(cmd < 0)
link_list_del(-cmd, head);
printf("next: ");
link_list_show(head); //后序遍历
printf("prev: ");
link_list_show_prev(head); //前序遍历
}
return 0;
}
// 删除指定节点
void link_list_del(int del_data, node *head)
{
// 0.判断是否为空链表
if(head->next == head)
{
printf("ERROR: Empty!\n");
return;
}
// 1.遍历链表,逐个对比找出欲删除节点pos
node *pos;
for(pos=head->next; pos!=head; pos=pos->next)
if(pos->data == del_data)
break;
if(pos == head)
{
printf("Not Found!\n");
return;
}
// 2.操作pos的前后节点,使他们联系起来。
// 前节点->next = 后节点;
// 后节点->prev = 前节点;
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
// 3.free释放pos
free(pos);
}
// 链表遍历(后序遍历)
void link_list_show(node *head)
{
node *pos;
for(pos=head->next; pos!=head; pos=pos->next)
printf("%d ", pos->data);
printf("\n");
}
// 链表遍历(前序遍历)
void link_list_show_prev(node *head)
{
node *pos;
for(pos=head->prev; pos!=head; pos=pos->prev)
printf("%d ", pos->data);
printf("\n");
}
// 添加新数据(尾插法)
void link_list_add_tail(int new_data, node *head)
{
// 直接使用尾插也可以。
// link_list_add(new_data, head->prev);
// 1.新节点分配堆空间,并把新数据放入
node *new = link_list_init();
new->data = new_data;
// 2.操作节点
// 2.1 操作新节点(顺序无所谓)
// 新节点->next = 头节点;
// 新节点->prev = 头节点->prev;
new->next = head;
new->prev = head->prev;
// 2.2 操作前后节点(顺序不能反)
// 前节点->next = 新节点;
// 后节点->prev = 新节点;
head->prev->next = new;
head->prev = new;
}
// 添加新数据(头插法)
void link_list_add(int new_data, node *head)
{
// 1.新节点分配堆空间,并把新数据放入
node *new = link_list_init();
new->data = new_data;
// 2.操作节点
// 2.1 操作新节点(顺序无所谓)
// 新节点->next = 头节点->next;
// 新节点->prev = 头节点;
new->next = head->next;
new->prev = head;
// 2.2 操作前后节点(顺序不能反)
// 后节点->prev = 新节点;
// 前节点->next = 新节点;
head->next->prev = new;
head->next = new;
}
// 初始化一个节点
node *link_list_init(void)
{
// 1.申请堆空间,并初始化(指针域指向自身)
node *p = malloc(sizeof(node));
if(p == NULL)
{
printf("malloc failled\n");
return NULL;
}
p->data = 0;
p->next = p;
p->prev = p;
// 2.返回
return p;
}
#ifndef __DLIST_H
#define __DLIST_H
/* This file is from Linux Kernel (include/linux/list.h)
* and modified by simply removing hardware prefetching of list items.
* Here by copyright, credits attributed to wherever they belong.
* Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)
*/
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions (“__xxx”) are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
/**
* container_of - cast a member of a structure out to the containing structure
*
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
/*
* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200)
struct list_head
{
struct list_head *prev;
struct list_head *next;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
// 宏定义语法规定只能有一条语句
// 如果需要多条语句,那就必须将多条语句放入一个do{}while(0)中使之成为一条复合语句
#define INIT_LIST_HEAD(ptr) \
do { \
(ptr)->next = (ptr); \
(ptr)->prev = (ptr); \
} while (0)
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
* list_add – add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail – add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del – deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}
/**
* list_del_init – deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move – delete from one list and add as another’s head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail – delete from one list and add as another’s tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_empty – tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(struct list_head *head)
{
return head->next == head;
}
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
/**
* list_splice – join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
/**
* list_splice_init – join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry – get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); \
pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop counter.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop counter.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_safe – iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop counter.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
#endif
#include <stdio.h>
#include <stdlib.h>
#include "kernel_list.h"
// 内核链表节点结构体(大结构体)
typedef struct node{
int data; // 数据域
struct list_head list; // 小结构体(前后逻辑)
}node;
// 初始化一个空链表(只有一个头节点)
node *kl_init(void);
// 头插法
void kl_add(int new_data, node *head);
// 尾插
void kl_add_tail(int new_data, node *head);
// 遍历内核链表
void kl_show(node *head);
// 删除数据节点
void kl_del(int del_data, node *head);
int main()
{
// 1.初始化一个空链表(只有一个头节点)
node *head = kl_init();
// 2.数据操作
int cmd;
while(1)
{
printf("Pls Input: ");
scanf("%d", &cmd); while(getchar()!='\n');
if(cmd > 0)
// kl_add(cmd, head); // 头插
kl_add_tail(cmd, head); // 尾插
else if(cmd < 0)
kl_del(-cmd, head); //删除
kl_show(head);
}
return 0;
}
// 删除数据节点
void kl_del(int del_data, node *head)
{
// 0.判断是否为空链表
if(list_empty(&head->list))
{
printf("ERROR: empty!\n");
return;
}
// 1.遍历链表,对比找到指定节点,找到则跳出break
// 如果在遍历中删除、移动节点,必须使用安全模式
// 如果只是遍历访问,不修改节点,使用安全/非安全都可以!
node *get_node;
struct list_head *pos, *n;
list_for_each_safe(pos, n, &head->list)
{
get_node = list_entry(pos, node, list);
if(get_node->data == del_data)
break;
}
// 2.如果找不到(循环正常结束)
if(pos == &head->list)
{
printf("ERROR: Not Found!\n");
return;
}
// 3.删除节点,释放大结构体堆空间
list_del(pos);
free(get_node);
}
// 尾插
void kl_add_tail(int new_data, node *head)
{
// 1.给新节点分配堆空间,并把数据放入
node *new = kl_init();
new->data = new_data;
// 2.操作节点
list_add_tail(&new->list, &head->list);
}
// 头插法
void kl_add(int new_data, node *head)
{
// 1.给新节点分配堆空间,并把数据放入
node *new = kl_init();
new->data = new_data;
// 2.操作节点
list_add(&new->list, &head->list);
}
// 遍历内核链表
void kl_show(node *head)
{
node *get_node; // 暂存大结构体地址
struct list_head *pos; // 遍历指针
list_for_each(pos, &head->list)
{
// 由小结构体地址pos,获得大结构体地址
get_node = list_entry(pos, node, list);
printf("%d ", get_node->data);
}
printf("\n");
}
// 初始化一个空链表(只有一个头节点)
node *kl_init(void)
{
// 1.分配一个节点堆空间,清空数据并初始化,2个指针指向自身。
node *p = malloc(sizeof(node));
if(p == NULL)
{
printf("malloc failed\n");
return NULL;
}
p->data = 0;
// p->list.next = &p->list;
// p->list.prev = &p->list;
INIT_LIST_HEAD(&p->list);
// 2.返回堆空间
return p;
}
#include <stdio.h>
#include <stdlib.h>
#include "kernel_list.h"
// 内核链表节点结构体(大结构体)
typedef struct node{
int data; // 数据域
struct list_head list; // 小结构体(前后逻辑)
}node;
// 初始化一个空链表(只有一个头节点)
node *kl_init(void);
// 头插法
void kl_add(int new_data, node *head);
// 尾插
void kl_add_tail(int new_data, node *head);
// 遍历内核链表
void kl_show(node *head);
// 删除数据节点
void kl_del(int del_data, node *head);
// 奇升偶降重排
void rearrange(node *head);
int main()
{
// 1.初始化一个空链表(只有一个头节点)
node *head = kl_init();
// 2.数据插入并显示
int cmd;
printf("Pls Input: ");
scanf("%d", &cmd); while(getchar()!='\n');
int i;
for(i=1; i<=cmd; i++)
kl_add_tail(i, head); // 尾插
kl_show(head);
// 3.重排后再次显示
rearrange(head);
kl_show(head);
return 0;
}
// 奇升偶降重排
void rearrange(node *head)
{
// pos向前遍历,直到重新回到head
// 如奇数,则继续遍历
// 如偶数,则移动到链表最后
// node *get_node;
// struct list_head *pos, *n;
// for(pos=(&head->list)->prev->prev; pos!=&head->list; pos=n)
// {
// n=pos->prev;
// get_node = list_entry(pos, node, list);
// if(get_node->data%2 == 0)
// list_move_tail(pos, &head->list);
// }
// 自行添加的前序遍历安全模式
node *get_node;
struct list_head *pos, *n;
list_for_each_prev_safe(pos, n, &head->list)
{
get_node = list_entry(pos, node, list);
if(get_node->data%2 == 0)
list_move_tail(pos, &head->list);
}
}
// 删除数据节点
void kl_del(int del_data, node *head)
{
// 0.判断是否为空链表
if(list_empty(&head->list))
{
printf("ERROR: empty!\n");
return;
}
// 1.遍历链表,对比找到指定节点,找到则跳出break
// 如果在遍历中删除、移动节点,必须使用安全模式
// 如果只是遍历访问,不修改节点,使用安全/非安全都可以!
node *get_node;
struct list_head *pos, *n;
list_for_each_safe(pos, n, &head->list)
{
get_node = list_entry(pos, node, list);
if(get_node->data == del_data)
break;
}
// 2.如果找不到(循环正常结束)
if(pos == &head->list)
{
printf("ERROR: Not Found!\n");
return;
}
// 3.删除节点,释放大结构体堆空间
list_del(pos);
free(get_node);
}
// 尾插
void kl_add_tail(int new_data, node *head)
{
// 1.给新节点分配堆空间,并把数据放入
node *new = kl_init();
new->data = new_data;
// 2.操作节点
list_add_tail(&new->list, &head->list);
}
// 头插法
void kl_add(int new_data, node *head)
{
// 1.给新节点分配堆空间,并把数据放入
node *new = kl_init();
new->data = new_data;
// 2.操作节点
list_add(&new->list, &head->list);
}
// 遍历内核链表
void kl_show(node *head)
{
node *get_node; // 暂存大结构体地址
struct list_head *pos; // 遍历指针
list_for_each(pos, &head->list)
{
// 由小结构体地址pos,获得大结构体地址
get_node = list_entry(pos, node, list);
printf("%d ", get_node->data);
}
printf("\n");
}
// 初始化一个空链表(只有一个头节点)
node *kl_init(void)
{
// 1.分配一个节点堆空间,清空数据并初始化,2个指针指向自身。
node *p = malloc(sizeof(node));
if(p == NULL)
{
printf("malloc failed\n");
return NULL;
}
p->data = 0;
// p->list.next = &p->list;
// p->list.prev = &p->list;
INIT_LIST_HEAD(&p->list);
// 2.返回堆空间
return p;
}