经典数据结构之间接寻址
来源:互联网 发布:软件需求调研表 编辑:程序博客网 时间:2024/04/29 11:35
线性表主要有两中表现方式,一种是连续存储式,例如数组,另一种是非连续存储式,例如链表。 相比于链表,数组最大的好处就在于可以随机存取,链表的最大优势是,能够有效利用存储空间,合理的添加,删除操作只需要o(1)。于是诞生了间接寻址,即是把数组与链表的优点结合起来。
代码如下:
#ifndef INDIRECTLIST_HH#define INDIRECTLIST_HH#include <stdio.h>template<typename T>class CIndrectList{private: // pointe to a potiner which point to a T type object; T** m_ppTable; // length of CurrentIndrectList; unsigned int m_nLength; unsigned int m_nSize;public: // constructor; CIndrectList(int size); ~CIndrectList(); // methods; // Insert; CIndrectList& mInsert(int k, const T& data); // Delete; CIndrectList& mDelete(int k, const T& data); // find; bool mFind(int k, const T& data) const; // length; int mLength() const; // size; int mSize() const;}; // CIndrectList;// implement;// constructors;template<typename T>CIndrectList<T>::CIndrectList(int size = 10): m_nLength(0),m_nSize(size){ m_ppTable = new T*[m_nSize];}template<typename T>CIndrectList<T>::~CIndrectList(){ for(int i = 0; i < m_nLength; i ++) delete m_ppTable[i]; delete [] m_ppTable;}// Insert data after (k-1)th element;template<typename T>CIndrectList<T>& CIndrectList<T>::mInsert(int k, const T& data){ if( k < 0 || k > m_nLength){ printf("Input wrong index in Insert\n;"); return *this; }if(m_nLength == m_nSize){ printf("Not enough Space\n");return *this;} // move elements to contain k; for(int i = m_nLength - 1; i >= k; i --){ m_ppTable[i + 1] = m_ppTable[i]; } // add data; m_ppTable[k] = new T; *m_ppTable[k] = data; m_nLength ++; return *this;}// Delete;template<typename T>CIndrectList<T>& CIndrectList<T>::mDelete(int k, const T& data){ if(mFind(k,data)){ delete m_ppTable[k - 1]; for(int i = k; i < m_nLength; i ++) m_ppTable[i - 1] = m_ppTable[i]; m_nLength --; } return *this;}template<typename T>bool CIndrectList<T>::mFind(int k, const T& data) const{ if(k > m_nLength || k <= 0){ printf("Input wrong index in Find\n;"); return false;} return (*(m_ppTable[k - 1]) == data) ? true : false;}template<typename T>int CIndrectList<T>::mLength() const{ return m_nLength;}template<typename T>int CIndrectList<T>::mSize() const{ return m_nSize;}#endif
测试代码如下:
#include "IndrectList.h"#include <iostream>#include <stdlib.h>using namespace std;int main(){CIndrectList<int> list(5);list.mInsert(0,1);cout << list.mLength() << endl;list.mInsert(1,2);cout << list.mLength() << endl; list.mDelete(1,1);list.mDelete(1,2);list.mDelete(2,2);cout << list.mLength() << endl; system("pause"); return 0;}
结果如下:
12Input wrong index in Find0请按任意键继续. . .
- 经典数据结构之间接寻址
- 数据结构之间接寻址
- 实验二之间接寻址
- 数据结构实验(二):间接寻址
- c++数据结构之线性表:间接寻址类模板的实现
- 间接寻址
- 间接寻址
- 间接寻址
- 线性表综合实验之间接寻址
- 《数据结构》间接寻址类的定义参考部分代码
- 数据结构与算法C++描述(3)---间接寻址
- 数据结构|创建学生成绩的间接寻址(实验2.5)
- 数据结构|创建学生成绩的间接寻址(实验2.5)
- 数据结构:实验二线性表综合实践⑤间接寻址
- AT&T汇编心得之间接寻址和LEA指令
- AT&T汇编心得之间接寻址和LEA指令
- 实验二 线性表综合实验之间接寻址
- 实验二 线性表综合实验之《间接寻址》
- write()
- InputStreamReader 、 BufferedReader
- 2800c cisco 寄存器值为0x2102时保存配置才生效
- 程序员面试100题:求子数组的最大和
- processing学习实例(1)点击区域实现颜色改变
- 经典数据结构之间接寻址
- GUI
- netlink简单实例
- open的O_DIRECT选项
- 使用MVC结构计算梯形面积
- 友好城市
- 网易博客#include<cstdio>转载
- 无锁编程入门
- CSS3+HTML5学习笔记四--CSS三栏布局-宽度只适应