c++数据结构之循环链表

c++模板类 循环链表

#include<iostream>
#include<list>
using namespace std;
template<class T> class List;//循环链表

template<class T>
class ListNode{
    friend class List<T>;
    private:
    ListNode<T> *link;
    T data;
    ListNode(T);
    ListNode(){}
};
template<class T>
ListNode<T>::ListNode(T element){
    data=element;
    link=0;
}
template<class T>
class List{
    public:
    List(){first = new ListNode <T>;first->link=first;};
    void Insert(T);
    void Delete(T);
    // void Invert();
    // void show();
    // void Concatentate(List<T>);
    private:
    ListNode<T> *first;
};
template<class T>
void List<T>::Insert(T k){
    ListNode<T> *newnode=new ListNode<T> (k);
   newnode->link = first->link;//newnode->link = first;
	first->link = newnode;//first = newnode;
}
template<class T>
void List<T>::Delete(T k){
ListNode<T> *previous=first;
ListNode<T> *current;
for (current = first->link;
		(current!=first)&&current->data != k;
		previous = current, current = current->link)
	{
		;
	}
	if (current!=first)
    previous->link = current->link;
			delete current;
}
// template<class T>
/* void List<T>::Invert(){
    ListNode<T> *p=first,*q=0;
    while(p)
    {
        ListNode<T> *r=q;q=p;
        p=p->link;
        q->link=r;
    }
    first=q;
}
template<class T>
void List<T>::show(){
for(ListNode<T> *current=first;current;current= current->link){
    std::cout<<current->data;
    if(current->link) std::cout<<"->";
}
std::cout<<std::endl;
}
template<class T>
void List<T>::Concatentate(List<T> b)
{
	if (!first){first = b.first; return;}
	if (b.first)
    {
			ListNode<T> *p;
			for (p = first; p->link; p = p->link) ;//空循环
			p->link = b.first;
	}
}
 */
int main(){
    
    List <int> intlist;
    intlist.Insert(5);
    intlist.Insert(15);
    intlist.Insert(25);
    intlist.Delete(5);

    return 0; 
}

猜你喜欢

转载自blog.csdn.net/qq_45347584/article/details/104139412