上一篇:http://www.BkJia.com/kf/201201/116965.html
C++實現一個順序表
[cpp] //Algri.h
#include "stdafx.h"
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
namespace Jeffery
{
template<class T>
class SeqList
{
public:
int Maxsize;
private:
vector<T> data;
int Last;
public:
T &operator[](size_t index){
return data[index];
}
SeqList(int size){
data=vector<T>(size);
Maxsize=size;
Last=-1;
}
void Append(T item){
if(IsFull())
cout<<"List is full";
else
data[++Last]=item;
}
void Clear(){
Last=-1;
}
T Delete(int i){
T tmp;
if(IsEmpty())
cout<<"List is empty";
else if(i<0||i>Last)
cout<<"Position is error!";
else if(i==Last)
tmp=data[Last--];
else{
tmp=data[i];
for(int j=i+1;j<=Last;++j){
data[j-1]=data[j];
}
--Last;
}
return tmp;
}
T GetItem(int i){
if(IsEmpty()||i<0||i>Last){
cout<<"List is empty or position is error!";
return T();
}
else
return data[i];
}
int GetLength(){
return Last+1;
}
void Insert(T item, int i){
if(IsFull())
cout<<"List is full";
else if(i<0||i>=Last+2)
cout<<"Position is error!";
else if(i==Last+1)
data[++Last]=item;
else{
for(int j=Last++;j>=i;--j){
data[j+1]=data[j];
}
data[i]=item;
}
}
bool IsEmpty(){
if(Last==-1)
return true;
else
return false;
}
bool IsFull(){
if(Last==Maxsize-1)
return true;
else
return false;
}
int Locate(T value){
if(IsEmpty()){
cout<<"List is Empty!";
return -1;
}
for(int i=0;i<=Last;++i){
if(value==data[i])
return i;
}
return -1;
}
};
}
//Algri.h
#include "stdafx.h"
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
namespace Jeffery
{
template<class T>
class SeqList
{
public:
int Maxsize;
private:
vector<T> data;
int Last;
public:
T &operator[](size_t index){
return data[index];
}
SeqList(int size){
data=vector<T>(size);
Maxsize=size;
Last=-1;
}
void Append(T item){
if(IsFull())
cout<<"List is full";
else
data[++Last]=item;
}
void Clear(){
Last=-1;
}
T Delete(int i){
T tmp;
if(IsEmpty())
cout<<"List is empty";
else if(i<0||i>Last)
cout<<"Position is error!";
else if(i==Last)
tmp=data[Last--];
else{
tmp=data[i];
for(int j=i+1;j<=Last;++j){
data[j-1]=data[j];
}
--Last;
}
return tmp;
}
T GetItem(int i){
if(IsEmpty()||i<0||i>Last){
cout<<"List is empty or position is error!";
return T();
}
else
return data[i];
}
int GetLength(){
return Last+1;
}
void Insert(T item, int i){
if(IsFull())
cout<<"List is full";
else if(i<0||i>=Last+2)
cout<<"Position is error!";
else if(i==Last+1)
data[++Last]=item;
else{
for(int j=Last++;j>=i;--j){
data[j+1]=data[j];
}
data[i]=item;
}
}
bool IsEmpty(){
if(Last==-1)
return true;
else
return false;
}
bool IsFull(){
if(Last==Maxsize-1)
return true;
else
return false;
}
int Locate(T value){
if(IsEmpty()){
cout<<"List is Empty!";
return -1;
}
for(int i=0;i<=Last;++i){
if(value==data[i])
return i;
}
return -1;
}
};
}[cpp] #include "stdafx.h"
#include "Algri.h"
#include <iostream>
using namespace std;
using namespace Jeffery;
int _tmain(int argc, _TCHAR* argv[])
{
SeqList<int> li= SeqList<int>(10);
for(int i=0;i<8;i++)
{
li.Append(i);
}
for(int i=0;i<li.GetLength();i++)
{
cout<<li[i]<<", ";
}
cout<<endl;
li.Insert(100,5);
for(int i=0;i<li.GetLength();i++)
{
cout<<li[i]<<", ";
}
cout<<endl;
li.Insert(1000,9);
for(int i=0;i<li.GetLength();i++)
{
cout<<li[i]<<", ";
}
cout<<endl;
li.Delete(2);
for(int i=0;i<li.GetLength();i++)
{
cout<<li[i]<<", ";
}
cout<<endl;
cout<<li.GetItem(7)<<endl;
cout<<li.Locate(4)<<endl;
return 0;
}
#include "stdafx.h"
#include "Algri.h"
#include <iostream>
using namespace std;
using namespace Jeffery;
int _tmain(int argc, _TCHAR* argv[])
{
SeqList<int> li= SeqList<int>(10);
for(int i=0;i<8;i++)
{
li.Append(i);
}
for(int i=0;i<li.GetLength();i++)
{
cout<<li[i]<<", ";
}
cout<<endl;
li.Insert(100,5);
for(int i=0;i<li.GetLength();i++)
{
cout<<li[i]<<", ";
}
cout<<endl;
li.Insert(1000,9);
for(int i=0;i<li.GetLength();i++)
{
cout<<li[i]<<", ";
}
cout<<endl;
li.Delete(2);
for(int i=0;i<li.GetLength();i++)
{
cout<<li[i]<<", ";
}
cout<<endl;
cout<<li.GetItem(7)<<endl;
cout<<li.Locate(4)<<endl;
return 0;
}
摘自 xufei96的專欄