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

 

转载于:https://www.cnblogs.com/libin123/p/10420106.html

面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值