双向链表——基本操作
2016-06-02 13:43
357 查看
"test.c"
"DoubleSListNode.h"
"DoubleSListNode.c"
<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1 #include "DoubleSLishtNode.h" void Test1()//InitDSList PushBack PrintfDSList PopBack { PDoubleSListNode pHead = NULL; InitDSList(&pHead); PushBack(&pHead,0); PushBack(&pHead,1); PushBack(&pHead,2); PushBack(&pHead,3); PrintfDSList(&pHead); PopBack(&pHead); PopBack(&pHead); //PopBack(&pHead); //PopBack(&pHead); //PopBack(&pHead); PrintfDSList(&pHead); } void Test2()//PushFront PopFront { PDoubleSListNode pHead = NULL; InitDSList(&pHead); PushFront(&pHead,0); PushFront(&pHead,1); PushFront(&pHead,2); PushFront(&pHead,3); PrintfDSList(&pHead); PopFront(&pHead); PopFront(&pHead); //PopFront(&pHead); //PopFront(&pHead); //PopFront(&pHead); PrintfDSList(&pHead); } void Test3()//Find Erase { PDoubleSListNode pHead = NULL; InitDSList(&pHead); PushBack(&pHead,0); PushBack(&pHead,1); PushBack(&pHead,2); PushBack(&pHead,3); PrintfDSList(&pHead); //Erase(&pHead,Find(&pHead,0)); //Erase(&pHead,Find(&pHead,2)); //PrintfDSList(&pHead); Insert(&pHead,Find(&pHead,2),10); //Insert(&pHead,Find(&pHead,3),11); //Insert(&pHead,Find(&pHead,3),12); PrintfDSList(&pHead); } int main() { //Test1(); //Test2(); Test3(); system("pause"); return 0; }</span></strong>
"DoubleSListNode.h"
<strong><span style="font-size:18px;">#ifndef __DOUBLESLISTNODE_H__ #define __DOUBLESLISTNODE_H__ #pragma once; #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef int DataType; typedef struct DoubleSListNode { DataType data; struct DoubleSListNode *_LeftNode; struct DoubleSListNode *_RightNode; }DoubleSListNode,*PDoubleSListNode; //初始化双向链表 void InitDSList(PDoubleSListNode* pHead); //打印双向链表 void PrintfDSList(PDoubleSListNode* pHead); //开辟结点内存 PDoubleSListNode ByeNode(DataType data); //尾插法 void PushBack(PDoubleSListNode* pHead,DataType data); //尾删法 void PopBack(PDoubleSListNode* pHead); //头插法 void PushFront(PDoubleSListNode* pHead,DataType data); //头删法 void PopFront(PDoubleSListNode* pHead); //在双向链表中查找一个元素 PDoubleSListNode Find(PDoubleSListNode* pHead,DataType x); //删除pos位置上的结点 void Erase(PDoubleSListNode* pHead,PDoubleSListNode pos); //在双向链表的pos位置插入x void Insert(PDoubleSListNode* pHead,PDoubleSListNode pos,DataType x); #endif//__DOUBLESLISTNODE_H__</span></strong>
"DoubleSListNode.c"
<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1 #include "DoubleSLishtNode.h" void InitDSList(PDoubleSListNode* pHead) { assert(pHead); pHead = NULL; } PDoubleSListNode ByeNode(DataType data) { PDoubleSListNode pNewNode = (PDoubleSListNode)malloc(sizeof(struct DoubleSListNode)); if (pNewNode == NULL) { printf("开辟结点失败\n"); } else { pNewNode->data = data; pNewNode->_LeftNode = NULL; pNewNode->_RightNode = NULL; } return pNewNode; } void PrintfDSList(PDoubleSListNode* pHead) { PDoubleSListNode pNode = NULL; assert(pHead); if (*pHead == NULL) { return; } pNode = *pHead; while (NULL !=pNode) { printf("%d ",pNode->data); pNode = pNode->_RightNode; } printf("\n"); } void PushBack(PDoubleSListNode* pHead,DataType data) { PDoubleSListNode pNode = NULL; PDoubleSListNode pNewNode = NULL; assert(pHead); if (*pHead == NULL) { *pHead = ByeNode(data); } else { pNode = *pHead; while (NULL !=pNode->_RightNode) { pNode = pNode->_RightNode; } pNewNode = ByeNode(data); pNewNode->_RightNode = NULL; pNewNode->_LeftNode = pNode; pNode->_RightNode = pNewNode; } } void PopBack(PDoubleSListNode* pHead) { PDoubleSListNode pPurNode = NULL; PDoubleSListNode pCurNode = NULL; assert(pHead); if (*pHead == NULL) { printf("DoubleSListNode is empty\n"); return; } else if ((*pHead)->_RightNode == NULL) { pPurNode = *pHead; free(pPurNode); pPurNode = NULL; *pHead = NULL; } else { pPurNode = *pHead; while (NULL != pPurNode->_RightNode) { pPurNode = pPurNode->_RightNode; } pCurNode = pPurNode; pPurNode->_LeftNode->_RightNode = NULL; free(pCurNode); pCurNode = NULL; } } void PushFront(PDoubleSListNode* pHead,DataType data) { PDoubleSListNode pNewNode = NULL; assert(pHead); if (*pHead == NULL) { *pHead = ByeNode(data); } else { pNewNode = ByeNode(data); pNewNode->_RightNode = (*pHead); pNewNode->_LeftNode = NULL; *pHead = pNewNode; } } void PopFront(PDoubleSListNode* pHead) { PDoubleSListNode pNode = NULL; assert(pHead); if (*pHead == NULL) { printf("DoubleSListNode is emtpy\n"); return; } //if ((*pHead)->_RightNode = NULL) //{ // pNode = *pHead; // free(pNode); // pNode = NULL; // *pHead = NULL; //} else { if (NULL != (*pHead)->_RightNode) { pNode = *pHead; *pHead = (*pHead)->_RightNode; free(pNode); pNode = NULL; } else { //pNode = *pHead; //free(pNode); //pNode = NULL; //*pHead = NULL; free(*pHead); *pHead = NULL; } } } PDoubleSListNode Find(PDoubleSListNode* pHead,DataType x) { PDoubleSListNode pNode = NULL; assert(pHead); if (*pHead == NULL) { printf("PDoubleSListNode is empty\n"); return *pHead; } pNode = *pHead; while (NULL !=pNode) { if (pNode->data == x) { return pNode; } else { pNode = pNode->_RightNode; } } return NULL; } void Erase(PDoubleSListNode* pHead,PDoubleSListNode pos) { PDoubleSListNode pNode = NULL; assert(pHead); if (pos == NULL) { printf("Erase is error\n"); return; } else { if (pos == *pHead) { //pNode = pos; //pos = pos->_RightNode; //free(pNode); //pNode = NULL; pNode = *pHead; *pHead = (*pHead)->_RightNode; free(pNode); pNode = NULL; } else { pNode = pos; pos->_LeftNode->_RightNode = pos->_RightNode; free(pNode); pNode = NULL; } } } //还没屡清楚,等我弄明白了后面再补上 //void Insert(PDoubleSListNode* pHead,PDoubleSListNode pos,DataType x) //{ // PDoubleSListNode pNode = NULL; // PDoubleSListNode pNewNode = NULL; // assert(pHead); // // if (pos == NULL) // { // printf("no find x\n"); // return; // } // // pNode = *pHead; // while (NULL != pNode) // { // if (pNode == pos) // { // pNewNode = ByeNode(x); // if (pos->_RightNode == NULL) // { // PushBack(pHead,x); // } // else // { // pNewNode->_RightNode = pos->_RightNode; // pos->_RightNode->_LeftNode = pNewNode; // pos->_RightNode = pNewNode; // pNewNode->_LeftNode = pos; // } // } // else // { // pNode = pNode->_RightNode; // } // } //}</span></strong>
相关文章推荐
- 史上最全最强SpringMVC详细示例实战教程
- 查看光纤卡wwn号【转载】
- mysql 安全
- java dom4j解析XML读取数据源配置的代码实例
- (Java程序员)简单了解网站搭建的所处流程
- HTTP基础:URL格式、 HTTP请求、响应、消息
- 实例讲解基于 React+Redux 的前端开发流程
- 解决DataNode Volume Failures故障
- web控件
- h5小游戏——HitRocket
- HDOJ/HDU 1984 Mispelling4(删除第n个字符~)
- HDOJ/HDU 1984 Mispelling4(删除第n个字符~)
- 关于模板页调用js的问题
- MongoDB 3.X 用户权限控制
- 安装提示设备为允许启用的解决办法
- 工具网站
- 大数据架构
- Structural Patterns Part 2/7: Bridge Pattern
- 便捷的宏定义
- Django细谈之:Model和ModelForm关联