线性表

1.线性表的定义

线性结构是n(n≥0)个结点的有穷序列(a0, a1, ..., an-1)。a0被称为起始结点, an-1被称为终端结点,i称为ai的序号或位置。对任意一对相邻结点ai和ai+1(0≤i≤n-2),ai被称为ai+1的直接前驱,ai+1称为ai的直接后继。其实结点只有直接后继,终端结点只有直接前驱。线性结构中的每个结点代表一个数据元素。在不同的实际问题中,结点代表的数据元素是不同的。

直接前驱和直接后继就是所谓的结点间的逻辑关系,这两种关系从不同角度刻画了同一种关系,及邻接关系。线性结构中的邻接关系是一对一的,及每个结点至多有一个直接前驱和后继。而所有的结点按一对一的邻接关系构成的整体就是线性结构。

线性表是处理线性结构的数据结构。线性表中的数据元素的个数称为线性表的长度,简称为表长。表长为0的线性表为空表。 线性表以某种方式保存结构的值以及结点直接的关系,实现线性结构中的各种操作。

根据线性结构的特点,线性表的基本运算有以下几种:

    1.创建一个空线性表 create。

    2.删除线性表中的所有数据元素 clear。

    3.求线性表的长度 length。  

    4.在第i个位置插入一个元素insert(i, x),使线性表从(a0, a1, ,...ai-1, ai,..., an-1) 变成(a0, a1, ,...ai-1, x,ai,..., an-1) ,参数           的合法取值范围为0到n。

    5.删除第i个位置的元素remove(i,x), 使线性表从(a0, a1, ,...ai-1, ai,..., an-1) 变成(a0, a1, ,...ai-1, ..., an-1),参数的合法取          值范围为0到n-1。

    6.搜索元素search(x), 检查某个元素x在线性表中是否出现,并返回x的位置。

    7.返回线性表中第i个数据元素数据的值visit(i)。

    8.按序访问线性表的每一个数据元素traverse。

    线性表的抽象类:

template<class elemType>

class list{
    public:
    virtual void clear() = 0;
    virtual int length() const = 0;
    virtual void insert(int i, const elemType &x) = 0;
	virtual void remove(int i) = 0;
    virtual int search(const elemType &x) const = 0;
    virtual elemType visit(int i) const = 0;
    virtual void traverse() const = 0;
	virtual ~list() {}
};

线性表的抽象类是一个类模板,模板参数是线性表中数据元素的类型。线性表的抽象类定义了线性表类必须具备的行为。在上述中的代码中,除了创建一个线性表以外,线性表的每一基本运算都被定义成为一个纯虚函数。创建一个线性表的功能可以由每一具体类的构造函数来实现。

除此之外,抽象类中还定义了一个函数体为空的虚析构函数。定义虚析构函数是为了防止内存泄漏。

 

2.线性表的顺序实现

//抽象类
template <class elemType>
class list
{
public:
    virtual void clear() = 0;
    virtual int length() const = 0;
    virtual void insert(int i, const elemType &x) = 0;
    virtual void remove(int i) = 0;
    virtual int search(const elemType &x) const = 0;
    virtual elemType visit(int i) const = 0;
    virtual void traverse() const = 0;
    virtual ~list(){};
};

//抽象实现
template <class elemType>
class seqList : public list<elemType>
{
private:
    elemType *data;
    int currentLength;
    int maxSize;
    void doubleSpace();

public:
    seqList(int initSize = 10);
    ~seqList(){};
    void clear();
    int length() const;
    void insert(int i, const elemType &x);
    void remove(int i);
    int search(const elemType &x) const;
    elemType visit(int i) const;
    void traverse() const;
    void erase(int i);
};

/**
 *析构函数
 */
template <class elemType>
seqList<elemType>::~seqList()
{
    delete[] data;
}

/**
 * clear 函数,清除所有的元素,实际上只需要将length置0就好
 * 因为之后再进行操作仍然会覆盖掉之前的数据,并且之前的数据
 * 已经不可以获取了
 */

template <class elemType>
void seqList<elemType>::clear()
{
    currentLength = 0;
}

/**
 * length 函数 返回线性表的长度
 */
template <class elemType>
int seqList<elemType>::length() const
{
    return currentLength;
}

/**
 * visit 函数 访问指定位置的元素值
 */
template <class elemType>
elemType seqList<elemType>::visit(int i) const
{
    return data[i];
}

/**
 * traverse 函数 输出线性表中的元素
 */

template <class elemType>
void seqList<elemType>::traverse() const
{
    cout << endl;
    for (int i = 0; i < currentLength; i++)
    {
        cout << data[i];
        cout << " ";
    }
}

/**
 * 构造函数 申请空间 初始化变量
 */
template <class elemType>
seqList<elemType>::seqList(int initSize)
{
    data = new elemType[initSize];
    maxSize = initSize;
    currentLength = 0;
}

/**
 * search 函数 搜索列表中的值,如果存在输出相对应的序号
 */
template <class elemType>
int seqList<elemType>::search(const elemType &x) const
{
    int i;
    for (i = 0; i < currentLength && data[i] != x; ++i)
        ;
    if (i == currentLength)
        return -1;
    else
        return i;
}

/**
 * doubleSpace 函数 动态扩展空间
 */
template <class elemType>
void seqList<elemType>::doubleSpace()
{
    elemType *tmp = data;
    maxSize *= 2;
    data = new elemType[maxSize];
    for (int i = 0; i < currentLength; ++i)
        data[i] = tmp[i];
    delete[] tmp;
}

/**
 * insert 函数 向某个特定的位置插入值
 */
template <class elemType>
void seqList<elemType>::insert(int i, const elemType &x)
{
    if (currentLength == maxSize)
        doubleSpace();
    for (int j = currentLength; j > i; j--)
        data[j] = data[j - 1];
    data[i] = x;
    ++currentLength;
}

/**
 * remove 函数 删除线性表中某个位置的数
 */
template <class elemType>
void seqList<elemType>::remove(int i)
{
    if (i > currentLength)
    {
        cout << "无此序号的元素";
        return -1;
    }
    else
        for (int j = i; j < currentLength; j++)
            data[j] = data[j + 1];
    --currentLength;
}

/**
 * erase 函数 删除数组中的特定的序号对应元素值的所有元素
 */

template <class elemType>
void seqList<elemType>::erase(int i)
{
    elemType tmp = data[i];
    int cnt = 0;
    for (int j = 0; j < currentLength; j++)
        if (data[j] == tmp)
            ++cnt;
        else
            data[j - cnt] = data[j];
    currentLength -= cnt;
}

3.线性表的链接实现

1.单链表实现

#pragma once
#include<iostream>
using namespace std;
template <class elemType>
class list
{
public:
	virtual void clear() = 0;
	virtual int length() const = 0;
	virtual void insert(int i, const elemType& x) = 0;
	virtual void remove(int i) = 0;
	virtual int search(const elemType& x) const = 0;
	virtual elemType visit(int i) const = 0;
	virtual void traverse() const = 0;
	virtual ~list() {}
};

template <class elemType>
class sLinkList : public list<elemType>
{
private:
	struct node
	{
		elemType data;
		node* next;
		node(const elemType& x, node* n = nullptr)
		{
			data = x;
			next = n;
		}
		node() : next(nullptr) {}
		~node() {}
	};
	node* head;
	int currentLength;
	node* move(int i) const;

public:
	sLinkList();
	~sLinkList();
	void clear();
	int length() const;
	void insert(int i, const elemType& x);
	void remove(int i);
	int search(const elemType& x) const;
	elemType visit(int i) const;
	void erase(int i);
	void traverse() const;
};

/**
 * move 函数 移动指针到相应的位置
 */
template <class elemType>
typename sLinkList<elemType>::node* sLinkList<elemType>::move(int i) const
{
	node* p = head;
	while (i-- > 0) {
		p = p->next;
	}
	return p;
}

/**
 * 构造函数
 */
template <class elemType>
sLinkList<elemType>::sLinkList()
{
	head = new node;
	currentLength = 0;
}

/**
  *析构函数
  */
template<class elemType>
sLinkList<elemType>::~sLinkList() {
	clear();
	delete head;
}

/**
  *length 函数 返回线性表的长度
  */
template<class elemType>
int sLinkList<elemType>::length()const {
	return currentLength;
}

/**
 * clear 函数 清除所有的结点
 */
template <class elemType>
void sLinkList<elemType>::clear()
{
	node* p = head->next;
	node* q;
	head->next = nullptr;
	while (p != nullptr)
	{
		q = p->next;
		delete p;
		p = q;
	}
	currentLength = 0;
}

/**
 * insert 函数 特定位置插入特定的值
 */
template <class elemType>
void sLinkList<elemType>::insert(int i, const elemType& x)
{
	if (i < 1)
	{
		cout << "输入位置有误";
		return;
	}
	node* p = move(i-1);
	p->next = new node(x, p->next);

	++currentLength;
}

/**
 * remove 函数 删除特定位置上的元素
 */
template <class elemType>
void sLinkList<elemType>::remove(int i)
{
	node* p = move(i - 1);
	node* tmp = p->next;
	p->next = tmp->next;
	delete tmp;
	--currentLength;
}

/**
 * search 函数 寻找给定的值,并返回对应的序号
 */
template <class elemType>
int sLinkList<elemType>::search(const elemType& x) const
{
	node* p = head->next;
	int i = 0;

	while (p != nullptr && p->data != x)
	{
		p = p->next;
		++i;
	}
	if (p = nullptr)
		return -1;
	else
		return i;
}

/**
 * visit 函数 获得指定位置的元素
 */
template <class elemType>
elemType sLinkList<elemType>::visit(int i) const
{
	return move(i)->data;
}

/**
 * traverse 函数 历遍所有的元素
 */
template <class elemType>
void sLinkList<elemType>::traverse() const
{
	node* p = head->next;
	cout << std::endl;
	while (p != nullptr)
	{
		cout << p->data << " ";
		p = p->next;
	}
}

/**
 * erase 函数 删除与指定位置元素值一样的元素
 */
template <class elemType>
void sLinkList<elemType>::erase(int i)
{
	node* pre = move(i - 1);
	node* delp = pre->next;
	elemType tmp = delp->data;
	int cnt = 1;

	pre->next = delp->next;
	delete delp;
	for (pre = head; pre->next != nullptr; pre = pre->next)
	{
		if (pre->next->data == tmp)
		{
			delp = pre->next;
			pre->next = delp->next;
			delete delp;
			++cnt;
		}
	}
	currentLength -= cnt;
}

2.双链表实现

#include <iostream>
using namespace std;
template <class T>
class list
{
public:
    virtual void clear() = 0;
    virtual int length() const = 0;
    virtual void insert(int i, const T &x) = 0;
    virtual void remove(int i) = 0;
    virtual int search(const T &x) const = 0;
    virtual T visit(int i) const = 0;
    virtual void traverse() const = 0;
    virtual ~list(){};
};

template <class T>
class dLinkList : public list<T>
{
private:
    struct node
    {
        T data;
        node *prev, *next;

        node(const T &x, node *p = NULL, node *n = NULL)
        {
            data = x;
            next = n;
            prev = p;
        }
        node() : next(NULL), prev(NULL) {}
        ~node() {}
    };

    node *head, *tail;
    int currentLength;
    node *move(int i) const
    {
        node *p = head;
        while (i-- >= 0)
            p = p->next;
        return p;
    }

public:
    dLinkList();
    ~dLinkList()
    {
        clear();
        delete head;
        delete tail;
    }

    void clear();
    int length() const;
    void insert(int i, const T &x);
    void remove(int i);
    int search(const T &x) const;
    T visit(int i) const;
    void traverse() const;
};

/**
 * 构造函数
 */
template <class T>
dLinkList<T>::dLinkList()
{
    head = new node;
    head->next = tail = new node;
    tail->prev = head;
    currentLength = 0;
}

/**
  *length 函数 返回线性表的长度
  */
template <class T>

int dLinkList<T>::length() const
{
    return currentLength;
}

/**
 * insert 函数 特定位置插入特定的值
 */
template <class T>
void dLinkList<T>::insert(int i, const T &x)
{
    node *pos, *tmp;
    pos = move(i);
    tmp = new node(x, pos->prev, pos);
    pos->prev->next = tmp;
    pos->prev = tmp;
    ++currentLength;
}

/**
 * remove 函数 删除特定位置上的元素
 */
template <class T>
void dLinkList<T>::remove(int i)
{
    node *pos;
    pos = move(i);
    pos->prev->next = pos->next;
    pos->next->prev = pos->prev;
    delete pos;
    --currentLength;
};

/**
 * clear 函数 清除所有的结点
 */
template <class T>
void dLinkList<T>::clear()
{
    node *p = head->next, *q;
    head->next = tail;
    tail->prev = head;
    while (p != NULL)
    {
        q = p->next;
        delete p;
        p = q;
    }
    currentLength = 0;
}

/**
 * search 函数 寻找给定的值,并返回对应的序号
 */
template <class T>
int dLinkList<T>::search(const T &x) const
{
    node *p = head->next;
    int i;
    for (i = 0; p != tail && p->data != x; i++)
        p = p->next;
    if (p == tail)
        return -1;
    else
        return i;
}

/**
 * visit 函数 获得指定位置的元素
 */
template <class T>
T dLinkList<T>::visit(int i) const
{
    return move(i)->data;
}

/**
 * traverse 函数 历遍所有的元素
 */
template <class T>
void dLinkList<T>::traverse() const
{
    node *p = head->next;
    cout << endl;
    while (p != tail)
    {
        cout << p->data << "  ";
        p = p->next;
    }
    cout << endl;
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值