双向链表的C实现

双向链表的C实现

ID:38194777

大小:41.00 KB

页数:5页

时间:2019-05-25

上传者:U-3714
双向链表的C实现_第1页
双向链表的C实现_第2页
双向链表的C实现_第3页
双向链表的C实现_第4页
双向链表的C实现_第5页
资源描述:

《双向链表的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; } 

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
关闭