zoukankan      html  css  js  c++  java
  • C++实现循环链表

    practice6.h文件

    #ifndef PRACTICE4_H_INCLUDED
    #define PRACTICE4_H_INCLUDED
    
    #include<iostream>
    //循环链表
    template <class Type> class List;//申明友元类的前置声明
    template<class Type> class ListIterator;//申明友元类的前置声明
    template<class Type>
    class ListNode   //节点类
    {
     friend class List<Type>;
     friend class ListIterator<Type>;
     private:
      Type data;
      ListNode *link;
      ListNode(Type);  //构造函数
      ListNode() {}
    };
    
    
    template<class Type>
    class List
    {
        friend class ListIterator<Type>;
     public:
         List(){first=new ListNode<Type>;first->link=first;}; // 一个空的链表  表头 只有一个表头
         void Insert(Type);
    
         void Delete(Type);
    
     private:
        ListNode<Type> *first;
    };
    template<class Type>
    class ListIterator
    {
    public:
        ListIterator(const List<Type>& l):list(l),current(l.first->link){};//构造函数,参数是链表,这个链表的迭代器   初始化链表和指针
        bool NotNull();
        bool NextNotNull();
        Type*First();
        Type*Next();
    
    
    private:
        const List<Type> &list;//链表 迭代器是哪个链表的
        ListNode<Type> *current;//指针,指向链表里的节点
    
    };
    
    template <class Type>//判断非空
    bool ListIterator<Type>::NotNull()
    {
    
        if(current!=list.first)  return true;
        else return false;
    }
    
    template <class Type>//判断下一个非空
    bool ListIterator<Type>::NextNotNull()
    {
    
        if(current->link!=list.first)  return true;
        else return false;
    
    }
    
    template <class Type>//取first的值
    Type*ListIterator<Type>::First()
    {
    
        if(current!=list.first)   return &current->data;
        else return 0;
    }
    
    template <class Type>//判断下一个是否为空
    Type *ListIterator<Type>::Next()
    {
    
            current=current->link;
            if (current==list.first)  current=current->link;
            return &current->data;
    }
    
    template<class Type>//构造函数
    ListNode<Type>::ListNode(Type element)
    {
        data=element;
        link=0;
    }
    template <class Type>//插入
    void List<Type>::Insert(Type k)
    {
    
        ListNode<Type> *newnode=new ListNode<Type>(k);
        newnode->link=first->link;
        first->link=newnode;
    
    }
    
    
    
    template<class Type>//删除节点
    void List<Type>::Delete(Type k)
    {
    
        ListNode<Type> *previous=first;//前一个的指针
        ListNode<Type> *current;
        for(current=first->link;(current!=first)&&current->data!=k;
        previous=current,current=current->link)
        {
          ;
        }
        if(current!=first)
        {
            //if(previous) previous->link=current->link;
            //else first=first->link;
            previous->link=current->link;
            delete current;
        }
    }
    
    
    
    
    
    #endif // PRACTICE4_H_INCLUDED

    practice3.cpp文件

    #include<iostream>
    #include "practice6.h"
    #include<list>
    using namespace std;
    int main()
    {
    
        List<int> intList;
    
        intList.Insert(5);
        intList.Insert(15);
    
        intList.Insert(25);
        intList.Insert(35);
    
    
       // cout<<"这是标准c++stl中的链表和迭代器"<<endl;
        //std::list<int> listIntegers;
       // listIntegers.push_front(5);
       // listIntegers.push_front(15);
       // listIntegers.push_front(25);
       // listIntegers.push_front(35);
    
       // std::list<int>::iterator i=listIntegers.begin();
        //while(i!=listIntegers.end())
       // {
        //    cout<<*i<<"->";
        //    ++i;
    
       // }
        //cout<<endl;
        cout<<"这是我的链表和迭代器"<<endl;
    
    
    
    
    //    if(li.NotNull())
    //    {
    //        cout<<*li.First();
    //        while(li.NextNotNull())
    //            cout<<"->"<<*li.Next();
    //        cout<<endl;
    //    }
        cout<<"测试一下循环"<<endl;
        ListIterator<int> iter(intList);
        cout<<*iter.First()<<endl;
        cout<<*iter.Next()<<endl;
        cout<<*iter.Next()<<endl;
        cout<<*iter.Next()<<endl;
    
        cout<<*iter.Next()<<endl;
        cout<<*iter.Next()<<endl;
        cout<<*iter.Next()<<endl;
        cout<<*iter.Next()<<endl;
    
    
    
        return 0;
    }
  • 相关阅读:
    CodeForces gym Nasta Rabbara lct
    bzoj 4025 二分图 lct
    CodeForces 785E Anton and Permutation
    bzoj 3669 魔法森林
    模板汇总——快读 fread
    bzoj2049 Cave 洞穴勘测 lct
    bzoj 2002 弹飞绵羊 lct裸题
    HDU 6394 Tree 分块 || lct
    HDU 6364 Ringland
    nyoj221_Tree_subsequent_traversal
  • 原文地址:https://www.cnblogs.com/libin123/p/10420115.html
Copyright © 2011-2022 走看看