用C++语言实现线性表

1.用数组实现

#include<iostream>
#include "utility.h"

using namespace std;

const int max_list = 5000;

template <class List_entry> //模板
class List {
public:
   List();  //构造函数
   int size() const; //返回线性表的大小
   bool full() const; //判满
   bool empty() const; //判空
   void clear();  //清空
   void traverse(void (*visit)(List_entry &)); //遍历
   Error_code retrieve(int position, List_entry &x) const; //读取指定位置的数据
   Error_code replace(int position, const List_entry &x);  //换掉指定位置的数据
   Error_code remove(int position, List_entry &x); //删除指定位置的数据
   Error_code insert(int position, const List_entry &x); //在指定位置插入数据

protected:
   int count;  //记数
   List_entry entry[max_list]; //数组,存放数据
};

template <class List_entry>
List<List_entry>::List() //构造函数
{
    count = 0;
}

template <class List_entry>
void List<List_entry>::clear() //清空
{
    count = 0;
}

template <class List_entry>
int List<List_entry>::size() const //返回线性表的大小
{
   return count;
}

template <class List_entry>
bool List<List_entry>::empty() const //判空
{
   return count <= 0;
}

template <class List_entry>
bool List<List_entry>::full() const //判满
{
   return count >= max_list;
}

template <class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry &x) //在指定位置插入数据
{
   if (full())
      return overflow; //判断能否再插入数据

   if (position < 0 || position > count) //判断指定位置是否合法
      return range_error;

   for (int i = count - 1; i >= position; i--)
      entry[i + 1] = entry[i];//从指定位置开始往后,每一个数据向后移一位

   entry[position] = x; //给指定位置赋值
   count++; //计数器加一
   return success;
}

template <class List_entry>
Error_code List<List_entry>::retrieve(int position,List_entry &x) const  //读取指定位置的数据
{
    if((position < 0) || (position >= count)) return range_error; //判断指定位置是否合法
    x = entry[position]; //将指定位置元素赋给x
    return success;
}

template <class List_entry>
Error_code List<List_entry>::replace(int position,const List_entry &x)  //换掉指定位置的数据
{
    if((position < 0) || (position >= count)) return range_error;  //判断指定位置是否合法
    entry[position] = x;//将指定位置元素换成x
    return success;
}

template <class List_entry>
Error_code List<List_entry>::remove(int position,List_entry &x) //删除指定位置的数据
{
    if(count == 0) return underflow;  //判断是否有数据
    if((position < 0) || (position >= count)) return range_error; //判断指定位置是否合法
    x = entry[position]; //将这个位置的值取出赋给x
    for(int i = position+1;i <= count - 1;i++)
    {
        entry[i - 1] = entry[i];  //从position+1往后每个数据都向前挪一个位置
    }
    count--; //计数器减一
    return success;
}

template <class List_entry>
void List<List_entry>::traverse(void (*visit)(List_entry &))  //遍历。参数*visit指向任意一个无参函数,意思是将线性表中的每一项传给函数供函数使用
{
   for (int i = 0; i < count; i++)
      (*visit)(entry[i]);
}

2.用链表实现

#include<iostream>
#include "utility.h"

using namespace std;

template <class Node_entry>
struct Node {
   Node_entry entry;
   Node<Node_entry> *next;
   Node(); //构造函数
   Node(Node_entry data, Node<Node_entry> *link = NULL);
};

template <class Node_entry>
Node<Node_entry>::Node()
{
    next = NULL;
}

template <class Node_entry>
Node<Node_entry>::Node(Node_entry data,Node<Node_entry> *link)
{
    entry = data;
    next = link;
}

template <class List_entry>
class List {
public:
    List();  //构造函数
    int size() const; //返回线性表的大小
    bool full() const; //判满
    bool empty() const; //判空
    void clear();  //清空
    void traverse(void (*visit)(List_entry &)); //遍历
    Error_code retrieve(int position, List_entry &x) const; //读取指定位置的数据
    Error_code replace(int position, const List_entry &x);  //换掉指定位置的数据
    Error_code remove(int position, List_entry &x); //删除指定位置的数据
    Error_code insert(int position, const List_entry &x); //在指定位置插入数据
    ~List(); //析构函数
    List(const List<List_entry> &copy);  //拷贝构造函数
    void operator =(const List<List_entry> &copy); //“=”运算符重载
protected:
    int count; //计数器
    Node<List_entry> *head; //头指针
    Node<List_entry> *set_position(int position) const; //让指针指向指定位置
};

template <class List_entry>
List<List_entry> ::List()  //构造函数
{
    count = 0; //计数器置0
    head = NULL;
}

template <class List_entry>
void List<List_entry>::clear() //清空
{
    Node<List_entry> *p,*q;
    for(p = head;p;p = q)
    {
        q = p->next;
        delete p;
    }  //遍历,有数据的释放空间
    count = 0;
    head = NULL; //置空
}

template <class List_entry>
int List<List_entry>::size() const  //返回线性表大小
{
    return count;
}

template <class List_entry>
bool List<List_entry>::empty() const //判空
{
    return count <= 0;
}

template <class List_entry>
bool List<List_entry>::full() const //判满
{
    return false;
}

template <class List_entry>
void List<List_entry>::traverse(void (*visit)(List_entry &))  //遍历。参数*visit指向任意一个无参函数,意思是将线性表中的每一项传给函数供函数使用
{
    Node<List_entry> *q;
    for(q = head;q;q = q->next) (*visit)(q->entry); //将每个值都以引用形式传回
}

template <class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry &x)   //在指定位置插入数据
{
    if (position < 0 || position > count) return range_error; //判断指定位置是否合法
    Node<List_entry> *new_node, *previous, *following;
    if (position > 0)  //如果指定位置不是表头
    {
      previous = set_position(position - 1); //前一位就是指定位置的前一位
      following = previous->next; //后一位就是指定位置
    }
    else following = head; //如果指定位置是表头,那么它的后一位就是当前表头
    new_node = new Node<List_entry>(x, following); //创建新的数据域,并且将新的数据域与它的后一位连起来
    if (new_node == NULL) return overflow; //判断创建是否成功
    if (position == 0) head = new_node; //如果指定位置是表头,那么新的表头就是插入的数据
    else
        previous->next = new_node; //否则,将前一个与新创建的关联起来
    count++; //计数器加一
    return success;
}

template <class List_entry>
Error_code List<List_entry>::retrieve(int position, List_entry &x) const //读取指定位置的数据
{
    Node<List_entry> *current;
    if (position < 0 || position > count) return range_error; //判断指定位置是否合法
    current = set_position(position); //将current指向指定位置
    x = current->entry; //取出数据赋给x
    return success;
}

template <class List_entry>
Error_code List<List_entry>::replace(int position, const List_entry &x) //替换指定位置的数据
{
    Node<List_entry> *current;
    if (position < 0 || position > count) return range_error; //判断指定位置是否合法
    current = set_position(position); //将current指向指定位置
    current->entry = x; //将要换入的值填入这个位置
    return success;
}

template <class List_entry>
Error_code List<List_entry>::remove(int position, List_entry &x) //删除指定位置的数据
{
    Node<List_entry> *prior,*current;
    if(count == 0) return underflow;
    if (position < 0 || position > count) return range_error; //判断指定位置是否合法
    if(position > 0) //如果指定位置不是表头
    {
        prior = set_position(position - 1); //前一位就是指定位置的前一位
        current = prior->next; //确定当前位置
        prior->next = current->next; //将下一个元素和前一个相连
    }
    else{
        current = head; //如果指定位置是表头,当前位置是表头
        head = head->next; //表头变成下一个元素
    }
    delete current; //删除这个空间
    count--; //计数器减一
    return success;
}

template <class List_entry>
Node<List_entry> *List<List_entry>::set_position(int position) const  //让指针指到指定位置
{
   Node<List_entry> *q = head;
   for (int i = 0; i < position; i++) q = q->next; //遍历,指到找到指定位置
   return q;
}

template <class List_entry>
List<List_entry> ::~List()  //析构函数
{
    clear();
}

template <class List_entry>
List<List_entry> ::List(const List<List_entry> &copy) //拷贝构造函数
{
    count = copy.count; //表中元素个数相同
    Node<List_entry> *new_node,*old_node = copy.head;
    if(old_node == NULL) head = NULL; //如果原表为空则新表也为空
    else{
        new_node = head = new Node<List_entry>(old_node->entry); //定义新表的头
        while(old_node->next != NULL) //遍历旧表
        {
            old_node = old_node->next;
            new_node->next = new Node<List_entry>(old_node->entry);
            new_node = new_node->next;  //将旧表的每一项赋给新的表
        }
    }
}

template <class List_entry>
void List<List_entry> ::operator =(const List<List_entry> &copy) //“=”运算符重载
{
    List new_copy(copy); //调用拷贝构造函数
    clear(); //将这个表清空
    count = new_copy.count;
    head = new_copy.head; //将拷贝的值传给新的表
    new_copy.count = 0;
    new_copy.head = NULL; //清空拷贝的表
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

格格不入ち

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值