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; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值