• 链表基本操作


    /*   注意C++ 的内存管理的复杂性
         尤其是在merge() 之中,当融合之后如何保证被合并了的链表之后的对象的析构函数会出错,不会被delete两次
         还有就是友元函数的模板写法
    */ #include <iostream> #include <typeinfo> using namespace std; template<class DataType> struct Node { DataType data; Node<DataType> *next; }; template<class DataType> class Linklist; template<class DataType> Linklist<DataType> &Merge(Linklist<DataType> &, Linklist<DataType> &); template<class DataType> class Linklist { public: Linklist(); Linklist(DataType a[], int n); void insert(DataType x, int i); DataType Delete( int i); DataType get( int i); int length(); void reverse(); void sort(); ~Linklist(); void print(); friend Linklist<DataType> & Merge<DataType>(Linklist<DataType> &L1, Linklist<DataType> &L2); private: Node<DataType> *first; }; template<class DataType> Linklist<DataType>::Linklist() { first = new Node<DataType>(); first->next = NULL; } template<class DataType> Linklist<DataType>::Linklist(DataType a[], int n) { first = new Node<DataType>(); first->next = NULL; Node<DataType> * temp = NULL; Node<DataType> * s = NULL; s = first; for( int i = 0; i < n; i++) { temp = new Node<DataType>(); temp->data = a[i]; s->next = temp; s = temp; } s->next = NULL; } template<class DataType> void Linklist<DataType>::insert(DataType x,int index) { Node<DataType> *p = first; int count = 0; while( p != NULL && count < index -1) { p = p->next; count++; } if(p == NULL) { cout<< "wrong place\n"; return ; } else { Node<DataType> *s = new Node<DataType>(); s->data = x; s->next = p->next; p->next = s; } } template<class DataType> int Linklist<DataType>::length() { Node<DataType> *temp = first; int count = 0; while(temp != NULL) { count++; temp = temp->next; } return count; } template<class DataType> DataType Linklist<DataType>::Delete( int index) { Node<DataType> *p= first; Node<DataType> *q=NULL; DataType x; int count = 0; while( p != NULL && count < index - 1) { p = p->next; count++; } if(p == NULL || p->next == NULL) { throw "wrong positon\n"; } else { q = p->next; x = p->data; p->next = q->next; delete q; return x; } } template<class DataType> DataType Linklist<DataType>::get( int index) { Node<DataType> *p = first; int count = 0; while(p != NULL && count < index -1) { p = p->next; count++; } if( p->next == NULL) { throw "wrong positon \n"; } return p->next->data; } template<class DataType> void Linklist<DataType>::print() { Node<DataType> *temp = first->next; while(temp!= NULL) { cout<< temp->data<< '\t'; temp = temp->next; } cout<<endl; //cout<<"print over!"<<endl; } template<class DataType> Linklist<DataType>::~Linklist() { Node<DataType> *temp = NULL; while( first!= NULL) { temp = first; first = first->next; cout<< "In: "<<typeid (*this ).name()<<"\t"<<temp->data<<endl; delete temp; temp = NULL; } } template<class DataType> void Linklist<DataType>::reverse() { Node<DataType> *pre = first->next; Node<DataType> *cur = pre->next; Node<DataType> *post; while(cur != NULL) { post = cur->next; cur->next = pre; pre = cur; cur = post; } first->next->next = NULL; first->next = pre; } template<class DataType> void Linklist<DataType>::sort() { Node<DataType> *cur1 = first->next; while(cur1 != NULL) { Node<DataType> *cur2 = cur1->next; Node<DataType> *min = cur1; while(cur2 != NULL) { if( cur2->data < min->data) { min = cur2; } cur2 = cur2->next; } DataType tmp = cur1->data; cur1->data = min->data; min->data = tmp; cur1 = cur1->next; } } template<class DataType> Linklist<DataType> & Merge(Linklist<DataType> &L1, Linklist<DataType> &L2) { Node<DataType> *L1_head = L1.first->next; Node<DataType> *L2_head = L2.first->next; Node<DataType> *tmp = new Node<DataType>(); L1.sort(); L2.sort(); while(L1_head != NULL && L2_head != NULL) { if(L1_head->data <= L2_head->data) { tmp->next = L1_head; tmp = tmp->next; L1_head = L1_head->next; } else { tmp->next = L2_head; tmp = tmp->next; L2_head = L2_head->next; } } if(L1_head != NULL) { tmp->next = L1_head; } if( L2_head != NULL) { tmp->next = L2_head; } if(L1.first->next->data <= L2.first->next->data) { cout<< "L2 Delte\n"; Linklist<DataType> &L = L1; delete L2.first; L2.first = NULL; return L; } else { cout<< "L1 Delte \n"; Linklist<DataType> &L = L2; delete L1.first; L1.first = NULL; return L; } //return L1.first->next->data < L2.first->next->data ? L1 : L2; } int main() { int a[] = {10,20,30,40,50}; int b[] = {1,20,7,5}; int sizea = sizeof(a) / sizeof(a[0]); int sizeb = sizeof(b) / sizeof(b[0]); Linklist< int> L(a,sizea); Linklist< int> L1(b,sizeb); // L.print(); // L.Delete(3); // L.print(); // cout<<L.get(3)<<endl; // L.insert(9,3); // L.print(); // cout<<"Original: \n"; // L.print(); // cout<<L.get(2)<<endl; // L.reverse(); // cout<<"After Reverse: \n"; // L.print(); // cout<<L.get(2)<<endl; // L.sort(); // cout<<"After sort\n"; L.print(); L1.print(); Merge(L,L1).print(); return 0; }

    听说某公司要求20分钟写完...

  • 相关阅读:
    jquery中attr和prop的区别
    director.js:客户端的路由---简明中文教程
    MVC利用Routing实现多域名绑定一个站点、二级域名以及二级域名注册Area
    机器学习——几种分类算法的汇总
    图像预处理(二值化)
    深度学习训练数据打标签过程
    卷积神经网络(CNN)
    机器学习算法中的准确率(Precision)、召回率(Recall)、F值(F-Measure)
    TortoiseGit上传项目到GitHub
    机器学习中的数据清洗与特征工程
  • 原文地址:https://www.cnblogs.com/zhuyp1015/p/2648092.html
Copyright © 2020-2023  润新知