循环双链表删除第一个值为x的结点
2017-09-26 21:40
127 查看
#include <iostream> using namespace std; int const NONE = 0; int const DONE = 1; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ class List; //结点 class Node{ friend class List; public: Node(double data, Node *prev = NULL, Node *next = NULL){ this->data = data; this->prev = prev; this->next = next; } private: double data; Node *prev; Node *next; }; //循环双链表 class List{ public: List(); ~List(); bool isEmpty(){ return length == 0; } void addNode(double data); //添加结点 void print(); void deleteX(double x); //删除指定数值节点 private: int length; Node *head; Node *end; Node *p; }; List::List(){ length = 0; //头结点存结点数量 head = new Node(0); end = head; p = NULL; } List::~List(){ Node *temp = NULL; Node *count = head->next; head->next = NULL; while(count){ temp = count; count = count->next; delete temp; } length = 0; head = NULL; end = NULL; temp = NULL; p = NULL; } //添加结点 void List::addNode(double data){ Node *newNode = new Node(data); length ++; head->data = length; if(head == end){ end = newNode; head->next = newNode; head->prev = end; end->prev = head; end->next = head; }else{ end->next = newNode; newNode->prev = end; end = newNode; end->next = head; } newNode = NULL; } void List::print(){ Node *temp = head->next; if(length == 0){ cout<<"空"<<endl; }else{ while(temp != head && temp != NULL){ cout<<temp->data<<'\t'; temp = temp->next; } cout<<endl; temp = NULL; } } void List::deleteX(double x){ //判断是否为空 if(isEmpty()){ cout<<"无法执行删除操作!"<<endl; }else{ int count = NONE; //记录是否找到结点 p = head->next; while(p != head && p != NULL){ if(p->data == x){ count = DONE; length --; head->data = length; if(length == 0){ head->prev = NULL; head->next = NULL; end = head; delete p; p = NULL; }else{ p->next->prev = p->prev; p->prev->next = p->next; delete p; p = NULL; } break; }else{ p = p->next; } } if(count == NONE){ cout<<"未找到要删除的x值!"<<endl; } } } //测试 int main(int argc, char** argv) { double x; List list; list.addNode(5); list.addNode(8); list.addNode(9); list.addNode(5); list.addNode(9); list.addNode(14); list.print(); cout<<"请输入要删除的x值:"<<endl; cin>>x; list.deleteX(x); list.print(); cout<<"请输入要删除的x值:"<<endl; cin>>x; list.deleteX(x); list.print(); cout<<"请输入要删除的x值:"<<endl; cin>>x; list.deleteX(x); list.print(); return 0; }
相关文章推荐
- 给出一个循环双链表,p指向第一个元素值为x的节点,设计算法删除节点*p
- C语言实现非循环双链表节点的删除(不带头结点)
- C语言实现非循环双链表节点的删除(带头结点尾结点)
- 双链表的创建,求长,插入,删除,打印,释放(循环和非循环)
- 循环链表的初始化、插入、删除、返回结点位置、遍历
- 循环双链表的删除、插入、显示
- 【c++版数据结构】之循环双链表的实现(带头结点以及尾节点)
- 设一个没有头结点指针的单链表。一个指针指向此单链表中间的一个结点(不是第一个,也不是最后一个结点),将该结点从单链表中删除,要求时间复杂度O(1)。
- 链表知识点(十)【删除带有头结点的单链表的第一个结点(包含数据的第一个节点)】
- 循环结点的前驱删除操作
- 双向循环链表的建立,插入,删除(不带头结点)
- 写给初学数据结构的同学之(循环双链表基本操作,创建,插入,删除,排序)
- 第十九周项目一:动态链表体验(三):删除链表中的第一个结点
- 给了一串数字:218916754,根据下面规则可以找出扣扣号码:首先删除第一个数,紧接着将第二个数放到这串数字的末尾,再将第三个数删除,并将第四个数放到这串数字的末尾......如此循环,知道剩下最后
- 第十八周项目一(3):删除链表中的第一个结点
- 假设在长度大于1的循环链表中,即无头结点也无头指针,s为指向链表中的某个结点的指针,试编写算法删除结点s的前驱结点
- 数据结构学习(五)——循环双链表的操作之创建,插入、删除
- 删除带头结点的双循环链表p中第i个结点
- 双向循环列表的增加和删除结点
- WV.6-动态链表-删除链表中的第一个结点