欢迎来到天天文库
浏览记录
ID:38194777
大小:41.00 KB
页数:5页
时间:2019-05-25
《双向链表的C实现》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
双向链表的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 TABLE*); 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 GetTailPosition(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; 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*)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 = pNode; } void RemoveAt(struct POSITION* pos) { if(pos->pNode == NULL){ errcode|=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 = NULL; }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 = pos->pNode->pPrev; } } free(pos->pNode); } void InsertAt(struct POSITION* pos,void* pUserData) { struct Node* pNode; if(pos->pNode == NULL){ return; } pNode = (struct Node*)malloc(sizeof(__Node)); if(pNode == NULL){ return; }pNode->pUserData = pUserData; if(pos->pNode == pos->pTable->pHead){ pNode->pPrev = NULL; pNode->pNext = pos->pTable->pHead; pos->pTable->pHead->pPrev = pNode; pos->pTable->pHead = pNode; }else{ pos->pNode->pPrev->pNext = pNode; pNode->pPrev = pos->pNode->pPrev; pNode->pNext = pos->pNode; pos->pNode->pPrev = pNode; } } void* GetAt(struct POSITION* pos) { if(pos->pNode == NULL){ return NULL; }return pos->pNode->pUserData; } void* GetNext(struct POSITION* pos) { struct Node* pNode; if(pos->pNode == NULL) return NULL; pNode = pos->pNode; pos->pNode = pos->pNode->pNext; return pNode->pUserData; } void* GetPrev(struct POSITION* pos) { struct Node* pNode; if(pos->pNode == NULL) return NULL; pNode = pos->pNode; pos->pNode = pos->pNode->pPrev; return pNode->pUserData; } struct POSITION GetHeadPosition(struct TABLE* pTable) { struct POSITION pos; pos.pTable = pTable; pos.pNode = pTable->pHead; return pos; } struct POSITION GetTailPosition(struct TABLE* pTable) { struct POSITION pos; pos.pTable = pTable; pos.pNode = pTable->pTail; return pos; } struct POSITION GetPosition(struct TABLE* pTable,unsigned int index) { struct POSITION pos; pos.pTable = pTable; pos.pNode = pTable->pHead; while(index--){ if(pos.pNode == NULL){ break; } pos.pNode = pos.pNode->pNext; } return pos; } int GetCount(struct TABLE* pTable) { struct Node* pNode; int count = 0; pNode = pTable->pHead; while(pNode){ count++; pNode = pNode->pNext; } return count; }
此文档下载收益归作者所有
举报原因
联系方式
详细说明
内容无法转码请点击此处