• C++实现模板化创建单链表


    practice4.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);  //构造函数
    };
    
    
    template<class Type>
    class List
    {
        friend class ListIterator<Type>;
     public:
         List(){first=0;};  //构造函数
         void Insert(Type);
         void Show();
         void Delete(Type);
         void Invert();
         void Concatenate(List<Type>);
     private:
        ListNode<Type> *first;
    };
    template<class Type>
    class ListIterator
    {
    public:
        ListIterator(const List<Type>& l):list(l),current(l.first){};//构造函数,参数是链表,这个链表的迭代器   初始化链表和指针
        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)  return true;
        else return false;
    }
    
    template <class Type>//判断下一个非空
    bool ListIterator<Type>::NextNotNull()
    {
    
        if(current&&current->link)  return true;
        else return false;
    
    }
    
    template <class Type>//取first的值
    Type*ListIterator<Type>::First()
    {
    
        if(list.first)   return &list.first->data;
        else return 0;
    }
    
    template <class Type>//判断下一个是否为空
    Type *ListIterator<Type>::Next()
    {
        if(current)
        {
            current=current->link;
            return &current->data;
        }
        else return 0;
    }
    
    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;
        first=newnode;
    
    }
    
    template <class Type>//展示
    void List<Type>::Show()
    {
    
        for(ListNode<Type> *current=first;current;current=current->link)
        {
            std::cout<<current->data;
            if(current->link) std::cout<<"->";
        }
        std::cout<<std::endl;
    }
    
    template<class Type>//删除节点
    void List<Type>::Delete(Type k)
    {
    
        ListNode<Type> *previous=0;//前一个的指针
        ListNode<Type> *current;
        for(current=first;current&&current->data!=k;
        previous=current,current=current->link)
        {
          ;
        }
        if(current)
        {
            if(previous) previous->link=current->link;
            else first=first->link;
            delete current;
        }
    }
    template <class Type>//反转
    void List<Type>::Invert()///////////////尚且不懂
    {
    
        ListNode<Type>*p=first,*q=0;
        while(p)
        {
    
            ListNode<Type> *r=q;q=p;
            p=p->link;
            q->link=r;
    
        }
        first=q;
    
    }
    template<class Type>//合并两个链表
    void List<Type>::Concatenate(List<Type> b)
    {
        if(!first){first=b.first;return;}
        if(b.first)
        {
            ListNode<Type> *p;
            for(p=first;p->link;p=p->link);//空循环
            p->link=b.first;
    
        }
    }
    
    
    
    
    #endif // PRACTICE4_H_INCLUDED

    practice3.cpp

    #include<iostream>
    #include "practice6.h"
    #include<list>
    using namespace std;
    int main()
    {
        cout<<"²âÊÔ"<<endl;
        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;
    }

    #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);  //构造函数};

    template<class Type>class List{    friend class ListIterator<Type>; public:     List(){first=0;};  //构造函数     void Insert(Type);     void Show();     void Delete(Type);     void Invert();     void Concatenate(List<Type>); private:    ListNode<Type> *first;};template<class Type>class ListIterator{public:    ListIterator(const List<Type>& l):list(l),current(l.first){};//构造函数,参数是链表,这个链表的迭代器   初始化链表和指针    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)  return true;    else return false;}
    template <class Type>//判断下一个非空bool ListIterator<Type>::NextNotNull(){
        if(current&&current->link)  return true;    else return false;
    }
    template <class Type>//取first的值Type*ListIterator<Type>::First(){
        if(list.first)   return &list.first->data;    else return 0;}
    template <class Type>//判断下一个是否为空Type *ListIterator<Type>::Next(){    if(current)    {        current=current->link;        return &current->data;    }    else return 0;}
    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;    first=newnode;
    }
    template <class Type>//展示void List<Type>::Show(){
        for(ListNode<Type> *current=first;current;current=current->link)    {        std::cout<<current->data;        if(current->link) std::cout<<"->";    }    std::cout<<std::endl;}
    template<class Type>//删除节点void List<Type>::Delete(Type k){
        ListNode<Type> *previous=0;//前一个的指针    ListNode<Type> *current;    for(current=first;current&&current->data!=k;    previous=current,current=current->link)    {      ;    }    if(current)    {        if(previous) previous->link=current->link;        else first=first->link;        delete current;    }}template <class Type>//反转void List<Type>::Invert()///////////////尚且不懂{
        ListNode<Type>*p=first,*q=0;    while(p)    {
            ListNode<Type> *r=q;q=p;        p=p->link;        q->link=r;
        }    first=q;
    }template<class Type>//合并两个链表void List<Type>::Concatenate(List<Type> b){    if(!first){first=b.first;return;}    if(b.first)    {        ListNode<Type> *p;        for(p=first;p->link;p=p->link);//空循环        p->link=b.first;
        }}



    #endif // PRACTICE4_H_INCLUDED

  • 相关阅读:
    大数据总结
    spark_streaming_微批量处理
    spark_sql_解析器
    spark_sql_函数
    spark-sql-04-spark连接hive的几种方式
    spark-sql-04-on_hive
    spark-sql-04-hive
    CF550C Divisibility by Eight
    CF489C Given Length and Sum of Digits...
    CF550A Two Substrings
  • 原文地址:https://www.cnblogs.com/libin123/p/10420106.html
Copyright © 2020-2023  润新知