C++实现单链表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++实现单链表相关的知识,希望对你有一定的参考价值。
单链表的C++实现
#include<iostream> using namespace std; typedef int DataType; struct Node//struct在C++中和class公私有属性不同 { Node(const DataType &d) :_data(d) ,_next(NULL) { cout << "Node(const DataType &d)" << endl; } DataType _data; struct Node* _next; }; class SList { friend ostream&operator<<(ostream&os, SList&s); public: SList()//构造 :_head(NULL) , _tail(NULL) { cout << "SList()" << endl; } SList(const SList&s)//拷贝构造 :_head(NULL) , _tail(NULL) { cout << "SList(const SList&s)" << endl; Node*cur = s._head; while (cur) { PushBack(cur->_data); cur = cur->_next; } } ~SList() { cout << "~SList()" << endl; if (_head == NULL) { return; } Node*cur = _head; while (cur->_next != NULL) { Node*del = cur; cur = cur->_next; delete del; } delete cur; _head = NULL; _tail = NULL; } public: void PushBack(const DataType& d);//尾插 void PopBack();//尾删 void PushFront(const DataType&d);//头插 void PopFront();//头删 Node*Find(const DataType&d);//查找 void Insert(Node*pos,const DataType&d);//插入 void Reverse();//反转 void Sort();//排序 void Remove(const DataType&d);//删除某节点 void RemoveAl(const DataType&d);//删除某节点后所有节点 private: Node*_head;//指向头节点 Node*_tail;//指向尾节点 }; ostream&operator<<(ostream&os, SList&s) { Node*cur =s._head; while (cur) { cout << cur->_data << "->"; cur = cur->_next; } cout << "over" << endl; return os; } void SList::PushBack(const DataType&d)//尾插 { Node*newNode = new Node(d); if (_head == NULL)//空链表 { _head = newNode; _tail = newNode; } else { _tail->_next = newNode; _tail = _tail->_next; } } void SList::PopBack() { if (_head == NULL) { return; } if (_head == _tail) { delete _tail; _head = NULL; _tail = NULL; return; } Node*cur = _head; while (cur->_next != _tail)//找到尾节点的前一个结点 { cur = cur->_next; } delete _tail; _tail = cur; _tail->_next = NULL; } void SList::PushFront(const DataType&d) { Node*newNode = new Node(d); if (_head == NULL) { _head = newNode; _tail = newNode; } else { newNode->_next = _head; _head = newNode; } } void SList::PopFront()//头删 { if (_head == NULL)//没有节点 { return; } if (_head = _tail)//只有一个节点 { delete _tail; _head = NULL; _tail = NULL; return; } Node*cur = _head; _head = _head->_next; delete _head; } Node* SList::Find(const DataType&d)//查找 { Node*cur = _head; while (cur) { if (cur->_data == d) { return cur; } cur = cur->_next; } return NULL; } void SList::Insert(Node*pos,const DataType&d)//插入 { Node*newNode = new Node(d); if (pos == _tail) { _tail->_next = newNode; _tail = newNode; } else { newNode->_next = pos->_next; pos->_next = newNode; } } void SList::Reverse()//反转 { if (_head == NULL) return; if (_head == _tail) return; Node*pnewHead = NULL; Node*cur = _head; Node*prev = NULL; _tail = _head; while (cur) { prev = cur; cur = cur->_next; prev->_next = pnewHead; pnewHead = prev; } _head = pnewHead; } void SList::Sort()//排序 { Node*cur = _head; Node*end = NULL; while (cur != end) { while (cur && (cur->_next!=end)) { if ((cur->_data) > (cur->_next->_data)) { DataType tmp = cur->_data; cur->_data = cur->_next->_data; cur->_next->_data = tmp; } cur = cur->_next; } /*if (cur->_next == NULL) { _tail = cur; }*/ end = cur; cur = _head; } } void SList::Remove(const DataType&d)//删除某节点 { Node*cur = _head; if (_head == NULL) return; if (_head->_data == d) { Node*del = _head; _head = del->_next; delete del; return; } while (cur->_next) { if (cur->_next->_data == d) { if (cur->_next == _tail) { delete _tail; _tail = cur; _tail->_next = NULL; return; } Node*del = cur->_next; cur->_next = del->_next; delete del; break; } cur = cur->_next; } } void SList::RemoveAl(const DataType&d)//删除某节点后所有相同节点 { Node*cur = _head; if (_head == NULL) return; if (_head->_data == d) { Node*del = _head; _head = del->_next; delete del; return; } while (cur->_next) { if (cur->_next->_data == d) { if (cur->_next == _tail) { delete _tail; _tail = cur; _tail->_next = NULL; return; } Node*del = cur->_next; cur->_next = del->_next; delete del; } cur = cur->_next; } }
本文出自 “无以伦比的暖阳” 博客,请务必保留此出处http://10797127.blog.51cto.com/10787127/1752032
以上是关于C++实现单链表的主要内容,如果未能解决你的问题,请参考以下文章