STL_迭代器_单向链表

7 篇文章 0 订阅
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
template<class T>
class List{
public:
	struct node{
		T data;
		node* next;
		node(){ next = NULL;}
	};
	node *head, *tail;
	List(){ head = new node, tail = NULL;}
    node* begin(){ return head->next;}
    node* end(){ return NULL;}
	void push_front(T data){
		node *p = new node;
		p->data = data, p->next = head->next, head->next = p; 
		if(tail == NULL)
			tail = p;
	}
	void push_back(T data){
		node *p = new node;
		p->data = data;
		if(tail == NULL) {
			tail = p; 
			head->next = tail;
		}
		else
			tail->next = p, tail = p;
	}
	void pop_front(){
		if(head->next == NULL){
			puts("runtime error");
			return;
		}
		node* p = head->next;
		head->next = p->next;
		delete p;
		if(head->next == NULL)tail = NULL;
	}
	T front(){
		if(head->next == NULL){
			puts("runtime error");
			exit(0);
		}
		return head->next->data;
	}
	T back(){
		if(tail == NULL){
			puts("runtime error");
			exit(0);
		} 
		return tail->data;
	}
	void print(){
		node *L = head->next;
		while(L){
			cout << L->data << endl;
			L = L->next; 
		}
	}
	void clear(){
		node* L = head->next;
		while(L){
			node* p = L;
			L = L->next;
			delete p;
		}
		head->next = NULL;
		tail = NULL;
	}
	virtual ~List(){clear(); delete head;}

    struct iterator{
        node *p;
        iterator(){p = NULL;}
        iterator(node* q){ p = q;}
        node* operator ++(){p = p->next; return p;}
        iterator& operator = (node* q){p = q; return *this;}
        bool operator != (node* q){ return p != q;}
        bool operator ==(node* q){ return p == q;}
        T operator *(){ return p->data;}
    };
};

int main(){
	List<int>L;
	for(int i = 0; i < 5; i++) L.push_front(i);
	for(int i = 0; i < 5; i++) L.push_back(i+5);
//	for(int i = 0; i < 20; i++) L.pop_front();//pop的太多
	for(int i = 0; i < 5; i++) L.push_front(i);

    List<int>::iterator it;
    for(it = L.begin(); it != L.end(); ++it){
        cout << *it << " ";
    }
    puts("");
    for(List<int>::iterator iter = L.begin(); iter != L.end(); ++iter){
        cout << *iter << " ";
    }    
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(Standard Template Library)是C++标准库的一部分,它提供了一系列高效、灵活的数据结构和算法,用于处理动态数据。在STL中,常见的容器主要包括以下几种: 1. **序列容器**(Sequence Containers): - `std::vector`:动态数组,支持随机访问。 - `std::deque`:双端队列,可以在两端进行高效的插入和删除操作。 - `std::list`:双向链表,元素按插入顺序排列,但查找效率较低。 - `std::forward_list`:单向链表,类似于`list`,但不支持在任意位置插入或删除。 - `std::array`:固定大小的数组,类似C语言中的数组。 2. **关联容器**(Associative Containers): - `std::map`(或`std::unordered_map`):关联键值对,使用哈希表实现高效查找。 - `std::set`(或`std::unordered_set`):无序的键集合,不允许重复。 - `std::multiset`:有序的键集合,允许重复。 - `std::multimap`:关联键值对的多值集合,允许多个键对应同一值。 3. **堆容器**(Priority Container): - `std::priority_queue`:堆数据结构,常用于实现优先级队列。 4. **集合容器**(Set-like Containers): - `std::set`:无序集合,使用哈希表实现。 - `std::unordered_set`:无序且无重复的集合。 5. **容器适配器**(Container Adapters): - `std::stack`:栈,基于`vector`或`deque`实现。 - `std::queue`:队列,同样基于`vector`或`deque`实现。 - `std::bitset`:位集,表示一系列二进制位。 STL迭代器是一种抽象概念,它是容器和算法之间通用的接口,使得我们能够遍历容器中的元素,而不必关心底层的具体实现细节。迭代器提供了读取和修改容器元素的方法,可以指向容器的开始、结束和中间位置。无论是序列还是关联容器,都有相应的迭代器类型,如`iterator`和`const_iterator`等,分别用于读写操作。迭代器的生命周期管理也非常重要,确保它们不会超出容器的有效范围。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值