数据结构之单链表——C++模板类实现
来源:互联网 发布:印刷排版用什么软件 编辑:程序博客网 时间:2024/06/06 02:11
单链表定义
#ifndef SinglyLinkedListEDLIST_H_INCLUDED#define SinglyLinkedListEDLIST_H_INCLUDED#include <bits/stdc++.h>using namespace std;template<class T>class LinkNode {public: LinkNode(LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by default way!" << endl; link = ptr;} LinkNode(const T& item, LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by defined way!" << endl; data = item, link = ptr;} ~LinkNode() {cout << "Desctructing a LineNode!" << endl; delete link;} T data; // Data Scope LinkNode<T> *link; // Point Scope, Point to next Node};// SinglyLinkedListList with head nodetemplate <class T>class SinglyLinkedList : public LinkNode<T> {public: SinglyLinkedList() {cout << "Constructing a SinglyLinkedListList by default way" << endl; first = new LinkNode<T>;} SinglyLinkedList(const T&x); SinglyLinkedList(SinglyLinkedList<T>& L); // Deep copy constructor function ~SinglyLinkedList() {cout << "Destructing a SinglyLinkedListNode by default way" << endl; delete first;} void makeEmpty(); // Delete all nodes in the SinglyLinkedList int Length() const; // Get the length of the SinlyLinkedList LinkNode<T> *GetHead() const {return first;} // Return first LinkNode<T> *Search(T x); // x can only be a variable while T& x LinkNode<T> *Locate(int i); // Get the i-th node's address bool GetData(int i, T& x); // Get the data in i-th node and let x = data void SetData(int i, T& x); // Make the data in i-th node equals x bool Insert(int i, T& x); // Insert a node in i-th with its' data be x bool Remove(int i, T& x); // Delete the i-th node and make x = node[i]->data bool IsEmpty() const {return first->link == NULL ? true : false;} bool IsFull() const {return false;} void Sort(); // Sort all datas in the SinglyLinkedList by non-decreasing way void Input(); // Input all elements void Output(); // Out all elements SinglyLinkedList<T>& operator=(SinglyLinkedList<T>& L); // Overloading operator =protected: LinkNode<T> *first; // Point to Head node};#endif // SinglyLinkedListEDLIST_H_INCLUDED
单链表实现
#include <bits/stdc++.h>#include "SinglyLinkedList.h"using namespace std;template<class T>SinglyLinkedList<T>::SinglyLinkedList(const T&x) { first = new LinkNode<T>; if(first == NULL) { cerr << "Invalid allocation " << endl; } first->link->data = x;}template<class T>SinglyLinkedList<T>::SinglyLinkedList(SinglyLinkedList<T>& L) { // Deep copy constructor function LinkNode<T> *destptr = first = new LinkNode<T>; LinkNode<T> *srcptr = L.GetHead(); while(srcptr->link != NULL){ destptr->link = srcptr->link; destptr->link->data = srcptr->link->data; destptr = destptr->link; srcptr = srcptr->link; }}template<class T>void SinglyLinkedList<T>::makeEmpty() { // Cannot delete first due to is the head in order to make some operation like insert more easy LinkNode<T> *current; while(first->link != NULL) { current = first->link; first->link = current->link; delete current; }}template<class T>int SinglyLinkedList<T>::Length() const { int cnt = 0; LinkNode<T> *current = first; while(current->link != NULL) { cnt++; current = current->link; } return cnt;}template<class T>LinkNode<T>* SinglyLinkedList<T>::Search(T x) { // x can only be a variable while T& x LinkNode<T> *current = first; while(current->link != NULL) { if(current->link->data == x) { return current; } current = current->link; } return NULL; // Not found}template<class T>LinkNode<T>* SinglyLinkedList<T>::Locate(int i) { // Return the address of i-th node if(i < 0) { cerr << "Invalid Location of " << i << endl; return NULL; } LinkNode<T> *current = first; int k = 0; while(current != NULL && k < i) { current = current->link; k++; } return current; // Return NULL while i is too large}template<class T>bool SinglyLinkedList<T>::GetData(int i, T& x) { if(i <= 0) { cerr << "Invalid index of " << i << endl; return false; } LinkNode<T> *current = Locate(i); if(current == NULL) { return false; } x = current->data; return true;}template<class T>void SinglyLinkedList<T>::SetData(int i, T& x) { if(i <= 0) { cerr << "Invalid index of " << i << endl; return ; } LinkNode<T> *current = Locate(i); if(current == NULL) return ; current->data = x;}template<class T>bool SinglyLinkedList<T>::Insert(int i, T& x) { // Insert a node behind i-th node if(i <= 0) { cerr << "Invalid index of " << i << endl; return false; } LinkNode<T> *current = Locate(i-1); if(current == NULL) { return false; } LinkNode<T> *newLinkNode = new LinkNode<T>(x); if(newLinkNode == NULL) { cerr << "Invalid allocation " << endl; return false; } newLinkNode->link = current->link; current->link = newLinkNode; return true;}template<class T>bool SinglyLinkedList<T>::Remove(int i, T& x) { // Remove the i-th node is the SinglyLinkedList if(i <= 0) { cerr << "Invalid position " << endl; } LinkNode<T> *current = Locate(i-1); if(current == NULL || current->link == NULL) { return false; // The SinglyLinkedList is too short } LinkNode<T> *del = current->link; x = del->data; current->link = del->link; delete del; return true;}template<class T>void SinglyLinkedList<T>::Sort() { // Sort all datas in the SinglyLinkedList by non-decreasing way LinkNode<T> *p = first->link; LinkNode<T> *q; T value; while(p->link != NULL) { q = p->link; while(q != NULL) { if(q->data < p->data) { value = p->data; p->data = q->data; q->data = value; } q = q->link; } p = p->link; }}template<class T>void SinglyLinkedList<T>::Input() { int cnt; cout << "Please enter the total nodes of the SinglyLinkedListedList" << endl; while(true) { cin >> cnt; if(cnt <= 0) { cerr << "Invalid length of SinglyLinkedListedList, the length must be a positive interger " << endl; } else break; } LinkNode<T> *current = first; T val; for(int i = 1; i <= cnt; ++i) { cin >> val; current->link = new LinkNode<T>(val); current = current->link; }}template<class T>void SinglyLinkedList<T>::Output() { LinkNode<T> *current = first; int cnt = 1; while(current->link != NULL) { cout << "#No. " << cnt++ << " " << current->link->data << endl; current = current->link; }}template<class T>SinglyLinkedList<T>& SinglyLinkedList<T>::operator=(SinglyLinkedList<T>& L) { LinkNode<T> *srcptr, *destptr; destptr = first = new LinkNode<T>; srcptr = L.GetHead(); while(srcptr->link != NULL) { destptr->link = srcptr->link; destptr->link->data = srcptr->link->data; destptr = destptr->link; srcptr = srcptr->link; } destptr->link = NULL; return *this;}int main(){ int a = 10; SinglyLinkedList<int> SinglyLinkedListList; if(SinglyLinkedListList.IsEmpty()) { cout << "Jesus Christ, is empty!" << endl; } SinglyLinkedListList.Input(); cout << "Length: " << SinglyLinkedListList.Length() << endl; SinglyLinkedListList.Sort(); SinglyLinkedListList.Output(); int num = 284; SinglyLinkedListList.Insert(1, num); SinglyLinkedListList.Insert(2, num); SinglyLinkedListList.Insert(3, num); SinglyLinkedListList.Output(); cout << "\n----------------------------\n"; SinglyLinkedListList.Sort(); SinglyLinkedListList.Output(); SinglyLinkedListList.GetData(1, a); cout << "a: " << a << endl; cout << "Length before remove the first element is: " << SinglyLinkedListList.Length() << endl; SinglyLinkedListList.Remove(1, a); cout << "a: " << a << endl; cout << "Length after remove the first element is: " << SinglyLinkedListList.Length() << endl; SinglyLinkedListList.Output(); SinglyLinkedListList.makeEmpty(); if(SinglyLinkedListList.IsEmpty()) { cout << "Jesus Christ, is Empty!" << endl; } return 0;}
0 0
- 数据结构之单链表——C++模板类实现
- 数据结构之单链表——C++模板类实现
- 数据结构之图——模板实现
- 数据结构之顺序表——C++模板类实现
- 数据结构—类模板实现链表
- 数据结构之单链表C++(模板)
- [数据结构]线性表之单链表的类模板实现
- 数据结构之 单链表 模板类
- 数据结构之栈(链式存储)——C++模板类实现
- 数据结构之循环队列(顺序表存储)——C++模板类实现
- 数据结构(C++)----单链表模板类
- 数据结构(C++)最小优先权队列实现<模板类>
- [数据结构] 顺序表的实现(c++/类模板)
- [数据结构]链表的实现(c++/类模板)
- [数据结构]栈的实现(c++/类模板)
- C++类模板实现数据结构——栈
- 数据结构——静态链表模板类实现
- 数据结构——顺序双栈模板类实现
- mysql架构
- Java && ||
- 【机房重构】数据库设计
- HDU --1874畅通工程续
- 构造方法(java中跟类名一样的方法名的作用)
- 数据结构之单链表——C++模板类实现
- pentaho下载
- php邮箱找回密码功能
- Strom学习00--介绍
- Rxjava+Retrofit+Realm的天气预报
- MFC消息分类和处理方式
- 迭代器的简单理解和应用
- JAVA蓝桥杯:字符串对比
- 坚持#第139天~证明了一个真理