数据结构-C++实现(一):数组链表

本文介绍了如何使用C++实现数据结构中的数组链表,包括一个抽象类LinearList.h、数组链表头文件ArrayList.h以及主程序源文件。内容涵盖数组链表的容量、实际大小、迭代器、添加元素、删除元素、查找元素等操作。同时讨论了构造函数、拷贝构造函数以及析构函数的实现,并实现了容量调整和输出功能。

数据结构C++实现第一发,主要有几部分构成,分别是一个抽象类LinearList.h、数组链表的头文件ArrayList.h以及main.cpp源文件。

LinearList.h文件具体信息如下:

#ifndef INC_01_ARRAYLIST_LINEARLIST_H
#define INC_01_ARRAYLIST_LINEARLIST_H

#include 
template 
class LinearList {
public:
    virtual ~LinearList() {};
    virtual bool empty() const = 0;
    virtual int size() const = 0;
    virtual T& get(int index) const = 0;
    virtual int indexof(const T& theElement) const = 0;//first pos
    virtual void erase(int index) = 0;
    virtual void insert(int index, const T& theElement) = 0;
    virtual void output(std::ostream& out) const = 0;
};
ArrayList.h文件如下:

#ifndef INC_01_ARRAYLIST_ARRAYLIST_H
#define INC_01_ARRAYLIST_ARRAYLIST_H

#include "LinearList.h"
#include 
#include 
using namespace std;

template  void resizeCapacity(T*& a,int oldCapacity, int newCapacity);

template 
class ArrayList :public LinearList {
public:
    //iterator
    class iterator{
    public:
        //
        typedef bidirectional_iterator_tag iterator_category;
        typedef T value_type;
        typedef ptrdiff_t difference_type;
        typedef T* pointer;
        typedef T& reference;
        //construct
        iterator(T* thepos=0) { pos = thepos;}
        //operator
        T& operator*() const { return *pos;}
        T* operator->() const { return &*pos;}
        iterator& operator++()
            { ++pos; return *this;}
        iterator& operator++(int)
            { iterator old = *this; ++pos; r
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值