C++的STL提供的列表是一种使用双向迭代器的序列存储容器,它能够在序列的任何位置以恒定的时间插入或删除数据元素。此外,存储管理(空间分配和回收)也是由其自动完成的。

双向迭代器意味着列表中使用的迭代器既可以使用增量操作符(前进),也要以使用减量操作符(后退)。

想要利用链表这种数据存储结构,所以完全可以利用STL的列表,而不需要考虑自定义链表。

实例代码:

运行:

请输入字符串,以#结束:

hi,world!#

原始字符串为:

hi,world!

在字符h后面插入c后为:

hci,world!

附代码:

#include #include #include using namespace std;void Showlist(list &llist); //输出列表的函数int main(){ list linklist; list::iterator iter; cout <>temp; if (temp!='#') { linklist.push_back(temp); } }while (temp!='#'); cout < &llist){ list::iterator iter; if (llist.empty()) //判断列表中是否还有元素 { cout <

-End-

查看原文 >>
相关文章