双链表的各种操作 创建 插入 删除 查找
来源:互联网 发布:第八届云计算大会 编辑:程序博客网 时间:2024/06/18 00:05
首先定义一下双链表的结点结构
#include<iostream> using namespace std; class Node { public: Node()=default; // c++ 11标准,如果不支持其实就是各种成员值默认都赋0 Node(int val):value(val),pre(nullptr),next(nullptr){} inline Node*& getPre(){ return pre; } // 得到前一个结点 inline Node*& getNext(){ return next; } //得到后一个结点 inline int getValue(){ return value; } //得到当前结点的值 private: Node *pre; Node *next; int value; }; void CreateList(Node *&head); //创建一个双链表 void printLList(Node *head); //打印双链表 void Insert(Node *&head, size_t pos, int val); //插入一个结点 void DeleteByValue(Node *&head,int val); //按给的值删一个元素 void DeleteByPos(Node *&head,size_t position); //按位置删一个元素 Node* SearchByValue(Node *head, int val, size_t *where=0); //按值查找 Node* SearchByPos(Node *head, size_t pos, int *val=0); //按给的位置查找元素 size_t Length(Node *head); //返回链表的长度 void CreateList(Node *&head) { int input; size_t pos=1; cout<<"正在创建链表,输入结点值 (ctrl+z 结束输入)"<<endl; while(cin >> input) Insert(head,pos++,input); cout<<"The list was created!"<<endl<<endl; cin.clear(); return; } void Insert(Node *&head, size_t pos, int val) { size_t length = Length(head); if(pos < 1 || pos > (length+1)) // 插入位置不合法 { cout<<"The given position is illegal!"<<endl<<endl; return; } Node *newNode = new Node(val); if(pos == 1) // 表头插入 { if(head == nullptr) //空表 head = newNode; else //非空表 { head->getPre() = newNode; newNode->getNext() = head; head = newNode; } } else { Node *position = SearchByPos(head,pos-1);; if( pos == (length+1)) // 表尾插入 { position->getNext() = newNode; newNode->getPre() = position; } else // 其他位置插入 { newNode->getNext() = position->getNext(); newNode->getPre() = position; position->getNext()->getPre() = newNode; position->getNext() = newNode; } } return; } size_t Length(Node *head) { size_t len = 0; Node *pos = head; while( pos != nullptr ) { ++len; pos = pos->getNext(); } return len; } void DeleteByValue(Node *&head,int val) { Node* pos = SearchByValue(head,val); if(pos == nullptr) { cout<<val<<" is not found, cannot delete it!"<<endl<<endl; return; } else if(pos == head) // 删表头 head = head->getNext(); else if(pos->getNext() != nullptr) //删其他位置 { pos->getPre()->getNext() = pos->getNext(); pos->getNext()->getPre() = pos->getPre(); } else //删表尾 pos->getPre()->getNext() = nullptr; delete pos; pos = nullptr; cout<<val<<" is deleted!"<<endl<<endl; return; } void DeleteByPos(Node *&head,size_t position) { size_t length = Length(head); if(position < 1 || position > length) { cout<<"The position is illegal, cannot delete it!"<<endl<<endl; return; } Node* pos = SearchByPos(head,position); if(position == 1) // 删除 表头 head = head->getNext(); else { if(pos->getNext() == nullptr) //删除 表尾 pos->getPre()->getNext() = nullptr; else //删其他位置 { pos->getPre()->getNext() = pos->getNext(); pos->getNext()->getPre() = pos->getPre(); } } delete pos; pos = nullptr; cout<<"The value at position "<<position<<" is deleted!"<<endl<<endl; return; } Node* SearchByValue(Node *head, int val, size_t *where) { if(where != 0) ++(*where); Node *pos = head; while(pos != nullptr && val != pos->getValue()) { pos = pos->getNext(); if(where != 0) ++(*where); } if(pos == nullptr) return nullptr; else return pos; } Node* SearchByPos(Node *head, size_t pos, int *val) { size_t length = Length(head); if(pos < 1 || pos > length) return nullptr; Node *position = head; size_t temp = 0; while(position != nullptr && temp < pos-1) { ++temp; position = position->getNext(); } if(position == nullptr) return nullptr; else { if(val != 0) *val = position->getValue(); return position; } } void printLList(Node *head) { if(head == nullptr) cout<<"The list is empty!"<<endl<<endl; else { do { cout<<head->getValue()<<" "; head = head->getNext(); }while(head != nullptr); cout<<endl<<endl; } return; } int main(int argc, char** argv) { Node *temp; size_t pos; int val; Node *head = nullptr; CreateList(head); //创建链表 printLList(head); cout<<"插入, 输入要插入的位置和要插入的值(位置从1开始)"<<endl; cin>>pos>>val; Insert(head,pos,val); printLList(head); pos=0; cout<<"按值查找, 输入要查找的值"<<endl; cin>>val; temp = SearchByValue(head,val,&pos); if(temp == nullptr) cout<<"The value of "<<val<<" was no found!"<<endl<<endl; else cout<<"The value of "<<val<<" was found! Its position is "<<pos<<endl<<endl; val=0; cout<<"按地址查找, 输入要查找的位置"<<endl; cin>>pos; temp = SearchByPos(head,pos,&val); if(temp == nullptr) cout<<"The value at position "<<pos<<" was no found!"<<endl<<endl; else cout<<"The value at position "<<pos<<" was found! Its value is "<<val<<endl<<endl; cout<<"按值删除,输入要删除的值"<<endl; cin>>val; DeleteByValue(head,val); printLList(head); cout<<"按地址删除,输入要删除的位置"<<endl; cin>>pos; DeleteByPos(head,pos); printLList(head); return 0; }
运行样例
0 0
- 双链表的各种操作 创建 插入 删除 查找
- 单链表的创建、插入,删除、查找等操作
- 单向链表的操作:创建,删除,插入,销毁,查找
- 哈希表的常用操作:创建、插入、查找、删除
- Linked List的基本操作(创建/删除/插入/查找)
- 单链表基本操作的实现--创建、插入、查找、删除
- 二叉查找树的各种操作(插入、删除、查找、遍历)
- 各种集合的查找,删除,插入效率
- 顺序表的各种操作(建立,查找,删除,插入等)
- 单链表的各种操作(建立,查找,删除,插入,归并等)
- 关于搜索二叉树的各种操作(插入,删除,输出,查找,递归非递归排序)
- 线段树的创建插入查找删除
- 二叉数组的创建删除插入查找
- 单链表的创建+ 求表长+ 查找+插入+删除
- 二叉排序树的创建,查找,插入,删除
- 数据结构 二叉排序树的创建,查找,插入,删除
- 线性表的基本操作,包括:创建、插入、删除、查找等基本操作
- BST的插入、删除、查找操作
- CentOS中配置国内yum源
- activemq在一台服务器上启动多个Broker
- 欢迎使用CSDN-markdown编辑器
- 使用 xheditor + prettify 做代码高亮的总结
- VC++使用内联汇编的例子:冒泡法排序(从大到小排序)
- 双链表的各种操作 创建 插入 删除 查找
- MyEclipse通过TomCat创建一个简单的Web后台供Android使用
- Python - 单继承与多继承
- Android 百分比布局
- Java的位运算符详解实例——与(&)、非(~)、或(|)、异或(^)
- 第十一周项目8二叉树构造(图示)
- latch: row cache objects:dc_rollback_segments
- 技术点详解---IPSec VPN基本原理
- Java8 lambda表达式是什么?