Cstyle notes, Freertos kernel details, article 2nd

Source: Internet
Author: User

Cstyle notes, Freertos kernel details, article 2nd

The most common and core data structure in RTOS is the implementation of two-way linked list. Compile and test in VS 2008.
/** @file   Copyright (c) 2008 - 2014, MX.Studio All rights reserved. Created by Cstyle          **/#ifndef _LIST_H_#define _LIST_H_#ifdef __cplusplusextern "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 listUINT8 List_DeletEnd(List_t * const ListHead); //delet item from the end of listUINT8 List_DeletHead(List_t * const ListHead); //delet item from head of listUINT8 List_IsEmpty(List_t * const ListHead);UINT8 List_Destroy(List_t * const ListHead);void List_Test();#ifdef __cplusplus}#endif#endif


/** @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 manualAssert(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 mannalAssert(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 itemsList_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 emptyprintf("check empty :");if( List_IsEmpty(List)) printf("empty\n\n");else printf("not empty\n \n");//destroy listprintf("destroy list \n");List_Destroy(List);//check emptyprintf("check empty :");if( List_IsEmpty(List)) printf("empty\n");else printf("not empty\n");}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.