C++实现单链表
2017-07-22 23:16
701 查看
链表相对于顺序表,有一个指向关系,当前节点的指针指向下一个节点,单链表是单向指向关系。此外,还有双链表,是双向指向关系。
链表主要实现数据的增删查改。具体代码实现如下:
测试用例:
主函数
链表主要实现数据的增删查改。具体代码实现如下:
//C++实现单链表 #pragma once #include<iostream> #include<assert.h> using namespace std; typedef int DataType; struct SListNode { SListNode* _next; DataType _data; SListNode(DataType x) :_data(x) ,_next(NULL) {} }; class SList { typedef SListNode Node; public: SList() :_head (NULL) ,_tail(NULL) {} //s1(s) SList(const SList& s) :_head(NULL) ,_tail(NULL) { Node* cur = s._head; while(cur) { PushBack(cur->_data); cur = cur->_next; } } //s=s1 SList& operator=(const SList& s) { if(this != &s)//先销毁空间,再拷贝 { Destroy(); Node* cur = s._head; while(cur) { this->PushBack(cur->_data); cur = cur->_next; } } return *this; } ~SList() { Destroy(); } void PushBack(DataType x) 4000 { if(_head == NULL) _head = _tail = new Node(x); else { Node* tmp = new Node(x); _tail->_next = tmp; _tail = tmp; } } void PopBack() { if(_head == NULL)//空节点 return; else if(_head == _tail)//1个节点 { delete _head; _head = _tail = NULL; } else //多个节点 { Node* tailPrev = _head; while(tailPrev->_next->_next) { tailPrev = tailPrev->_next; } delete _tail; _tail = tailPrev; _tail->_next = NULL; } } void PushFront(DataType x) { if(_head == NULL) _head = _tail = new Node(x); else { Node* tmp = new Node(x); tmp->_next = _head; _head = tmp; } } void PopFront() { if(_head == NULL) return; else if(_head == _tail) { delete _head; _head = _tail = NULL; } else { Node* del = _head; _head = _head->_next; delete del; } } // 插入一个节点在pos的前面 void Insert(Node* pos, DataType x) { assert(pos); if(pos == _head) { Node* cur = new Node(x); cur->_next = pos; _head = cur; //PushFront() } else { Node* tmp = new Node(x); Node* prev = _head; while(prev->_next!=pos) { prev = prev->_next; } prev->_next = tmp; tmp->_next = pos; } } void Erase(Node* pos) { assert(pos); if(pos == _head)//PopFront() { Node* del = _head; _head = _head->_next; delete del; del = NULL; } else if(pos == _tail)//PopBack() { Node* del = _tail; Node* prev = _head; while(prev->_next!=_tail) { prev = prev->_next; } prev->_next = del; delete del; del = NULL; _tail = prev; } else //pos位于中间 { Node* del = pos; Node* prev = _head; while(prev->_next!=pos) { prev = prev->_next; } prev->_next = pos->_next; delete del; del = NULL; } } Node* Find(DataType x) { Node* cur = _head; while(cur) { if(cur->_data == x) return cur; cur = cur->_next; } return NULL; } void Print() { Node* cur = _head; while(cur) { cout<<cur->_data<<" "; cur = cur->_next; } cout<<endl; } void Destroy() { if(_head == NULL) return; else { Node* cur = _head; while(cur) { Node* del = cur; cur = cur->_next; delete del; } } _head = _tail = NULL; } private: Node* _head; Node* _tail; };
测试用例:
void TestSList1() { SList l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); l1.Print(); SList l2(l1); SList l3; l3 = l1; l2.Print(); l3.Print(); l1.PopBack(); l1.PopBack(); l1.PopBack(); l1.PopBack(); l1.PopBack(); l1.Print(); } void TestSList2() { SList l1; l1.PushFront(1); l1.PushFront(2); l1.PushFront(3); l1.PushFront(4); l1.Print(); l1.PopFront(); l1.PopFront(); l1.PopFront(); l1.PopFront(); l1.PopFront(); l1.Print(); } void TestSList3() { SList l1; l1.PushFront(1); l1.PushFront(2); l1.PushFront(3); l1.PushFront(4); l1.Print(); SListNode* pos = l1.Find(3); l1.Insert(pos,33); l1.Print(); l1.Erase(pos); l1.Print(); SListNode* head = l1.Find(4); //l1.Insert(head,11); //l1.Print(); l1.Erase(head); l1.Print(); }
主函数
#define _CRT_SECURE_NO_WARNINGS 1 //#include"Seqlist.h" #include"SList.h" int main() { //TestSeqList1(); //TestSeqList2(); //TestSList1(); //TestSList2(); TestSList3(); return 0; }
相关文章推荐
- C++实现链表
- 用c++实现单向链表的创建,插入和删除
- 经典算法与数据结构的c++实现——带头结点的单链表
- C++实现双向链表
- 单向链表之c++实现二
- C++使用单指针Struct实现双向链表
- 链表C++模板实现
- C++实现单链表按k值重新排序的方法
- 链表java与c++实现
- 使用C++实现单向链表
- C++链表的创建和插入删除的实现
- 学生管理系统c++链表实现
- C++实现复杂链表的复制
- linkList C++简单链式链表的实现
- C++实现 简单 单链表
- C++ 实现链表的基本操作之一:链表插入
- (C++版)链表(一)——实现单向链表创建、插入、删除等相关操作
- 【C++】链表实现约瑟夫环
- 数据结构 链表的lua实现 仿照C++中list 实现
- (C++版)链表(三)——实现双向链表的创建、插入、删除等简单操作