数据结构 链表
来源:互联网 发布:nginx 网络模型 编辑:程序博客网 时间:2024/06/07 03:21
#ifndef LINK_H#define LINK_H#include<iostream>#include<cmath> #include<fstream> using namespace std;template<class T>struct LinkNode //-----------------------------链表结点类定义{T data;LinkNode<T>*link;LinkNode(LinkNode<T>*ptr=NULL){link=ptr;}LinkNode(const T&item,LinkNode<T>*ptr=NULL){data=item;link=ptr;}//初始化};template<class T> class List //单链表类定义,不用继承也可实现{public:List(){first=new LinkNode<T>;}//----------------构造函数List(const T&x){first=new LinkNode<T>(x);}//----构造函数List(List<T>&L);//----------------复制构造函数~List(){makeEmpty();}void makeEmpty();//----------------将链表置为空表int Length()const;//---------------计算链表长度LinkNode<T>*getHead()const{return first;}//------返回附加头结点地址LinkNode<T>*Search(T x);//---------------搜索含数据X的元素LinkNode<T>*Locate(int i);//-------------搜索第i个函数地址 void searchFromLast(int i); bool getData(int i,T&x);//-------------取出第i个函数地址void setData(int i,T&x);//-------------修改第i个函数地址bool Insert(int i,T&x); bool Remove(int i,T&x); bool IsJoined(List<int>L,List<int>R); bool IsEmpty()const{return first->link=NULL?true:false;}bool IsFull()const{return false;}void input(int a[],int n); void max(T &x);void min(T &x); void output(); void removeRepition(); void Reverseprint(LinkNode<T>*t);//逆向输出void Sort(); //插入排序法void DirectSort(); //直接排序法 bool IsCircle();//判断一个单链表是否存在环链 void Reverse();//原地翻转链表void Reverse(LinkNode<T>*t); //递归实现void ReverseTwo();//间接翻转链表List<T>&operator=(List<T>&L);//-------------重载函数:赋值protected:LinkNode<T>*first;};template<class T>List<T>::List(List<T> &L) { T value; LinkNode<T>*srcptr=L.getHead(); LinkNode<T>*destptr=first=new LinkNode<T>; while(srcptr->link!=NULL) { value=srcptr->link->data; destptr->link=new LinkNode<T>(value); destptr=destptr->link; srcptr=srcptr->link; } destptr->link=NULL;};template<class T>void List<T>::makeEmpty(){ LinkNode<T> *p;while(first->link!=NULL){p=first->link; //p用来保存被删的结点 first->link=p->link;delete p;} };template<class T>int List<T>::Length()const { int count=0;LinkNode<T> *p=first->link;while(p!=NULL) {count++;p=p->link;}return count;};template<class T> LinkNode<T>*List<T>::Search(T x){ LinkNode<T>*current=first->link; while(current!=NULL) { if(current->data==x) break; else current=current->link; } return current;};template<class T> LinkNode<T>*List<T>::Locate(int i){int k=0; LinkNode<T>*current=first;while(k<i&¤t!=NULL) {current=current->link;k++;}return current;}; template<class T> void List<T>::max(T &data) {LinkNode<T>*current=first->link;data=current->data; while(current!=NULL) {if(data<current->data) data=current->data; current=current->link;}}; template<class T> void List<T>::min(T &data) {LinkNode<T>*current=first->link;data=current->data; while(current!=NULL) {if(data>current->data) data=current->data; current=current->link;}}; template<class T> bool List<T>::getData(int i,T&x){LinkNode<T>*current=Locate(i); if(current==NULL)return false;else{x=current->data;return true;}};template<class T> void List<T>::setData(int i,T&x){ LinkNode<T>*current=Locate(i); if(current==NULL)return ;elsecurrent->data=x;}; template<class T> bool List<T>::Insert(int i,T&x) {LinkNode<T>*current=Locate(i); LinkNode<T>*newNode=new LinkNode<T>(x); if(current==NULL)return false; newNode->link=current->link;current->link=newNode; return true;}; template<class T> bool List<T>::Remove(int i,T&x){LinkNode<T>*current=Locate(i-1); LinkNode<T>*p; if(current==NULL||current->link=NULL) return false; p=current->link; current->link=p->link; x=current->data;delete p;return true;}; template<class T> void List<T>::output() { LinkNode<T>*current=first->link; while(current!=NULL) { cout<<current->data<<" "; current=current->link; } cout<<endl; }; template<class T> void List<T>::Reverseprint(LinkNode<T>*t)//逆向输出 { if(t==NULL) return; if(t->link==NULL) cout<<t->data<<" "; else{ Reverseprint(t->link); cout<<t->data<<" ";} }; template<class T> void List<T>::input(int a[],int n) { int k=0; for(int i=0;i<n;i++,k++ ) Insert(k,a[i]); }; template<class T> void List<T>::Sort() { LinkNode<T> *head=first->link; LinkNode<T> *t,*q,*p=NULL; first->link= NULL ; while(head!=NULL) { for(t=head, q=first;(q!=NULL)&&(q->data<t->data);p=q,q=q->link);//找到应该插入的位置 head = head->link; p->link=t; t->link=q; } }; template<class T> List<T>& List<T>::operator=(List<T>&L) { T value; LinkNode<T>*scrptr=first; LinkNode<T>*destptr=first=new LinkNode<T>; while(scrptr->link!=NULL) { value=scrptr->link->data; destptr->link=new LinkNode<T>(value); destptr=destptr->link; scrptr=scrptr->link; } destptr->link=NULL; return *this; }; template<class T>void List<T>::DirectSort() {LinkNode<T>*current=first->link;LinkNode<T>*next=current->link;for(;current->link!=NULL;current=current->link){for(next=current->link;next!=NULL;next=next->link){if(current->data>next->data) { swap(current->data,next->data); } }} };template<class T>bool List<T>::IsCircle() {LinkNode<T>*fast=first,*slow=first;//fast一次走两步,last一次走一步,判断最后是否会相交 while(fast&&fast->link){fast=fast->link->link; slow=slow->link;if(fast==slow){cout<<"Is Circle : "<<endl;return true;}}cout<<"Is not Circle : "<<endl; return false; };template<class T>void List<T>::searchFromLast(int i) { LinkNode<T>*fast=Locate(i),*slow=first; int k=1; while(fast!=NULL) { fast=fast->link;slow=slow->link; }cout<<"the last "<<i<<" data is : "<<slow->data<<endl; }; template<class T> //若相交则从相交点往后处处p==q bool List<T>::IsJoined(List<int>L,List<int>R)//如果两个链表相交,那么他们的最后结点相同{ LinkNode<T>*p=L.getHead()->link; LinkNode<T>*q=R.getHead()->link; int nL=0,nR=0;while(p->link!=NULL){nL++;p=p->link;} while(q->link!=NULL){nR++;q=q->link;} if(p!=q){cout<<"不相交"<<endl/*;return false*/;} if(nR>=nL) { q=R.Locate(nR-nL)->link; p=L.getHead()->link;while(p!=q) {p=p->link;q=q->link;} if(p!=NULL)cout<<"相交点为: "<<p->data<<endl; }if(nR<nL){ p=L.Locate(nL-nR)->link; q=R.getHead()->link; while(p!=q){p=p->link;q=q->link;}if(p!=NULL)cout<<"相交点为: "<<p->data<<endl; } return true;}; template<class T> void List<T>::Reverse()//原地翻转,让每一个指针都指向前面一个{LinkNode<T>*pNode=first->link; LinkNode<T>*prev=NULL;LinkNode<T>*pNext=NULL; while(pNode!=NULL){pNext=pNode->link; if(pNext==NULL) first->link=pNode; pNode->link=prev; prev=pNode; //----------------关键 pNode=pNext; } };template<class T> void List<T>::Reverse(LinkNode<T>*t)//用递归实现翻转{//递归就是一个进栈出栈的过程,链表前面的元素先进栈,在栈底,后面的元素后进栈,在栈顶,先出栈. if(NULL==t)return ; cout<<t->data<<" ";if(t->link!=NULL)Reverse(t->link); //如果把这句放在第二个if前面,那就是从头到尾输出链表,曾经的你或许是用while或者用for循环输出链表,现在你又多了一种方式 //cout<<t->data<<" ";};template<class T> void List<T>::removeRepition(){LinkNode<T>*p=first->link,*q;q=p;int temp=p->data;p=p->link; while(p!=NULL){if(p->data==temp) q->link=p->link; else temp=p->data; q=p;p=p->link; }}template<class T> void List<T>::ReverseTwo() //插入反转 {LinkNode<T>*current=first->link; first->link=NULL;while(current!=NULL){Insert(0,current->data);current=current->link;} }#endif
0 0
- 数据结构---链表
- 数据结构 - 链表
- 【数据结构】链表
- 数据结构-链表
- '数据结构' 链表
- 数据结构-链表
- 【数据结构】链表
- 数据结构--链表
- 【数据结构】 链表
- 数据结构--链表
- 数据结构-链表
- 数据结构 - 链表
- 链表 - 数据结构
- 数据结构:链表
- [数据结构]链表
- 数据结构--链表
- 【数据结构】链表
- 数据结构-链表
- POJ 3660 Cow Contest(Floyd求传递闭包)
- selenium(java)之文件系统FileHandler
- 通讯录
- Alluxio Paper
- Androi开发中的给予XMPP和第三方的即时通讯(面试简述)
- 数据结构 链表
- Git基础操作
- Oracle本地连接与远程连接配置
- IO--部分源码解读
- 两分钟彻底让你明白Android Activity生命周期(图文)
- React Native 页面之间传值、回传数据
- CSS列表属性list-style-type 和 list-style-image
- ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)
- BZOJ 1432: [ZJOI2009]Function【找规律