数据结构之顺序表

最近在找工作,复习一些基础知识,所以想把以前学过的东西总结一下。

在华为面试的时候被问到顺序表的优点,当然主要是与链表相对而言:

1)随机访问,存储密度大。

2)逻辑上相邻的物理上也相邻.

顺序表的缺点:

1)插入删除时不方便,要移动大量元素.

实现代码:

/***************************************************************************
 
 * file name : SeqList.h
 * created : 2011/11/01
 * description :
 * author : Gavin Dai XLX
 * update :

****************************************************************************/
#ifndef __SEQLIST_H
#define __SEQLIST_H

#include <iostream>
using namespace std;

const int g_iDefaultSize = 100;

template <class Type>

class SeqList
{

public:

 SeqList(int sz = g_iDefaultSize) : m_iMaxSize(sz), m_iCurrentSize(-1)
 {
  if (sz > 0)
  {
   m_elements = new Type[m_iMaxSize];
  }
 }

 ~SeqList()
 {
  delete[] m_elements;
 }

 int length() const
 {
  return m_iCurrentSize + 1;
 }

 int find(Type x)const;

 int isElement(Type x)const;
 
 int insert(Type x, int i);

 int remove(Type x);

 int isEmpty()
 {
  return m_iCurrentSize == -1;
 }

 int isFull()
 {
  return m_iCurrentSize == m_iMaxSize - 1;
 }

 Type get(int i)
 {
  return (i < 0 || i > m_iCurrentSize) ? (cout<<"can't find the element"<<endl,0) : m_elements[i];
 }

 void print();

private:

 Type *m_elements;

 const int m_iMaxSize;

 int m_iCurrentSize;

};

#endif

//下面是顺序表的实现文件 SeqList.cpp

/***************************************************************************
 
 * file name : SeqList.cpp
 * created : 2011/11/01
 * description :
 * author : Gavin Dai XLX
 * update :

****************************************************************************/

#include "SeqList.h"

template <class Type>
int SeqList<Type>::find(Type x)const
{
 for (int i = 0; i<=m_iCurrentSize; i++)
 {
  if (m_elements[i] == x)
  {
   return i;
  }
 }
 cout<<"can't find the element you want to find"<<endl;
 return -1;
}

template <class Type>
int SeqList<Type>::isElement(Type x)const
{
 if (find(x) == -1)
 {
  return 0;
 }
 return 1;
}

template <class Type>
int SeqList<Type>::insert(Type x, int i)
{
 if (i < 0 || i > (m_iCurrentSize +1) || m_iCurrentSize == m_iMaxSize - 1 )
 {
  cout<<"the operate is illegal"<<endl;
  return 0;
 }
 m_iCurrentSize++;
 for (int j = m_iCurrentSize; j > i; j--)
 {
  m_elements[j] = m_elements[j-1];
 }
 m_elements[i] = x;
 return 1;
}

template<class Type>
int SeqList<Type>::remove(Type x)
{
 int size = m_iCurrentSize;
 for (int i=0; i<=m_iCurrentSize;i++)
 {
  if (m_elements[i]==x)
  {
   for (int j=i;j<=m_iCurrentSize;j++)
   {
    m_elements[j] = m_elements[j+1];
   }
   m_iCurrentSize--;
   continue;
  }
 }
 if (size==m_iCurrentSize)
 {
  cout<<"can't find the element you want to remove"<<endl;
  return 0;
 }
 return 1;
}

template <class Type>
void SeqList<Type>::print()
{
 for (int i = 0; i <= m_iCurrentSize; i++)
 {
  cout<<i+1<<":\t"<<m_elements[i]<<endl;
 }
 cout<<endl<<endl;
}

//测试 主文件 main.cpp

/***************************************************************************
 
 * file name : main.cpp

 * created : 2011/11/01
 * description :
 * author : Gavin Dai XLX
 * update :

****************************************************************************/

#include "SeqList.cpp"   //注意用了模板,所以这里要包含CPP而不是.h哦 >)(<

int main()
{
 SeqList<int> test(16);
 int array[15] = { 2, 5, 6, 1, 9, 7, 6, 4, 3, 2, 9, 7, 7, 9, 12};
 for (int i = 0; i < 15; i++)
 {
  test.insert(array[i], 0);
 }
 test.insert(100, 0);
 test.print();
 test.find(20);
 test.remove(2);
 test.print();
 system("pause");
 return 1;
}

转载于:https://www.cnblogs.com/GavinDai/archive/2011/11/01/2232101.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值