zoukankan      html  css  js  c++  java
  • 二、线性表的顺序存储结构

    1、顺序存储的定义

    定义:线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表中的数据元素

    | (a_1) | ...... | (a_{i-1}) | (a_i) | ...... | (a_{n-1}) |

    2、相关操作

    程序设计思路:

    可以用一维数组来实现顺序存储结构

    • 存储空间:T* m_array
    • 当前长度:int m_length
    template <typename T>
    class SeqList : public  List<T>
    {
    protected:
        T* m_array;
        int m_length;
    }
    

    顺序存储结构的元素获取操作:

    • 判断目标位置是否合法
    • 将目标位置作为数组下标获取元素
    bool SeqList<T>::get(int i, T& e) const
    {
        bool ret = ((0 <= i) && (i < m_length));
        if (ret)
        {
            e = m_array[i];	// 用数组访问的方法获取数组元素
        }
        return ret;
    }
    

    顺序存储结构的元素插入操作:

    • 判断目标位置是否合法
    • 将目标位置及目标位置之后的所有元素后移一个位置
    • 将新元素插入目标位置
    • 线性表长度加1
    bool SeqList<T>::insert(int i, const T& e)
    {
    	bool ret = ((0 <= i) && (i <= m_length));
    	ret = ret && ((m_length + 1) <= capacity());
    	if ( ret )
    	{
    		for(int p = m_length-1; p>=i; p--)
    		{
    			m_array[p+1] = m_array[p];
    		}
    		m_array[i] = e;
    		m_length++;
    	}
    }
    

    顺序存储结构的元素删除操作:

    • 判断目标位置是否合法
    • 将目标位置后的所有元素前移一个位置
    • 线性表长度减1
    bool SeqList<T>::remove(int i)
    {
    	bool ret = ((0 <= i) && (i < m_length));
    	if (ret)
    	{
    		for (int p = i; p < m_length-1; p++)
    		{
    			m_array[p] = m_array[p+1];
    		}
    		m_length--;
    	}
    	return ret;
    }
    
  • 相关阅读:
    Toad 常用快捷键
    Oracle Form删除list项
    不得重新使用的登录口令
    OE_ORDER_PUB.PROCESS_ORDER to Release a hold on sales order in R12
    OE_ORDER_PUB.PROCESS_ORDER to Apply hold on a sales order
    说明性弹性域段
    使用VPD解决EBS中信息屏蔽问题
    Oracle EBS客户化程序中格式化金额
    Form开发中组件控制的几个常用方法
    .Net的差评
  • 原文地址:https://www.cnblogs.com/chenke1731/p/9479585.html
Copyright © 2011-2022 走看看