标签:结构 col insert splay spl list isp alt 控制
// 线性表(链表).cpp: 定义控制台应用程序的入口点。 // #include "stdafx.h" #include"iostream" using namespace std; template<class T> struct Node { T data; Node *next; }; template<class T> class listNode { private: Node<T> *Head; public: listNode(); void insert(T e); void Delete(T e); void display(); void mergelist(listNode<T>& Lb, listNode<T>& Lc); void UniqueMerge(listNode<T>& Lb ); }; template<class T> listNode<T>::listNode()//初始化链表 { Head = new Node<T>; Head->next = NULL; } template<class T> void listNode<T>::insert(T e)//插入元素 { Node<T>*q, *p=Head->next; q = Head; Node<T>*node = new Node<T>; node->data = e; node->next = NULL; if (p == NULL) { Head->next= node; } else { while (p) { if (p->data > e) { node->next=p; q->next = node; return; } else { q = p; p = p->next; } } q->next = node; } } template<class T> void listNode<T>::Delete(T e)//删除指定值 { Node<T>*p = Head->next; Node<T>*q = Head; while (p) { if (p->data == e) { q->next = p->next; delete p; return; } else { q = p; p = p->next; } } } template<class T> void listNode<T>::display()//输出链表中的值 { Node<T> *p = Head->next; while (p) { cout << p->data << " "; p = p->next; } cout << endl; } template<class T> void listNode<T>::mergelist(listNode<T> &Lb, listNode<T> &Lc)//合并两个链表 { Node<T>*pa, *pb, *pc; pa =Head->next; pb = Lb.Head->next; pc = Lc.Head; while (pa&&pb) { if (pa->data > pb->data) { pc->next = pb; pc = pb; pb = pb->next; } else { pc->next = pa; pc = pa; pa = pa->next; } } if (pa) pc->next = pa; else pc->next = pb; } template<class T> void listNode<T>::UniqueMerge(listNode<T>& Lb)//无重数合并 { Node<T>*pa, *pb,*pc; pa = Head->next; pb = Lb.Head->next; pc = Head; while (pa&&pb) { if (pa->data > pb->data) { pc->next = pb; pc = pb; pb = pb->next; } else if(pa->data==pb->data) { pb = pb->next; Lb.Delete(pa->data); } else { pc->next = pa; pc = pa; pa = pa->next; } } if (pa)pc->next = pa; else pc->next = pb; } int main() { listNode<int> s1,s2,s3; int n; int e; cout << "请输入s1链表中要输入的数据的个数" << endl; cin >> n; for (int i = 0; i < n; i++) { cin >> e; s1.insert(e); } cout << "s1表:" << endl; s1.display(); cout << "请输入s2链表中要输入的数据的个数" << endl; cin >> n; for (int i = 0; i < n; i++) { cin >> e; s2.insert(e); } cout << "s2表:" << endl; s2.display(); cout << "合并两个链表" << endl; s1.UniqueMerge(s2); cout << "合并后:" << endl; s1.display(); return 0; }
无重数合并
无脑合并
标签:结构 col insert splay spl list isp alt 控制
原文地址:https://www.cnblogs.com/Disyushen666/p/9379790.html