#include <iostream>
using namespace std;
template <typename T>
class llist{
public:
typedef T _type;
private:
struct node{ // 内嵌结构体,存每个节点的当前值和前后指针
_type data;
node* next;
node* pre;
node(){next=NULL, pre=NULL;}
};
node* head;
node* tail;
size_t _size;
public:
llist() { // 构造函数
head = tail = new node;
_size = 0;
}
~llist() { // 析构函数
node* p = head;
while(p!=NULL) {
node* tem = p;
p = p->next;
delete tem;
}
tail = head;
_size = 0;
}
llist(const llist& li) { // 拷贝构造函数
head = tail = new node;
node* p = head->next;
while (p!=NULL) {
push_back(p->data);
p = p->next;
}
}
llist& operator=(const llist &li) { // 重载赋值运算符
if (&li == this) return *this;
clear();
head = tail = new node;
node* p = li.head->next;
while (p != NULL) {
push_back(p->data);
p = p->next;
}
//return *this;
}
void clear() { // 清空
node* p = head->next;
while (p != NULL) {
node* tem = p;
p = p->next;
delete tem;
}
_size = 0;
tail = head;
}
size_t size() const { // 返回链表大小
return _size;
}
bool empty() const{ // 判断链表是否为空
return _size==0;
}
_type front() const{ // 返回表头
return head->next->data;
}
_type back() const { // 返回表尾
return tail->data;
}
void push_back(const _type& val) { // 在尾部插入一个节点
node* res = new node;
res->data = val;
res->next = NULL;
res->pre = tail;
tail->next = res;
tail = res;
_size++;
}
void pop_back() { // 在尾部删除一个节点
if(empty()) return;
node* tem = tail->pre;
delete tail;
tem->next = NULL;
tail = tem;
_size--;
}
void push_front(const _type& val) { // 在头部插入一个节点
node* res = new node;
res->data = val;
res->next = head->next;
res->pre = head;
if(head->next == NULL) tail = res;
else head->next->pre = res;
head->next = res;
_size++;
}
void pop_front() { // 在头部删除一个节点
if(empty()) return;
node* tem = head->next;
head->next = tem->next;
if(tem->next == NULL) tail = head;
else tem->next->pre = head;
delete tem;
_size--;
}
void Print()const{ // 正序逆序分别打印链表
node* p = head->next;
while(p!=NULL) {
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
p = tail;
while(p!=head) {
cout<<p->data<<" ";
p = p->pre;
}
cout<<endl;
}
class iterator { // 迭代器
node* res;
public:
iterator(node* x){res=x;}
_type& operator*() { // 重载运算符
return res->data;
}
_type* operator->() {
return &res->data;
}
iterator operator++() {
res = res->next;
return *this;
}
iterator operator--() {
res = res->pre;
return *this;
}
iterator operator++(int) {
iterator tem = res;
res = res->next;
return tem;
}
iterator operator--(int) {
iterator tem = res;
res = res->pre;
return tem;
}
bool operator == (const iterator& it) const {
return res == it.res;
}
bool operator != (const iterator& it) const { // 重载运算符
return res != it.res;
}
};
iterator begin() { // 返回第一个节点的迭代器
return iterator(head->next);
}
iterator end() {
return iterator(tail->next); // 返回最后一个节点的next节点
}
};
int main() {
llist<string> li;
string n;
for(int i = 1;i <= 5;i++) {
cin>>n;
li.push_back(n); // 插入节点
}
llist<string>::iterator it = li.begin();
while(it!=li.end()){
cout<<*it<<" ";
++it; // 测试自增运算符
}
cout<<endl;
for(auto x:li) {
cout<<x<<" ";
}
cout<<endl;
llist<string> li2 = li;
llist<string> li3;
li3 = li;
for(auto n:li3){
cout << n << " ";
}
cout << endl;
li.Print();
while(!li.empty()){
// li.pop_front();
li.pop_back(); //测试尾删函数
li.Print();
}
return 0;
}
C++手写list
最新推荐文章于 2024-04-23 17:37:57 发布