STL中的链表
#include<list>
链表有一个迭代器,迭代器可以一个一个的输出数据,功能强大。今天我们也设计链表迭代器。
我们自己设计的链表迭代器
//MyList.h
template<class Type>
class ListIterator{
public:
Boolean NotNull();
Boolean NextNotNull;
Type* First();
Type* Next();
}
在VS2013中新建项目,在头文件中加入MyList.h,在源文件里面加入Main.cpp
#ifndef MYLIST_H
#define MYLIST_H
#include<iostream>
template<class Type> class List;
template<class Type> class ListIterator;
template<class Type>
class ListNode{
friend class List<Type>;
friend class ListIterator<Type>;
private:
Type data;
ListNode *link;
ListNode(Type);//构造函数
};
template<class Type>
class List{
friend class ListIterator<Type>;
public:
List(){ first = 0; };
void Delete(Type);
void Insert(Type);
void Invert();
void Concatenate(List<Type>);
void Show();//测试使用的函数
private:
ListNode<Type>*first;
};
template<class Type>
class ListIterator{
public:
ListIterator(const List<Type>& l) :list(l), current(l.first){};
bool NotNull();
bool NextNotNull();
Type* First();
Type* Next();
private:
const List<Type> &list;
ListNode<Type>* current;
};
template<class Type>
bool ListIterator<Type>::NotNull(){
if (current)return true;
else return false;
}
template<class Type>
bool ListIterator<Type>::NextNotNull(){
if (current && current->link)return true;
else return false;
}
template<class Type>
Type* ListIterator<Type>::First(){
if (list.first)return &list.first->data;
else return 0;
}
template<class Type>
Type* ListIterator<Type>::Next(){
if (current){
current = current->link;
return ¤t->data;
}
else return 0;
}
//插入是往最前面插入
template<class Type>
void List<Type>::Insert(Type k){
ListNode<Type> *newnode = new ListNode<Type>(k);//k是存放节点里面的数据;
newnode->link = first;
first = newnode;
}
template<class Type>
ListNode<Type>::ListNode(Type element){
data = element;
link = 0;
}
template<class Type>
void List<Type>::Show(){
for (ListNode <Type> *current = first; current; current = current->link){
std::cout << current->data;
if (current->link)cout << " -> ";
}
std::cout << std::endl;
}
template<class Type>
void List<Type>::Invert(){
ListNode<Type> *p = first, *q = 0;
while (p){
ListNode<Type> *r = q; q = p;
p = p->link;
q->link = r;
}
first = q;
}
template<class Type>
void List<Type>::Delete(Type k){
ListNode<Type> *previous = 0;//前一个
ListNode<Type> *current;
for (current = first; current && current->data != k; previous = current, current = current->link){
;//什么都不做,空循环,找到要被删除的节点
}
if (current){
if (previous){
previous->link = current->link;
}
else{
first = first->link;
}
delete current;
}
}
template<class Type>
void List<Type>::Concatenate(List<Type>b){
if (!first){ first = b.first; return; }
if (b.first){
ListNode<Type>*p;
for (p = first; p->link; p = p->link);//空循环
p->link = b.first;
}
}
#endif
下面是主程序
#include<iostream>
#include<list>//C++STL中的链表
#include "MyList.h"
using namespace std;
int main(){
cout << "测试:" << endl;
cout << "这是标准C++ STL中的链表和迭代器:" << endl;
std::list<int> listIntegers;
listIntegers.push_front(5);
listIntegers.push_back(15);
listIntegers.push_back(25);
listIntegers.push_back(35);
std::list<int>::iterator i = listIntegers.begin();//返回一个迭代器,指向链表里的第一个数据
while (i != listIntegers.end()){
cout << *i * 10<< "->";
++i;
}
cout << endl;
cout << "这是我的链表和迭代器:" << endl;
List<int> initList;
initList.Insert(5);
initList.Insert(15);
initList.Insert(25);
initList.Insert(35);
ListIterator<int>li(initList);
//迭代是一种手段,可以一个一个的处理数据,想怎么处理就怎么处理
if (li.NotNull()){
cout << *li.First() * 10;
while (li.NextNotNull())
cout << " -> " << *li.Next() * 10;
cout << endl;
}
initList.Show();
initList.Delete(15);
initList.Show();
initList.Delete(20);
initList.Show();
initList.Invert();
initList.Show();
List<char> charList;
charList.Insert('a');
charList.Insert('b');
charList.Insert('c');
charList.Insert('d');
charList.Show();
charList.Invert();
charList.Show();
List<char> char2List;
char2List.Insert('e');
char2List.Insert('f');
char2List.Show();
char2List.Invert();
char2List.Show();
charList.Concatenate(char2List);
charList.Show();
system("pause");
return 0;
}
总结:自己写是链表虽然小,但是五脏俱全,但是真正在开发过程中,一般使用标准C++做好的链表,除非自己要设计有特殊功能的链表。
最后
以上就是义气板栗最近收集整理的关于数据结构与算法(C语言版)__链表的迭代器的全部内容,更多相关数据结构与算法(C语言版)__链表内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复