欢迎来到天天文库
浏览记录
ID:38194777
大小:41.00 KB
页数:5页
时间:2019-05-25
《双向链表的C实现》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、双向链表的C实现struct Node { void* pUserData; struct Node* pNext; struct Node* pPrev; } __Node; struct TABLE { struct Node* pHead; struct Node* pTail; } __TABLE; struct POSITION { struct TABLE* pTable; struct Node* pNode; } __POSITION;struct TABLE* CreateTable(void); void FreeTable(struct TAB
2、LE*); void AddTail(struct TABLE*,void* pUserData); void InsertAt(struct POSITION*,void*); void RemoveAt(struct POSITION*); void* GetAt(struct POSITION*); void* GetNext(struct POSITION*); void* GetPrev(struct POSITION*); struct POSITION GetHeadPosition(struct TABLE*); struct POSITION Ge
3、tTailPosition(struct TABLE*); struct POSITION GetPosition(struct TABLE*,unsigned int); int GetCount(struct TABLE*); struct TABLE* CreateTable() { struct TABLE* pTable = (struct TABLE*)malloc(sizeof(__TABLE)); if(pTable == NULL){ return NULL; } pTable->pHead = NULL; pTable->pTail = NULL
4、; return pTable; } void FreeTable(struct TABLE* pTable) { struct Node* pNode; while(pTable->pHead){ pNode = pTable->pHead; pTable->pHead = pTable->pHead->pNext; free(pNode); } free(pTable); } void AddTail(struct TABLE* pTable,void* pUserData) { struct Node* pNode; pNode = (struct Node*
5、)malloc(sizeof(__Node)); if(pNode == NULL){ return; } pNode->pUserData = pUserData; pNode->pNext = NULL; if(pTable->pHead==NULL && pTable->pTail==NULL){ pNode->pPrev = NULL; pTable->pHead = pNode; }else{ pNode->pPrev = pTable->pTail; pTable->pTail->pNext = pNode; } pTable->pTail = pNod
6、e; } void RemoveAt(struct POSITION* pos) { if(pos->pNode == NULL){ errcode
7、=1<<0; return; } if(pos->pTable->pHead == NULL && pos->pTable->pTail == NULL) return; if(pos->pNode == pos->pTable->pHead){ if(pos->pNode == pos->pTable->pTail){ pos->pTable->pHead = NULL; pos->pTable->pTail = N
8、ULL; }else{ pos->pTable->pHead = pos->pNode->pNext; pos->pTable->pHead->pPrev = NULL; } else{ if(pos->pNode == pos->pTable->pTail){ pos->pTable->pTail = pos->pNode->pPrev; pos->pTable->pTail->pNext = NULL; }else{ pos->pNode->pPrev->pNext = pos->pNode->pNext; pos->pNode->pNext->pPrev
此文档下载收益归作者所有