首页 > 代码库 > Cstyle的札记,Freertos内核详解,第2篇

Cstyle的札记,Freertos内核详解,第2篇

<span style="white-space:pre">	</span>RTOS里面最常见也最核心的数据结构,双向链表实现。 VS 2008下可编译测试。
<pre name="code" class="cpp">/** @file
   
	Copyright (c) 2008 - 2014, MX.Studio 
	
	All rights reserved. 

	Created by Cstyle          
**/
#ifndef _LIST_H_
#define _LIST_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "Syslib.h"

struct ListNode
{	
	struct ListNode *pPrevious;
	struct ListNode *pNext;
};

typedef struct ListNode List_t;


List_t * ListCreate();
UINT8 List_InsertEnd(List_t * const ListHead,List_t * const ListItem);
UINT8 List_InsertHead(List_t * const ListHead,List_t * const ListItem);//insert at the head of list
UINT8 List_DeletEnd(List_t * const ListHead); //delet item from the end of list
UINT8 List_DeletHead(List_t * const ListHead); //delet item from head of list
UINT8 List_IsEmpty(List_t * const ListHead);
UINT8 List_Destroy(List_t * const ListHead);
void List_Test();

#ifdef __cplusplus
}
#endif

#endif


</pre><pre code_snippet_id="438878" snippet_file_name="blog_20140730_5_4820611" name="code" class="cpp">
</pre><pre code_snippet_id="438878" snippet_file_name="blog_20140730_6_6663774" name="code" class="cpp">/** @file
   
	Copyright (c) 2008 - 2014, MX.Studio 
	
	All rights reserved. 

	Created by Cstyle          
**/
#include "List.h"


List_t * ListCreate()
{
	List_t *p;
	p=(List_t *)malloc(sizeof(List_t));
	Assert(p);
	p->pPrevious=p;
	p->pNext=p;
	return p;
}

UINT8 List_InsertEnd(List_t * const ListHead,List_t * const ListItem)//insert at the end of list
{
	Assert(ListHead);
	Assert(ListItem);
	ListItem->pPrevious=ListHead->pPrevious;
	ListItem->pNext=ListHead;
	ListHead->pPrevious->pNext=ListItem;
	ListHead->pPrevious=ListItem;
	return 0;
}

UINT8 List_InsertHead(List_t * const ListHead,List_t * const ListItem)//insert at the head of list
{
	Assert(ListHead);
	Assert(ListItem);
	ListItem->pPrevious = ListHead;
	ListItem->pNext = ListHead->pNext;
	ListHead->pNext->pPrevious = ListItem;
	ListHead->pNext = ListItem;	
	return 0;	
}

UINT8 List_DeletEnd(List_t * const ListHead) //delet item from the end of list
{
	//usr should free memory by manual
	Assert(ListHead);
	Assert(!List_IsEmpty(ListHead));
	ListHead->pPrevious->pPrevious->pNext=ListHead;
	ListHead->pPrevious=ListHead->pPrevious->pPrevious;
	return 0;
}

UINT8 List_DeletHead(List_t * const ListHead) //delet item from head of list
{
	//usr shoud free memor by mannal
	Assert(ListHead);
	Assert(!List_IsEmpty(ListHead));
	ListHead->pNext->pNext->pPrevious=ListHead;
	ListHead->pNext=ListHead->pNext->pNext;
	return 0;
}

UINT8 List_IsEmpty(List_t * const ListHead)
{
	Assert(ListHead);
	if((ListHead->pPrevious==ListHead)&&(ListHead->pNext==ListHead)) return 1;
	else return 0;
}

UINT8 List_Destroy(List_t * const ListHead)
{	
	List_t *p;
	Assert(ListHead);
	p =ListHead->pPrevious;
	while(!List_IsEmpty(p))
	{	
		p=p->pPrevious;
		free(p->pNext);	
		p->pNext=ListHead;
		ListHead->pPrevious=p;
		
	}
	return 0;	
}

void List_Test()
{
	int i;
	List_t * List,*ListH,*ListE,*pList;

	printf("-----------------------------------------------\n");
	printf("------------Start List Test!------------------\n");
	printf("-----------------------------------------------\n");

	List=ListCreate();
	pList=List;
	printf("List Header address: %x, List->previous:%x, List->pNext:%x\n",List,List->pPrevious,List->pNext);
	ListH=(List_t *)malloc(sizeof(List_t));
	ListE=(List_t *)malloc(sizeof(List_t));
	//insert at end


    List_InsertHead(List,(List_t *)malloc(sizeof(List_t)));	//insert items
    List_InsertHead(List,(List_t *)malloc(sizeof(List_t)));
	List_InsertEnd(List,(List_t *)malloc(sizeof(List_t)));
	List_InsertEnd(List,(List_t *)malloc(sizeof(List_t)));

	printf("\n\ninsert items:\n");
	i=0;
	pList=List;
	while(pList->pNext!=List)  //show list items
	{
		printf("List[%d]=%x-> \n",i,pList);
		pList=pList->pNext;
		i++;
	}

	printf("\n\ndelete items:\n");
	i=0;
	pList=List;
	List_DeletEnd(List);		//delete items
	List_DeletHead(List);
	
	while(pList->pNext!=List)  //show list items
	{
		printf("List[%d]=%x-> \n",i,pList);
		pList=pList->pNext;
		i++;
	}

	//check empty
	//List_DeletEnd(List);		//delete items
	//List_DeletHead(List);
	
	//check empty
	printf("check empty :");
	if( List_IsEmpty(List)) printf("empty\n\n");
	else printf("not empty\n \n");

	//destroy list
	printf("destroy list \n");
	List_Destroy(List);

	//check empty
	printf("check empty :");
	if( List_IsEmpty(List)) printf("empty\n");
	else printf("not empty\n");


}