C++实现单链表(类和对象)——简单实现
2016-08-22 16:04
513 查看
"test.cpp"
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include "LinkList.h"
void Test()
{
LinkList list;
for (int i = 1;i < 6;i++)
{
list.PushBack(i);
}
cout<<list<<endl;
list.PushFrond(10);
list.PushFrond(9);
list.PushFrond(8);
cout<<list<<endl;
list.PopBack();
list.PopBack();
cout<<list<<endl;
list.PopFrond();
list.PopFrond();
cout<<list<<endl;
LinkListNode* cur = list.Find(9);
if (cur != NULL)
{
cout<<*cur<<endl;
}
list.Insert(list.Find(3),4);
cout<<list<<endl;
list.Insert(list.Find(1),100);
cout<<list<<endl;
list.Insert(list.Find(10),11);
cout<<list<<endl;
list.Remove(list.Find(4));
cout<<list<<endl;
list.Remove(list.Find(10));
cout<<list<<endl;
list.Remove(list.Find(100));
cout<<list<<endl;
}
int main()
{
Test();
system("pause");
return 0;
}
"LinkList.h"
#pragma once
typedef int DataType;
struct LinkListNode
{
LinkListNode(DataType data)
:_data(data)
,_next(NULL)
{}
DataType _data;
LinkListNode* _next;
};
class LinkList
{
friend ostream& operator<<(ostream& os,LinkList& list);
typedef LinkListNode Node;
public:
LinkList()
:_head(NULL)
,_tail(NULL)
{}
~LinkList()
{
Node* cur = _head;
while (cur)
{
Node* tmp = cur;
cur = cur->_next;
delete tmp;
tmp = NULL;
}
}
public:
void PushBack(const DataType data)
{
if ((_head == NULL) && (_tail == NULL))
{
_head = new Node(data);
_tail = _head;
}
else
{
Node* tmp = new Node(data);
_tail->_next = tmp;
_tail = _tail->_next;
}
}
void PushFrond(const DataType data)
{
Node* cur = new Node(data);
if ((_head == NULL) && (_tail == NULL))
{
_head = cur;
_tail = _head;
}
else
{
cur->_next = _head;
_head = cur;
}
}
void PopBack()
{
if ((_head == NULL) && (_tail == NULL))
{
cout<<"链表已空"<<endl;
return;
}
else
{
Node* del = _head;
Node* delprev = NULL;
while (del != _tail)
{
delprev = del;
del = del->_next;
}
_tail = delprev;
_tail->_next = NULL;//尾指针的下一个位置一定要记得置空,否则程序中断
delete del;
del = NULL;
}
}
void PopFrond()
{
if ((_head == NULL) && (_tail == NULL))
{
cout<<"链表已空"<<endl;
}
else
{
Node* del = _head;
_head = _head->_next;
delete del;
del = NULL;
}
}
Node* Find(const DataType data)
{
if ((_head == NULL) && (_tail == NULL))
{
cout<<"List is Null"<<endl;
return NULL;
}
else
{
Node* cur = _head;
while (cur)
{
if (cur->_data == data)
{
return cur;
}
else
{
cur = cur->_next;
}
}
}
return NULL;
}
//在某一个节点的后面插入一个节点
void Insert(Node* cur,const DataType data)
{
Node* tmp = new Node(data);
if (cur == NULL)
{
_head = tmp;
_tail = _head;
}
//这个节点是尾结点的情况
if (cur == _tail)
{
cur->_next = tmp;
_tail = tmp;
}
else
{
tmp->_next = cur->_next;
cur->_next = tmp;
}
}
//删除当前节点
void Remove(Node* cur)
{
if (cur == NULL)
{
cout<<"no find"<<endl;
return;
}
Node* tmp = _head;
if (cur == _head)
{
Node* del = _head;
_head = _head->_next;
delete del;
del = NULL;
}
else if (cur == _tail)
{
while (tmp->_next != cur)
{
tmp = tmp->_next;
}
_tail = tmp;
_tail->_next = NULL;
delete cur;
cur = NULL;
}
else
{
//找到删除节点的前一个位置
while (tmp->_next == cur)
{
tmp = tmp->_next;
}
tmp->_next = cur->_next;
delete cur;
cur = NULL;
}
}
private:
Node* _head;//头指针
Node* _tail;//尾指针
};
ostream& operator<<(ostream& os,LinkListNode& node)
{
os<<node._data<<" ";
return os;
}
ostream& operator<<(ostream& os,LinkList& list)
{
LinkListNode* cur = list._head;
while (cur)
{
os<<*cur<<" ";
cur = cur->_next;
}
return os;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include "LinkList.h"
void Test()
{
LinkList list;
for (int i = 1;i < 6;i++)
{
list.PushBack(i);
}
cout<<list<<endl;
list.PushFrond(10);
list.PushFrond(9);
list.PushFrond(8);
cout<<list<<endl;
list.PopBack();
list.PopBack();
cout<<list<<endl;
list.PopFrond();
list.PopFrond();
cout<<list<<endl;
LinkListNode* cur = list.Find(9);
if (cur != NULL)
{
cout<<*cur<<endl;
}
list.Insert(list.Find(3),4);
cout<<list<<endl;
list.Insert(list.Find(1),100);
cout<<list<<endl;
list.Insert(list.Find(10),11);
cout<<list<<endl;
list.Remove(list.Find(4));
cout<<list<<endl;
list.Remove(list.Find(10));
cout<<list<<endl;
list.Remove(list.Find(100));
cout<<list<<endl;
}
int main()
{
Test();
system("pause");
return 0;
}
"LinkList.h"
#pragma once
typedef int DataType;
struct LinkListNode
{
LinkListNode(DataType data)
:_data(data)
,_next(NULL)
{}
DataType _data;
LinkListNode* _next;
};
class LinkList
{
friend ostream& operator<<(ostream& os,LinkList& list);
typedef LinkListNode Node;
public:
LinkList()
:_head(NULL)
,_tail(NULL)
{}
~LinkList()
{
Node* cur = _head;
while (cur)
{
Node* tmp = cur;
cur = cur->_next;
delete tmp;
tmp = NULL;
}
}
public:
void PushBack(const DataType data)
{
if ((_head == NULL) && (_tail == NULL))
{
_head = new Node(data);
_tail = _head;
}
else
{
Node* tmp = new Node(data);
_tail->_next = tmp;
_tail = _tail->_next;
}
}
void PushFrond(const DataType data)
{
Node* cur = new Node(data);
if ((_head == NULL) && (_tail == NULL))
{
_head = cur;
_tail = _head;
}
else
{
cur->_next = _head;
_head = cur;
}
}
void PopBack()
{
if ((_head == NULL) && (_tail == NULL))
{
cout<<"链表已空"<<endl;
return;
}
else
{
Node* del = _head;
Node* delprev = NULL;
while (del != _tail)
{
delprev = del;
del = del->_next;
}
_tail = delprev;
_tail->_next = NULL;//尾指针的下一个位置一定要记得置空,否则程序中断
delete del;
del = NULL;
}
}
void PopFrond()
{
if ((_head == NULL) && (_tail == NULL))
{
cout<<"链表已空"<<endl;
}
else
{
Node* del = _head;
_head = _head->_next;
delete del;
del = NULL;
}
}
Node* Find(const DataType data)
{
if ((_head == NULL) && (_tail == NULL))
{
cout<<"List is Null"<<endl;
return NULL;
}
else
{
Node* cur = _head;
while (cur)
{
if (cur->_data == data)
{
return cur;
}
else
{
cur = cur->_next;
}
}
}
return NULL;
}
//在某一个节点的后面插入一个节点
void Insert(Node* cur,const DataType data)
{
Node* tmp = new Node(data);
if (cur == NULL)
{
_head = tmp;
_tail = _head;
}
//这个节点是尾结点的情况
if (cur == _tail)
{
cur->_next = tmp;
_tail = tmp;
}
else
{
tmp->_next = cur->_next;
cur->_next = tmp;
}
}
//删除当前节点
void Remove(Node* cur)
{
if (cur == NULL)
{
cout<<"no find"<<endl;
return;
}
Node* tmp = _head;
if (cur == _head)
{
Node* del = _head;
_head = _head->_next;
delete del;
del = NULL;
}
else if (cur == _tail)
{
while (tmp->_next != cur)
{
tmp = tmp->_next;
}
_tail = tmp;
_tail->_next = NULL;
delete cur;
cur = NULL;
}
else
{
//找到删除节点的前一个位置
while (tmp->_next == cur)
{
tmp = tmp->_next;
}
tmp->_next = cur->_next;
delete cur;
cur = NULL;
}
}
private:
Node* _head;//头指针
Node* _tail;//尾指针
};
ostream& operator<<(ostream& os,LinkListNode& node)
{
os<<node._data<<" ";
return os;
}
ostream& operator<<(ostream& os,LinkList& list)
{
LinkListNode* cur = list._head;
while (cur)
{
os<<*cur<<" ";
cur = cur->_next;
}
return os;
}
相关文章推荐
- 单链表的简单c++实现
- 单链表c++简单模板实现
- 一个单链表C++简单的实现版本-转自chinaunix
- C++初学初练类与对象之矩阵简单功能实现
- 用C++简单实现单链表
- C++简单实现顺序表和单链表
- C++简单实现对象引用计数示例
- C++对象动态生成(Dynamic Create)的简单实现
- 必须要注意的 C++ 动态内存资源管理(二)——指针对象简单实现
- C++ 单链表简单实现
- C++对象动态生成(Dynamic Create)的简单实现
- 单链表c++简单模板实现
- C++设计模式 --单例模式(简单的实现和对象释放的控制)
- (C++)设计模式------单例模式 singleton(简单的实现和对象释放的控制) -- 创建型模式
- C++简单实现对象引用计数示例(转)
- C和C++的面向对象实现机制比较
- 标准纯C++实现简单的词法分析器(二)
- C++对象及多态实现探索之内存布局
- 《面向对象基础:C++实现》学习笔记之四
- C++ RTTI: 2个宏实现“根据对象名创建对象”