数据结构之顺序表
最近在找工作,复习一些基础知识,所以想把以前学过的东西总结一下。
在华为面试的时候被问到顺序表的优点,当然主要是与链表相对而言:
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;
}