当前位置:编程学习 > C#/ASP.NET >>

数据结构 C#语言版 第2章 线性表(3)

上一篇:http://www.zzzyk.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!&quo

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,