(Linux內核鏈表)用鏈表存儲若干自然數。 比如:鏈表中存儲: 1 2 3 4 5 6 7 8 9…… 將其重排成: 1 3 5 7 9 ……8 6 4 2 (奇數升序偶數降序)

更多資料請點擊:我的目錄
本篇僅用於記錄自己所學知識及應用,代碼仍可優化,僅供參考,如果發現有錯誤的地方,儘管留言於我,謝謝!

運行結果:
在這裏插入圖片描述
Linux內核鏈表:

#ifndef __DLIST_H
#define __DLIST_H

/*
* 這個文件來自Linux內核(include/Linux/list.h)
* 只需刪除列表項的硬件預取即可修改。
* 這裏的版權,學分歸屬於他們所屬的任何地方。
* 庫萊什·尚穆加桑達拉姆(庫萊什[蠕動]伊西斯·波利·埃杜)
*/
/*
* 簡單的雙鏈表實現。
* 一些內部功能(“__xxx”)是有用的 ,
* 當處理整個列表而不是單個條目時,
* 有時我們已經知道下一個/上一個條目, 
* 我們可以通過直接使用它們生成
* 比使用一般的單入口例程更好的代碼。
*/
/*
 * container-將結構的一個成員投射到包含結構
 * @ptr:	指向成員的指針。
 * @type:	嵌入的容器結構的類型。
 * @member:	結構中成員的名稱。
 */
#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) );})
/*
* 這些非空指針將導致頁面錯誤
* 在正常情況下,用於驗證沒有人使用
* 未初始化的列表項。
 */
#define LIST_POISON1  ((void *) 0x00100100)
#define LIST_POISON2  ((void *) 0x00200)

// 內核鏈表的標準實現
// 只包含雙向鏈表的指針(邏輯)
// 不包含任何具體的數據
// 這樣的節點形成的鏈表是純粹的鏈表
struct list_head
{
	struct list_head *next;
	struct list_head *prev;
};

// 圍繞以上標準鏈表的操作,都統一爲如下這些宏或者函數:

// 1. 初始化節點: 讓節點自身形成雙向循環
#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)

#define INIT_LIST_HEAD(ptr) do { \
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

/*
* 在兩個已知的連續條目之間插入一個新條目。
* 這隻適用於我們知道的內部列表操作
* 上一個/下一個條目已經存在!
*/
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 要添加到head後面的標題
*
* 在指定的頭之後插入新項。
* 這有利於實現堆棧。
*/

// 2. 將節點new,插入以head爲首的標準鏈表的頭部(即head的後面)
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 要添加到head前面的標題
*
*  Insert a new entry before the specified head.
*  在指定的頭之前插入新項。
*  This is useful for implementing queues.
*  這對於實現隊列很有用。
*/

// 3. 將節點new,插入以head爲首的標準鏈表的尾部(即head的前面)
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}

/*
* 通過使上一個/下一個條目
* 相互指向。
*
* 這隻適用於我們知道的內部鏈表操作
* 上一個/下一個條目已經存在!
*/
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.
* 注意:list_empty on entry不返回true之後,該項處於未定義狀態。
*/

// 4. 將entry指向的節點,從標準鏈表中,剔除出去
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.
*/

// 從小結構體指針ptr,獲得對應的大結構體指針
// 參數說明:
// ptr:   指向某個小結構體的指針
// type:  大結構體的類型
// member:大結構體中,小結構體的成員名
#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.
*/


// 遍歷:(不適用於一邊遍歷一邊刪除節點的場合)
// 從head開始往後遍歷標準鏈表
// pos將逐個指向每個標準節點(小結構體)
#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.
*/

// 遍歷:(不適用於一邊遍歷一邊刪除節點的場合)
// 從head開始往前遍歷
// pos將逐個指向每個標準節點(小結構體)
#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.
*/

// 遍歷:(適用於一邊遍歷一邊刪除節點的場合)
// 從head開始往前遍歷
// pos將逐個指向每個標準節點(小結構體)
// n每次保留pos的下一個節點,防止pos被刪除之後迷路
#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
* list_for_each_entry_safe–在給定類型safe的鏈表上迭代以防止刪除鏈表項
* @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

main函數:

#include <stdio.h>
#include <stdlib.h>
#include "kernel_list.h"

struct node
{
	int data;
	struct list_head list;
};

struct node *creat_list()
{
	struct node *head = malloc(sizeof(struct node));
	if(head != NULL)
	{
		INIT_LIST_HEAD(&head->list);
	}
	return head;
}

struct node * new_node(int data)
{
	struct node * new = malloc(sizeof(struct node));

	if(new != NULL)
	{
		new->data = data;
		INIT_LIST_HEAD(&new->list);
	}
	return new;
}

void list_link(struct node *head, struct node *new)
{
	list_add_tail(&new->list, &head->list);
}

void rearrange(struct node *head)
{
	struct list_head *pos,*k=head->list.prev;
	struct node *p;
	list_for_each_prev(pos , &head->list)
	{
		p = list_entry(pos, struct node, list);
		if(p->data %2 == 0)
		{
			list_move_tail(pos, &head->list);
			pos = k;
		}
		else
		{
			k = pos;
		}
	}
}

void destory_node(struct node *head)
{
	struct node *p;
	struct list_head *pos,*n;

	list_for_each_safe(pos, n, &head->list)
	{
		list_del(pos);

		p = list_entry(pos, struct node, list);

		free(p);
	}	
}

void show(struct node * head)
{
	if(list_empty( &head->list ))
	{
		printf("鏈表爲空!\n");
		return;
	}
	struct node *pos;

	list_for_each_entry(pos, &head->list, list)
	{
		printf("%d\t", pos->data);
	}
	printf("\n");
}

int main()
{
	struct node *head = creat_list();
	printf("請輸入將要插入的數據個數:");
	int n;
	scanf("%d", &n);
	for(int i= 1 ;i <= n; i++)
	{
		struct node *new = new_node(i);
		list_link(head, new);
	}

	show(head);
	rearrange(head);//重排鏈表
	show(head);	
	destory_node(head);//銷燬鏈表
	show(head);
	return 0;
}

更多資料請點擊:我的目錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章