双链表的实现C++封装
来源:互联网 发布:搜索引擎优化实战培训 编辑:程序博客网 时间:2024/05/18 14:45
双链表的C++实现,先给出主要算法,最后而给出完整的实现。
定义节点
//定义节点typedef class Node{public://数据int data;//前指针class Node *prior;//后指针class Node *next;}ListNode;
头插法插入元素节点
//创建并初始化(头插法)List(int a[], int n){int i;ListNode *p;L = (ListNode *)malloc(sizeof(ListNode));L->next = L->prior = NULL;for (i = 0; i < n; i++){p = (ListNode *)malloc(sizeof(ListNode));p->data = a[i];p->next = L->next;//将p后一个节点地址赋给l->nextif (L->next != NULL){L->next->prior = p;//将L后一节点的前指针赋值为带插入指针地址}p->prior = L;//带插入前指针赋值为LL->next = p;//L后指针赋值为p}}
尾插法插入节点
//尾插法List(int a[], int n){int i;ListNode *p, *s;//初始化表头L = (ListNode *)malloc(sizeof(ListNode));L->prior = NULL;//使p指向尾节点,初始时头结点即为尾节点p = L;//生成新节点,插入元素,链上节点for (i = 0; i < n; i++){s = (ListNode *)malloc(sizeof(ListNode));s->data = a[i];//前指针指向尾结点s->prior = p;//尾指针指向新节点p->next = s;//将新节点变成为节点p = s;}p->next = NULL;}释放双链表
//析构并释放空间~List(){ListNode *pre = L, *p = L->next;while (p != NULL){free(pre);pre = p;p = pre->next;//p指向下下个节点}free(pre);//下个节点}
判断是否为空
//判断是否为空bool isEmpty(){return (L->next == NULL);}返回链表长度
//返回长度int GetLength(){int i = 0;ListNode *p = L;while (p->next != NULL){//p->next为当前节点的指针域i++;p = p->next;}return i;}输出链表元素
//输出链表void DispleyList(){ListNode *p = L->next;while (p != NULL){cout << p->data << ends;p = p->next;}cout << endl;}按元素位置查找并返回值
//按序号查找并返回值bool getElem(int i, int &res){ListNode *p = L;int j = 0;while (j != i&&p->next != NULL){j++;p = p->next;}//讨论退出的情况if (j != i&&p->next == NULL){return false;}else{res = p->data;return true;}}按值查找并返回位置
//按值查找并返回序号int LocateElem(int elem){ListNode *p = L;int i = 0;while (p->next != NULL&&p->data!=elem){i++;p = p->next;}//讨论退出的情况if (p->next == NULL&&p->data != elem){return 0;}else{return i;}}插入新节点
//插入元素void InsertElem(int i, int &res){i--;//找到插入位置的前一个节点int j = 0;ListNode *p = L, *s;while (j != i&&p->next != NULL){j++;p = p->next;}//讨论退出情况if (j != i&&p->next == NULL){cout << "error No." << endl;}else{s = (ListNode *)malloc(sizeof(ListNode));s->data = res;//链上后继节点,当前节点的指针域赋值为p的后继节点s->next = p->next;//插入当前节点,下一节点的前指针指向当前节点p->next->prior = s;//p的后指针域赋值为当前节点p->next = s;//当前节点的前指针赋值为ps->prior = p;}}删除某一节点
//删除元素并返回值bool DeleteElem(int i,int &res){i--;ListNode *p = L, *s;int j = 0;while (j != i&&p->next != NULL){j++;p = p->next;}if (j != i&&p->next == NULL){return false;}else{//记录p的下一个节点的地址s = p->next;//得到数据res = s->data;//p的后指针域存储p的下一个的下一个节点p->next = p->next->next;//p的下一个的下一个节点的前指针域赋值为p的地址p->next->next->prior = p;//释放free(s);return true;}}
以上便是完整的双链表算法C++实现,下面附上完整代码
#include<iostream>using namespace std;const int MAX_N = 100;//定义节点typedef class Node{public://数据int data;//前指针class Node *prior;//后指针class Node *next;}ListNode;class List{public://创建并初始化(头插法)//List(int a[], int n){//int i;//ListNode *p;//L = (ListNode *)malloc(sizeof(ListNode));//L->next = L->prior = NULL;//for (i = 0; i < n; i++){//p = (ListNode *)malloc(sizeof(ListNode));//p->data = a[i];//p->next = L->next;//将p后一个节点地址赋给l->next//if (L->next != NULL){//L->next->prior = p;//将L后一节点的前指针赋值为带插入指针地址//}//p->prior = L;//带插入前指针赋值为L//L->next = p;//L后指针赋值为p//}//}//尾插法List(int a[], int n){int i;ListNode *p, *s;//初始化表头L = (ListNode *)malloc(sizeof(ListNode));L->prior = NULL;//使p指向尾节点,初始时头结点即为尾节点p = L;//生成新节点,插入元素,链上节点for (i = 0; i < n; i++){s = (ListNode *)malloc(sizeof(ListNode));s->data = a[i];//前指针指向尾结点s->prior = p;//尾指针指向新节点p->next = s;//将新节点变成为节点p = s;}p->next = NULL;}//析构并释放空间~List(){ListNode *pre = L, *p = L->next;while (p != NULL){free(pre);pre = p;p = pre->next;//p指向下下个节点}free(pre);//下个节点}//判断是否为空bool isEmpty(){return (L->next == NULL);}//返回长度int GetLength(){int i = 0;ListNode *p = L;while (p->next != NULL){//p->next为当前节点的指针域i++;p = p->next;}return i;}//输出链表void DispleyList(){ListNode *p = L->next;while (p != NULL){cout << p->data << ends;p = p->next;}cout << endl;}//按序号查找并返回值bool getElem(int i, int &res){ListNode *p = L;int j = 0;while (j != i&&p->next != NULL){j++;p = p->next;}//讨论退出的情况if (j != i&&p->next == NULL){return false;}else{res = p->data;return true;}}//按值查找并返回序号int LocateElem(int elem){ListNode *p = L;int i = 0;while (p->next != NULL&&p->data!=elem){i++;p = p->next;}//讨论退出的情况if (p->next == NULL&&p->data != elem){return 0;}else{return i;}}//插入元素void InsertElem(int i, int &res){i--;//找到插入位置的前一个节点int j = 0;ListNode *p = L, *s;while (j != i&&p->next != NULL){j++;p = p->next;}//讨论退出情况if (j != i&&p->next == NULL){cout << "error No." << endl;}else{s = (ListNode *)malloc(sizeof(ListNode));s->data = res;//链上后继节点,当前节点的指针域赋值为p的后继节点s->next = p->next;//插入当前节点,下一节点的前指针指向当前节点p->next->prior = s;//p的后指针域赋值为当前节点p->next = s;//当前节点的前指针赋值为ps->prior = p;}}//删除元素并返回值bool DeleteElem(int i,int &res){i--;ListNode *p = L, *s;int j = 0;while (j != i&&p->next != NULL){j++;p = p->next;}if (j != i&&p->next == NULL){return false;}else{//记录p的下一个节点的地址s = p->next;//得到数据res = s->data;//p的后指针域存储p的下一个的下一个节点p->next = p->next->next;//p的下一个的下一个节点的前指针域赋值为p的地址p->next->next->prior = p;//释放free(s);return true;}}private:ListNode *L;};int main(){int i, n, a[MAX_N], res = 0;cout << "Input length:";cin >> n;for (i = 0; i < n; i++){cin >> a[i];}//构建单链表List m_list(a, n);//输出m_list.DispleyList();//判断是否为空,非空输出长度if (!m_list.isEmpty()){cout << "It is not empty." << endl;cout << "It contained " << m_list.GetLength() << " elements." << endl;}else{cout << "It is empty." << endl;}//按序号查找并返回值cout << "Input a number for querry:";cin >> i;if (m_list.getElem(i, res)){cout << "Location: " << i << " ,element: " << res << endl;}else{cout << "Loaction " << i << " is invalid." << endl;}//按值查找并返回序号cout << "Input a element for querry:";cin >> res;if (m_list.LocateElem(res) == 0){cout << "Not contain this element." << endl;}else{cout << "Element: " << res << " ,No. is :" << m_list.LocateElem(res) << endl;}//插入cout << "Input NO. and element for insert:";cin >> i >> res;m_list.InsertElem(i, res);m_list.DispleyList();//删除cout << "Input NO. for delete:";cin >> i;if (!m_list.DeleteElem(i, res)){cout << "Invalid NO." << endl;}else{cout << "NO. :" << i << " ,Element: " << res << " is deleted." << endl;}m_list.DispleyList();return 0;}
测试运行结果
0 0
- C实现-CPP的-封装
- C语言实现简单的日志封装
- C语言实现封装
- C语言实现封装
- C语言封装成object-c的过程,实现原理
- 双链表的实现C++封装
- 在C中实现对struct内部数据的封装
- C语言实现MAC帧的封装与解封装
- C语言面向对象的实现---封装性
- C语言面向对象的实现---封装性
- C 语言中实现数据与方法的封装
- C 语言中实现数据与方法的封装
- C语言实现C++的封装继承和多态
- C语言是怎样实现封装、继承、多态的?
- 用C语言实现c++的封装、继承、多态
- C语言实现C++的封装继承与多态
- Linux下c实现域名转IP的方法封装
- C语言实现封装,继承
- 会话管理技术Cookie&Session
- uva6195
- CRC-CCITT算法
- Proxy
- 安卓控件使用系列25:Spinner下拉列表控件的使用方法
- 双链表的实现C++封装
- Ubuntu用Wine运行Windows应用程序和卸载
- hdu 3971
- 中兴f402 412等破解超级密码、破解用户限制、关闭远程控制、恢复路由器拨号
- 学习与实践Cadence总结
- Ubuntu 15.04 clang++ 3.6 编译boost 1.59/1.55
- 山峰
- 将矩阵顺时针旋转90度 leetCode:Rotate Image
- Convert Sorted List to Binary Search Tree