- 根据单链表的相关知识,借鉴了单链表C++实现代码_阿大古的博客-CSDN博客_链表c++实现的内容,写了一个C++单链表的实现。
- 目前因为typedef int datatype了,所以是整型的单链表,我改了typedef float datatype也简单试了以下没什么问题,但是改成float后cout那里要设置一下保留几位小数,不然打印出来的结果也可能有点偏差,后续有机会写成模板类。
- 第一次写,可能存在问题,欢迎大家批评指正,我个人比较好奇是否有内存泄漏,或者哪一步有降低复杂度的可能,也请提出意见嘿嘿。
- 函数的命名基本采用了STL中的命名方式,由于没有迭代器,insert()这种函数没办法在最后一位插入新元素,可以用尾插插入。
- 我个人喜欢分文件编写,但是感觉发布出来分文件可能看着不舒服,这次就写在同一个文件里了,代码如下。
#include<iostream> using namespace std; //定义链表存放的数据类型 typedef int datatype; //创建结点类 class Node { public: datatype data; //结点中的数据域 Node* next; //结点中的指针域,结点类型的指针 }; //创建单链表类 class SingleLinkList { public: SingleLinkList();//构造函数 ~SingleLinkList();//析构函数 bool empty();//判断链表是否为空,为空返回1,不为空返回0 int size();//返回链表元素个数 void traverse();//打印遍历整个链表 void clear();//清空链表 void push_front(datatype);//头插 void push_back(datatype);//尾插 void pop_front();//头删 void pop_back();//尾删 void insert(int, datatype);//任意位置插入 void erase(int);//删除任意位置元素 datatype at(int);//返回索引位置数据 datatype operator[](int);//重载[],返回索引位置数据 private: int counter;//链表元素个数 Node* head;//创建头结点指针,结点类型指针,当前未赋值 }; //构造函数 SingleLinkList::SingleLinkList() { this->head = new Node; //给头结点指针实例化一个结点 //this->head->data = 0; //头结点数据,头结点一般不存放数据 this->head->next = NULL; //头结点指针指空 this->counter = 0; //未插入数据前,元素个数为0 } //析构函数 SingleLinkList::~SingleLinkList() { Node* temp; while (this->head) { temp = head->next; delete head; head = temp; } } //判断链表是否为空,为空返回1,不为空返回0 bool SingleLinkList::empty() { if (this->head->next == NULL || this->head == NULL)//(头结点不算做链表中的元素,因此在此若只有头结点也算空链表) { return true; } else { return false; } } //返回链表元素个数 int SingleLinkList::size() { return this->counter; } //打印遍历整个链表 void SingleLinkList::traverse() { Node* temp = this->head->next;//创建一个结点指针遍历每个结点 while (temp) { cout << temp->data << " "; temp = temp->next; } cout << endl; } //清空链表 void SingleLinkList::clear() { if (this->head == NULL || this->head->next == NULL) { return; } else { Node* temp; while (this->head) { temp = head->next; delete head; head = temp; } } } //头插 void SingleLinkList::push_front(datatype newelem) { if (this->head == NULL) { return; } Node* newnode = new Node; newnode->data = newelem; newnode->next = this->head->next; this->head->next = newnode; this->counter = this->counter + 1;//元素个数加一 } //尾插 void SingleLinkList::push_back(datatype newelem) { if (this->head == NULL) { return; } Node* newnode = new Node; Node* temp = head; newnode->data = newelem; newnode->next = NULL; while (temp->next) { temp = temp->next; } temp->next = newnode; this->counter = this->counter + 1;//元素个数加一 } //头删 void SingleLinkList::pop_front() { if (this->head == NULL) { return; } Node* temp = new Node; temp = this->head->next; this->head->next = temp->next; delete temp; this->counter = this->counter - 1;//元素个数减一 } //尾删 void SingleLinkList::pop_back() { if (this->head == NULL) { return; } Node* temp = this->head; Node* temp2 = new Node; for (int i = 1; i < this->counter; i++) { temp = temp->next; } temp2 = temp->next; temp->next = NULL; delete temp2; this->counter = this->counter - 1;//元素个数减一 } //任意位置插入 void SingleLinkList::insert(int pos, datatype newelem) { if (this->head == NULL || pos > this->counter) { return; } Node* newnode = new Node; Node* temp = head; newnode->data = newelem; for (int i = 1; i < pos; i++) { temp = temp->next; } newnode->next = temp->next; temp->next = newnode; this->counter = this->counter + 1;//元素个数加一 } //任意位置删除 void SingleLinkList::erase(int pos) { if (this->head == NULL || pos > this->counter) { return; } Node* temp = this->head; Node* temp2 = new Node; for (int i = 1; i < pos; i++) { temp = temp->next; } temp2 = temp->next; temp->next = temp2->next; delete temp2; this->counter = this->counter - 1;//元素个数减一 } //返回索引位置数据 datatype SingleLinkList::at(int pos) { if (this->head == NULL || pos > this->counter) { return 0; } Node* temp = head; for (int i = 0; i < pos; i++) { temp = temp->next; } return temp->data; } //重载[],返回索引位置数据 datatype SingleLinkList::operator[](int pos) { if (this->head == NULL || pos > this->counter) { return 0; } Node* temp = head; for (int i = 0; i < pos; i++) { temp = temp->next; } return temp->data; } //主函数,测试 int main() { //Map map; //map.build_matrix(3, 6); SingleLinkList lst;//新建链表,实例化 cout << lst.empty() << endl;//判断链表是否为空 for (int i = 0; i < 10; i++) { lst.push_back(i);//尾插0-9十个元素 } for (int i = 10; i < 20; i++) { lst.push_front(i);//头插10-19十个元素 } lst.traverse();//遍历打印表中元素 lst.pop_front();//头删1个元素 lst.pop_back();//尾删1个元素 lst.insert(5, 100);//在第五位插入100 lst.erase(10);//删除第10位元素 lst.traverse();//遍历打印表中元素 cout << lst.size() << endl;//查看表中元素个数 cout << lst.empty() << endl;//判断链表是否为空 cout << lst.at(3) << endl;//查找第3位元素 cout << lst[4] << endl;//查找第4位元素 return 0; }
C++单链表
最新推荐文章于 2024-05-16 21:12:43 发布