顺序链表,动态数组实现

//SeqList.h

//顺序表类的定义

#ifndef SEQLIST_H
#define SEQLIST_H

#include <iostream>
using namespace std;

#define true 1
#define false 0

template<class Type>            //模板定义
class SeqList
{
public:
 SeqList(int defaultSize);
 ~SeqList()
    {delete []data;}
    int Length()const{return last + 1;} //计算表长度           
    int Find(Type &x);           //定位函数,在表中找到x的位置
    bool IsIn(Type &x);                //判断函数,判断x是否在表中
    void Insert(Type &x,int j);          //插入函数,在位置j插入x
    void Remove(Type &x);              //删除函数,删除x
    bool IsEmpty();                    //判断表是否为空
    bool IsFull();                    //判断表是否满
    Type Get(int i);                  //取第i个元素的值
    void FuZhi();                     //给数组赋值
    void ShowList();                  //输出数组
private:
    Type *data;                       //顺序表的存放数组
    int MaxSize;                    
    int last;                        //顺序表当前已存表项的最后位置
};

 

template<class Type>
SeqList<Type>::SeqList(int defaultSize)
{
    if(defaultSize > 0)
    {
        MaxSize = defaultSize;
        last = -1;
        data = new Type[MaxSize];
    }
}

template<class Type>
int SeqList<Type>::Find(Type &x)
{
    if(IsIn(x))
    {
        for(int i = 0;i < last;i++)
            if(x == data[i])
                return i;
    }
    else
        return -1;
}

template<class Type>
bool SeqList<Type>::IsIn(Type &x)
{
    int i;
    for(i = 0;i < last;i++)
    {
        if(x == data[i])
            return 1;
        else continue;
    }
    if(i == last)
        return 0;
}

template<class Type>
void SeqList<Type>::Insert(Type &x,int j)
{
    if(!IsFull() && j < last)
    {
        for(int i = last;i >= j;i--)
            data[i+1] = data[i];
        data[j] = x;
        last++;
    }
    else
        cout<<"Is Full or is wrong location!"<<endl;
}

template<class Type>
void SeqList<Type>::Remove(Type &x)
{
    int i;
    if(IsIn(x))
    {
       
        i = Find(x);
        for(int j = i;j < last;j++)
            data[j] = data[j+1];
        last--;
    }
    else
        cout<<"Is not in."<<endl;
}

template<class Type>
bool SeqList<Type>::IsEmpty()
{
    if(last == -1)
        return 1;
    else
        return 0;
}

template<class Type>
bool SeqList<Type>::IsFull()
{
    if(last == MaxSize)
        return 1;
    else
        return 0;
}

template<class Type>
Type SeqList<Type>::Get(int i)
{
    return data[i-1];
}

template<class Type>
void SeqList<Type>::FuZhi()
{
    int x = 0;
    cout<<"please input:";
    for(int j = 0;x != -1;j++)
    {
        cin>>x;
        data[j] = x;
        last++;
    }
}

template<class Type>
void SeqList<Type>::ShowList()
{
    cout<<"the new list is:";
    for(int i = 0;i < last;i++)
        cout<<data[i]<<" ";
    cout<<endl;
}
#endif;

 

//main.cpp

#include <iostream>
#include "SeqList.h"
using namespace std;

void main()
{
    int x;
    cin>>x;
    SeqList<int> seq(x);/


    int remove,insert,location,Tnum;
    seq.FuZhi();
    seq.ShowList();

 

    cout<<"please input the number remove:";
    cin>>remove;
    seq.Remove(remove);
    seq.ShowList();

 

    cout<<"please input the number insert and the location:";
    cin>>insert;
    cin>>location;
    seq.Insert(insert,location);
    seq.ShowList();

 

    cout<<"please input the location to get element:";
    cin>>Tnum;
    cout<<seq.Get(Tnum)<<endl;
}

posted @ 2010-04-16 14:07  Java EE  阅读(200)  评论(0编辑  收藏  举报