C++数据结构——顺序表
2013-06-13 16:48
357 查看
err.h
#ifndef _Err_
#define _Err_
#include<new.h>
class OutOfMemory
{
public:
OutOfMemory(){}
};
int my_Handler(size_t size)
{
throw OutOfMemory();
return 0;
};
_PNH old_handler=_set_new_handler(my_Handler);
//The type _PNH is a pointer to a function that returns type int and takes a single argument of type size_t.
//The _set_new_handler function returns the address of the previous new handler.
class OutOfBound
{
public:
OutOfBound(){}
};
#endif
List.h
#ifndef _LinearList_
#define _LinearList_
#include<iostream>
#include"err.h"
using namespace std;
template<class T>
class LinearList
{
private:
T *elements;
int size;
int length;
public:
LinearList(int size=10);
~LinearList();
LinearList<T> & clearList();
bool listEmpty()const;
int listLength()const;
bool getElem(int k,T &x)const;
int locateElem(T const &x)const;
T priorElem(T const &x)const;
T nextElem(T const &x)const;
LinearList<T> &listInsert(int k,T const x);
LinearList<T> &listDelete(int k);
friend ostream &operator<<(ostream &os,LinearList<T> &L)
{
if(L.length>0)
{
for(int i=0;i<L.length;i++)
os<<L.elements[i]<<"\t";
}
else
{
os<<"list is empty"<<endl;
}
return os;
};
};
#endif
List.cpp
#include"List.h"
template<class T>
LinearList<T>::LinearList(int size)
{
this->size=size;
length=0;
elements=new T[size];
};
template<class T>
LinearList<T>::~LinearList()
{
delete [] elements;
};
template<class T>
LinearList<T> & LinearList<T>::clearList()
{
delete [] elements;
elements=new T[size];
return *this;
};
template<class T>
bool LinearList<T>::listEmpty()const
{
return length==size;
};
template<class T>
int LinearList<T>::listLength()const
{
return length;
};
template<class T>
bool LinearList<T>:: getElem(int k,T &x)const
{
if(k<1||k>length)
{
throw OutOfBound();
return false;
}
x=elements[k-1];
return true;
};
template<class T>
int LinearList<T>:: locateElem(T const &x)const
{
for(int i=0;i<length;i++)
if(elements[i]==x)
return ++i;
return 0; //if LinearList does not exist x;
}
template<class T>
T LinearList<T>:: priorElem(T const &x)const
{
int k=locateElem(x);
if(k)
{
return elements[k-2];
}
return 0;
};
template<class T>
T LinearList<T>:: nextElem(T const &x)const
{
int k=locateElem(x);
if(k)
{
return elements[k];
}
return 0;
};
template<class T>
LinearList<T> &LinearList<T>:: listInsert(int k,T const x)
{
if(k<1||k>size)
{
throw OutOfBound();
return *this;
}
length++;
for(int j=length;j>=k;j--)
elements[j]=elements[j-1];
elements[k-1]=x;
return *this;
};
template<class T>
LinearList<T> & LinearList<T>::listDelete(int k)
{
if(k<1||k>length)
{
throw OutOfBound();
return *this;
}
for(int j=k-1;j<length;j++)
elements[j]=elements[j+1];
length--;
return *this;
};
main_c.cpp
#include"List.cpp"
int main(void)
{
LinearList<int> L(5);
cout<<"befor insert: listEmpty? "<<L.listEmpty()<<endl;
L.listInsert(1,1).listInsert(2,2).listInsert(3,3).listInsert(4,4).listInsert(5,5);
cout<<"after insert: listEmpty? "<<L.listEmpty()<<endl;
cout<<"list are:"<<endl;
cout<<L;
cout<<"list length is: "<<L.listLength()<<endl;
int x;
L.getElem(2,x);
cout<<"getElem(2,x) : "<<x<<endl;
cout<<"locateElem(x) : "<<L.locateElem(x)<<endl;
cout<<"priorElem(x) : "<<L.priorElem(x)<<endl;
cout<<"nextElem(x) : "<<L.nextElem(x)<<endl;
L.listDelete(2);
cout<<"after delet(2),list are:"<<endl;
cout<<L<<endl;
cout<<"list length is: "<<L.listLength()<<endl;
cout<<endl;
system("pause");
return 0;
}
#ifndef _Err_
#define _Err_
#include<new.h>
class OutOfMemory
{
public:
OutOfMemory(){}
};
int my_Handler(size_t size)
{
throw OutOfMemory();
return 0;
};
_PNH old_handler=_set_new_handler(my_Handler);
//The type _PNH is a pointer to a function that returns type int and takes a single argument of type size_t.
//The _set_new_handler function returns the address of the previous new handler.
class OutOfBound
{
public:
OutOfBound(){}
};
#endif
List.h
#ifndef _LinearList_
#define _LinearList_
#include<iostream>
#include"err.h"
using namespace std;
template<class T>
class LinearList
{
private:
T *elements;
int size;
int length;
public:
LinearList(int size=10);
~LinearList();
LinearList<T> & clearList();
bool listEmpty()const;
int listLength()const;
bool getElem(int k,T &x)const;
int locateElem(T const &x)const;
T priorElem(T const &x)const;
T nextElem(T const &x)const;
LinearList<T> &listInsert(int k,T const x);
LinearList<T> &listDelete(int k);
friend ostream &operator<<(ostream &os,LinearList<T> &L)
{
if(L.length>0)
{
for(int i=0;i<L.length;i++)
os<<L.elements[i]<<"\t";
}
else
{
os<<"list is empty"<<endl;
}
return os;
};
};
#endif
List.cpp
#include"List.h"
template<class T>
LinearList<T>::LinearList(int size)
{
this->size=size;
length=0;
elements=new T[size];
};
template<class T>
LinearList<T>::~LinearList()
{
delete [] elements;
};
template<class T>
LinearList<T> & LinearList<T>::clearList()
{
delete [] elements;
elements=new T[size];
return *this;
};
template<class T>
bool LinearList<T>::listEmpty()const
{
return length==size;
};
template<class T>
int LinearList<T>::listLength()const
{
return length;
};
template<class T>
bool LinearList<T>:: getElem(int k,T &x)const
{
if(k<1||k>length)
{
throw OutOfBound();
return false;
}
x=elements[k-1];
return true;
};
template<class T>
int LinearList<T>:: locateElem(T const &x)const
{
for(int i=0;i<length;i++)
if(elements[i]==x)
return ++i;
return 0; //if LinearList does not exist x;
}
template<class T>
T LinearList<T>:: priorElem(T const &x)const
{
int k=locateElem(x);
if(k)
{
return elements[k-2];
}
return 0;
};
template<class T>
T LinearList<T>:: nextElem(T const &x)const
{
int k=locateElem(x);
if(k)
{
return elements[k];
}
return 0;
};
template<class T>
LinearList<T> &LinearList<T>:: listInsert(int k,T const x)
{
if(k<1||k>size)
{
throw OutOfBound();
return *this;
}
length++;
for(int j=length;j>=k;j--)
elements[j]=elements[j-1];
elements[k-1]=x;
return *this;
};
template<class T>
LinearList<T> & LinearList<T>::listDelete(int k)
{
if(k<1||k>length)
{
throw OutOfBound();
return *this;
}
for(int j=k-1;j<length;j++)
elements[j]=elements[j+1];
length--;
return *this;
};
main_c.cpp
#include"List.cpp"
int main(void)
{
LinearList<int> L(5);
cout<<"befor insert: listEmpty? "<<L.listEmpty()<<endl;
L.listInsert(1,1).listInsert(2,2).listInsert(3,3).listInsert(4,4).listInsert(5,5);
cout<<"after insert: listEmpty? "<<L.listEmpty()<<endl;
cout<<"list are:"<<endl;
cout<<L;
cout<<"list length is: "<<L.listLength()<<endl;
int x;
L.getElem(2,x);
cout<<"getElem(2,x) : "<<x<<endl;
cout<<"locateElem(x) : "<<L.locateElem(x)<<endl;
cout<<"priorElem(x) : "<<L.priorElem(x)<<endl;
cout<<"nextElem(x) : "<<L.nextElem(x)<<endl;
L.listDelete(2);
cout<<"after delet(2),list are:"<<endl;
cout<<L<<endl;
cout<<"list length is: "<<L.listLength()<<endl;
cout<<endl;
system("pause");
return 0;
}
运行输出结果:
相关文章推荐
- C++数据结构之线性顺序表基本操作
- 数据结构顺序表链表C++实现
- 数据结构之——C++顺序表
- 计蒜客 数据结构 顺序表 C++
- [C++]数据结构:排序算法Part2----快速排序、归并排序、箱子排序、基数排序
- C++数据结构之map----第一篇
- 数据结构顺序表应用4-2:元素位置互换之逆置算法(数据改进)
- C++数据结构之字符串的KMP算法
- c/c++ 数据结构之位图(bitmap)详解
- 数据结构图文解析之:二叉堆详解及C++模板实现
- 数据结构与算法之枚举(穷举)法 C++实现
- c/c++ 数据结构之位图(bitmap)具体解释
- c++之数据结构
- 货物管理系统(数据结构顺序表)
- 数据结构之栈的初始化、创建、入栈、出栈、销毁-c++代码实现及运行实例结果
- C++容器&数据结构
- c++ 数据容器结构及比较
- 数据结构笔记(1)-顺序表
- C++存储数据结构之二CArray
- C++数据结构之AVL树