3.DataStructure Cp2 T1 Singly-Linked Node

本文介绍了一种通用的单链表数据结构实现方法,并详细解释了其内部工作原理,包括节点的创建、插入、删除等操作。文章还提供了一个完整的模板类实现,展示了如何在C++中使用模板来实现泛型编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3. DataStructure Cp2 T1 Singly-Linked Node

this one is quite similar with doubly-linked linknode

singlelinknode.h
#include"stdafx.h"
#include"targetver.h"
#ifndef singlelinknode_h
#define singlelinknode_h
#include<iostream>
using namespace std;

#pragma once

template <class type>
class list
{
public://virtual function shall be put into public

    virtual void clear() = 0;
    virtual int length()const = 0;
    virtual void insert(int i, const type&x) = 0;
    virtual void remove(int i) = 0;
    virtual int search(const type&x)const = 0;//一开始以为是用来调用结构中的数字
    virtual type visit(int i)const = 0;//加在后面不会修改数据
    virtual void traverse()const = 0;


    virtual ~list();
};

class OutOfBound {};
class IllegalSize {};



template <class type>
class singlelinknode :public list<type>//<type>
{
private:

    //part I
    struct node//这个struct实际上是个class,注意构造函数
    {
        type data;
        node*next;
        node() :next(NULL){};
        node(const type& x, node*m)
        {
            data = x; next = m;/*修改完毕*/
        }
        ~node() {};//我就说不用析构函数
    };

    //partII
    node*head, *tail;
    int currentlength;

    //partIII 不写成内置类编译怎么都不通过
    node* move(int i)const
    {
        if (i<0 || i>currentlength)
            throw OutOfBound(); //at first,this hacebeen ignorant

        node*tmp = head;
        while (i>0)/*check*/
        {
            tmp = tmp->next;
            i--;
        }//应该看看是不是移动到第i个节点了,有个问题,head是第0还是第一?
        return tmp;
    }

public:

    singlelinknode();
    ~singlelinknode();

    void clear();
    int length()const;
    void insert(int i, const type&x);
    void remove(int i);
    int search(const type&x)const;//一开始以为是用来调用结构中的数字
    type visit(int i)const;//加在后面不会修改数据
    void traverse()const;

};



#endif
singlelinknode.cpp
#include "stdafx.h"
#include "singlelinknode.h"

template<class type>//不知道写什么?为head tail申请空间,随后再连一起
singlelinknode<type>::singlelinknode()
{
    head = new node;
    tail = new node;
    head->next = tail;/*check*/
    currentlength = 0;//忘写了
}

template<class type>
singlelinknode<type>::~singlelinknode()
{
    clear();
    delete head;

}

template<class type>
void singlelinknode<type>::clear()
{
    node*tmp = head->next;
    head->next = tail;

    while (tmp != tail)//跟课本上不太一样
    {
        node*p = tmp;
        tmp = tmp->next;/*check*/
        delete p;
    }
    currentlength = 0;//注意清零,忘写了
}

template<class type>
int singlelinknode<type>::length() const
{
    return currentlength;
}

template<class type>//插到i前面还是i后面? 是i和i-1
void singlelinknode<type>::insert(int i, const type & x)
{
    node*p = move(i-1);
    node*tmp = new node(x, p->next);
    p->next = tmp;

    currentlength++;//注意清零,忘写了
}

template<class type>
void singlelinknode<type>::remove(int i)
{
    char*tmp = move(i-1);/*diffenence from doubly-linked is
                         we can only start from the very beginning*/
    char*p = tmp->next;
    tmp->next = tmp->next->next;/*check*/

    delete p;//曾经在这一块有过疑问,
    //事实上删除指针会自动启用对应的析构函数

    currentlength--;
}

template<class type>
int singlelinknode<type>::search(const type & x) const
{
    node*tmp = head;
    int i = 0;

    while (tmp != tail && tmp->data != x)
    {
        tmp = tmp->next;
        i++;
    }

    if (tmp == tail)
    {
        cout << "none exit\t\n";
        return -1;
    }
    else return i;//same question,should i start from 0?

    return 0;
}

template<class type>
type singlelinknode<type>::visit(int i) const
{
    if (i<0 || i>currentlength)throw OutOfBound();//at first,this hacebeen ignorant

    node*tmp = head;
    int n = 0;
    while (n < i)
    {
        tmp = tmp->next;
        n++;
    }

    return tmp->data;//我也不知道应该是前一个还是后一个
}

template<class type>
void singlelinknode<type>::traverse() const
{
    node*tmp = head - next;
    while (node != tail)
    {
        cout << tmp->data << endl;
        tmp = tmp->next;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值