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;
    }
  • 相关阅读:
    NodeJS Stream 五:双工流
    NodeJS Stream 四:Writable
    NodeJS Stream 三:readable
    NodeJS Stream 二:什么是 Stream
    NodeJS stream 一:Buffer
    JavaScript 精粹
    详解k8s中的liveness和readiness的原理和区别_转
    通过 GoLang 全面了解 HTTPS_转自知乎-腾讯技术
    golang之vim和vscode开发环境
    go命令工具补遗
  • 原文地址:https://www.cnblogs.com/libin123/p/10420115.html
Copyright © 2011-2022 走看看