数据结构(C++)----单链表模板类

所有的文件都在List.h文件下

#ifndef LIST_H
#define LIST_H
#include<iostream>
#include<stdlib.h>

using namespace std;

template<class Type>
class List;

template<class Type>
class ListNode
{
public:
    friend class List<Type>;
    //链表结点构造函数
    ListNode ( ):link(NULL) {}
    ListNode ( const Type& item ):data(item),link(NULL) {}
    //给出当前结点的下一结点地址
    ListNode<Type> *NextNode ( )
    {
        return link;
    }
    //在当前结点后插入结点p
    void InsertAfter ( ListNode<Type> *p )
    {
        link->link=p;
    }
    //摘下当前结点的下一结点
    ListNode<Type> *RemoveAfter ( )
    {
        ListNode<Type> *tempptr=link;
        if(tempptr==NULL)
        {
            return NULL;
        }
        //重新链接
        link=link->link;
        return tempptr;
    }
    Type GetData()
    {

        return  data;
    }
    ListNode<Type> *GetNext()
    {
        return link;

    }
    void SetNext(ListNode<Type> *node)
    {
        link=node;
    }
private:
    Type data;
    ListNode<Type> *link;
};

template<class Type>
class List
{
private:
    ListNode<Type> *first, *last;
public:
    //创建数据为item,指针为next的新结点
    ListNode<Type> *GetNode ( const Type& item, ListNode<Type> *next )
    {
        ListNode<Type> *newnode =new ListNode<Type> ( item );
        newnode->link = next;
        return newnode;

    }
    //构造函数
    List (  )
    {
        last=first=NULL;
    }
    //析构函数
    ~List ( )
    {

        ListNode<Type> *node;
        while(first->link!=NULL)
        {
            node=first->link;
            first->link=node->link;
        }
        delete first;
    }

    //求链表长度
    int Length ( ) const
    {

        //求单链表的长度
        ListNode<Type> *node = first;
        //检测指针p指示第一个结点
        int count = 0;
        //逐个结点检测
        while ( node != NULL )
        {
            node = node->link;
            count++;
        }
        return count;

    }
    //根据值查找节点
    ListNode<Type> *Find ( Type value )
    {

        //在链表中从头搜索其数据值为value的结点
        ListNode<Type> *node = first;
        //检测指针 p 指示第一个结点
        while ( node != NULL && node->GetData() != value )
        {
            node = node->link;
        }
        // p 在搜索成功时返回找到的结点地址
        // p 在搜索不成功时返回空值
        return node;

    }
    //根据序列查找节点
    ListNode<Type> *Find ( int i )
    {

        //在链表中从头搜索第 i 个结点,不计头结点
        if ( i < 1 )
            return NULL;
        if ( i == 1 )
            return first;       // i 应  0
        ListNode<Type> *p = first;
        int j = 1;
        while ( p != NULL && j < i )          // j = i 停
        {
            p = p->link;
            j++;
        }
        return p;
    }
    void Insert(Type value)
    {
        ListNode<Type> *node=new ListNode<Type>(value);
        if(first==NULL)
        {
            first=node;
            last=node;
        }
        else
        {
            ListNode<Type> *temp=last;
            last->SetNext(node);
            last=node;
        }
    }
    //在第i个位置插入节点
    int Insert ( Type value, int i )
    {
        ListNode<Type> *newNode=new ListNode<Type>(value);
        if(first==NULL||i==0)
        {
            if(newNode==NULL)
            {
                cout<<"创建新节点时,内存分配错误"<<endl;
                exit(1);
            }
            newNode->SetNext(first);
            first=newNode;
        }
        else
        {

            ListNode<Type> *current=first;
            for(int k=1; k<i; k++)
            {
                if(current==NULL)
                {
                    break;
                }
                else
                {
                    current=current->GetNext();
                }
            }
            if(current==NULL)
            {
                cerr<<"无效的插入位置"<<endl;
                return 0;
            }
            else
            {
                if(newNode==NULL)
                {
                    cout<<"创建新节点时,内存分配错误"<<endl;
                    exit(1);
                }
                else
                {
                    newNode->SetNext(current->GetNext());
                    current->SetNext(newNode);
                }
            }
            return 1;
        }

    }
    //移除第i个节点的函数
    Type *Remove ( int i )
    {

        //从链表中删去第 i 个结点
        ListNode<Type> *node= Find (i-1), *tempnode;
        if ( node == NULL || node->link == NULL )
            return NULL;
        tempnode = node->link;
        node->link = tempnode->link;    //重新链接
        Type value = new Type ( tempnode->GetData() );
        if ( tempnode == last )
            last = node;

        delete tempnode;
        return &value;

    }
    void Traverse()
    {
        int i=1;
        ListNode<Type> *node=first;
        while(node!=NULL)
        {
            cout<<i++<<"--"<<node->GetData()<<"  ";
            node=node->GetNext();
        }

    }
    //获取第i个节点的函数
    Type Get ( int i ) //提取第 i 个结点的数据
    {
        ListNode<Type> *node = Find ( i );
        // p 指向链表第 i 个结点
        if ( node == NULL )
            return NULL;
        else
            return node->GetData();
    }
    ListNode<Type>  * getFirst()
    {
        return first;
    }
    ListNode<Type> * getLast()
    {
        return last;
    }

};
#endif // LIST_H

main

#include <iostream>
#include<List.h>
#include<stdlib.h>
using namespace std;

int main()
{
    List<string> *stringList=new List<string>();
    string name;
    cout<<"从链表的表头开始插入数据,请依次输入5个数据:"<<endl;
    for(int i=0; i<5; i++)
    {
        cin>>name;
        stringList->Insert(name);
    }
    cout<<"\n遍历链表:"<<endl;
    stringList->Traverse();
    cout<<"\n请输入要插入的节点的数据和位置:";
    string data;
    int index;
    cin>>data>>index;
    cout<<stringList->Insert(data,index)<<endl;

    stringList->Traverse();
    return 0;
}

运行结果:
这里写图片描述

面向对象程序设计课程作业 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、付费专栏及课程。

余额充值