Cstyle的劄記,Freertos核心詳解,第2篇

來源:互聯網
上載者:User

標籤:des   style   blog   os   io   資料   2014   art   

<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 __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



</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 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");}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.