设为首页 加入收藏

TOP

线性表 - 间接寻址实现线性表
2015-07-20 17:42:15 来源: 作者: 【 】 浏览:1
Tags:线性 间接 寻址 实现

List.h

/********************************************************************
	purpose:	间接寻址实现线性表
	author:		xianyun1230
	QQ:		836663997
	e-mail:		xianyun1230@163.com
	created:	2014/02/17
*********************************************************************/

#include 
  
   

template
   
     class List { public: List(int MaxListSize = 10); ~List(); bool empty() const {return _length == 0;} int length() const {return _length;} int search(const T& x) const; bool del(int k, T &val); bool insert(int k, const T& x); void show() const; private : T **table; //table指向数组,数组中存储类型T 的指针 int _length,MaxSize; }; template
    
      List
     
      ::List(int MaxListSize) { MaxSize = MaxListSize; table = new T *[MaxSize]; _length = 0; } template
      
        List
       
        ::~List() { for (int i = 0; i < _length; i++) delete table[i]; delete [] table; } template
        
          bool List
         
          ::del(int k, T &val) { if (k < 0 || k >= _length) return false; val = *table[k]; delete table[k]; for (int i = k; i < _length - 1; i++) table[i] = table[i+1]; _length--; return true; } template
          
            bool List
           
            ::insert(int k, const T& x) { if (k < 0 || k > _length || _length == MaxSize) return false; for (int i = _length - 1; i >= k; i--) table[i+1] = table[i]; table[k] = new T; *table[k] = x; _length++; return true; } template
            
              void List
             
              ::show() const { std::cout <<"共" <<_length <<"项:\n"; for (int i=0; i < _length; ++i) std::cout <<*table[i] <
              
               

main.cpp

#include "List.h"

int main()
{
	List
                
                  link(12);
	link.insert(0,21);
	link.insert(0,12);
	link.insert(2,34);
	link.insert(1,12);
	link.insert(5,13);	//没有第五项,不会插入
	link.show();
	int a;
	link.del(2,a);
	link.show();
	std::cout <
                 
                  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇leetCode系列----Unique Paths II 下一篇BestCoder Round #9

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·工业机器人TCP校准中 (2025-12-25 05:19:17)
·opc 通讯协议与 TCP (2025-12-25 05:19:15)
·labview中tcp/ip通信 (2025-12-25 05:19:13)
·新书介绍《Python数 (2025-12-25 04:49:47)
·怎么利用 Python 进 (2025-12-25 04:49:45)