您的位置:首页 > 理论基础 > 数据结构算法

数据结构之——单链表的实现

2016-03-15 02:22 393 查看
/*
使用嵌套类实现【单链表】
学习要点:、
(1)实现单链表数据结构的定义以及各种操作
(1)区别深拷贝(另外动态分配内存进行拷贝) 和 浅拷贝(直接拷贝赋值)
(2)实现深拷贝构造和深拷贝赋值函数
(3)实现操作运算符的重载
=    (深拷贝赋值)
<<    输出运算符重载:建立全局函数,在类中定义时可声明为friend函数;
friend函数可访问类中private成员

*/

#include<iostream>
using namespace std;

class List{
public:
//构造函数 和  析构函数
List(void) : m_head(NULL){}
~List(){
clear();
}

// 深度拷贝构造  和  深度拷贝赋值
List(const List& that):m_head(NULL){
for(Node* node = that.m_head; node; node = node->m_next){
this->push_back(node->m_data);
}
}
List& operator=(const List& that){
if(this != &that){
List temp = that;
this->m_head  = temp.m_head;
temp.m_head = NULL;
}
return *this;
}

//清空链表
void clear(void){
for(Node* node = m_head, *next; node; node = next){
next = node->m_next;
delete node;
}
m_head = NULL;
}

//是否为空
bool empty(void) const {
return !m_head;
}

//元素个数
size_t size(void)const{
size_t count = 0;
for(Node* node = m_head; node; node = node->m_next){
count++;
}
return count;
}

//得到首元素
int& front(void){
if(empty()){
throw underflow_error("list under flow");
}
return m_head->m_data;
}

const int& front(void) const{
return const_cast<List*>(this)->front();
}

//插入首元素
void push_front(const int& data){
m_head = new Node(data, m_head);
}

//删除首元素
void pop_front(void){
if(empty()){
throw underflow_error("list under flow");
}
Node* temp =  m_head;
m_head = m_head->m_next;
delete temp;
temp = NULL;
}

//得到尾元素
int& back(void){
if(empty()){
throw underflow_error("list under flow");
}
Node* node = m_head;
while(node){
if(!node->m_next){
return node->m_data;
}
node = node->m_next;
}
}
const int& back(void) const {
return const_cast<List*>(this)->back();

}

//插入尾元素
void push_back(const int& data){
Node* node = m_head;
while(node){
if(!node->m_next){
node->m_next = new Node(data, NULL);
return;
}
node = node->m_next;
}
}

//删除尾元素
void pop_back(void){
if(empty()){
throw underflow_error("list under flow");
}

Node* node = m_head;
while(node){
if(!node->m_next->m_next){
Node* temp = node->m_next;
node->m_next = NULL;
delete temp;
temp = NULL;
break;
}
node = node->m_next;
}
}

//删除某个特定的元素
void remove(const int& data){
while(data == m_head->m_data){
this->pop_front();
}
for(Node* node = m_head; node; node = node->m_next){
if(node->m_next && data == node->m_next->m_data){
Node* temp = node->m_next;
node->m_next = node->m_next->m_next;
delete temp;
temp = NULL;
}
}
}

//打印整个链表
friend ostream& operator<<(ostream& os, const List& list){
for(Node* node = list.m_head; node; node = node->m_next){
os << node;
}
return os;
}

private:
class Node{
public:
int   m_data;
Node* m_next;
Node(int data, Node* next = NULL):m_data(data), m_next(next){}
/* 注意:重载输出/入运算符, 要定义全局函数,在类内定义时可定义为friend函数  */
//打印节点
friend ostream& operator<<(ostream& os, const Node& node){
return os << "["<< node.m_data << "]";
}
};
Node* m_head;

};
//  主函数测试
int main(){
List list;
cout << list.size() << endl;
list.push_front(5);
list.push_front(6);
list.push_front(5);
list.push_front(5);
cout << list.empty() << endl;
list.push_back(21);
list.push_back(22);
cout << list.size() << endl;
cout << list.empty() << endl;
cout << "================" << endl;
cout << list.front() << endl;
cout << list.back() <<endl;
list.pop_front();
list.pop_back();
cout << list.size() << endl;
cout << list.empty() << endl;
cout << "================" << endl;
list.remove(5);
cout << list.size() << endl;
cout << list.empty() << endl;
list.clear();
cout << list.size() << endl;
cout << list.empty() << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: