码迷,mamicode.com
首页 > 编程语言 > 详细

C++数据结构 顺序表的实现(模板类的实现)

时间:2015-05-24 17:33:50      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:数据结构   顺序表   c++   模板类   

利用模板类实现顺序表的操作

实现的功能: 

1.尾插,2.头插,3.显示,4.尾删,5.头删,6.按位置,7.插按值插,8.按位置删,9.按值删,10.按值查,11.求表长,12.清除数据,13.摧毁该顺序表,14.反转,15.排序(冒泡排序,快速排序)。

头文件源代码:

#pragma once        // 防止重复编译

#include<iostream>
using namespace std;

template<class Type>
class SeqList
{
public:
	SeqList(size_t sz=INIT_SIZE);
public:
	bool isfull()const
	{return size>=capacity;}
	bool isempty()const
	{return size==0;}
public:
	void push_back(const Type &x);   //尾插
	void push_front(const Type &x);  //头插
	void show_list();                //显示
	void pop_back();                 //尾删
	void pop_front();                //头删
	void insert_pos(int pos,const Type &x);//按位置插
	void insert_val(const Type &x);        //按值插
	void delete_pos(int pos);              //按位置删
	void delete_val(const Type &x);        //按值删
	int  find(const Type &key);            //按值查
	int  length()const;                    //求表长
	void clear();                          //清除数据
	void destroy();                        //摧毁该顺序表
	void reserv();                         //反转
	void sort(/*int low,int high*/);       //排序
private:
	enum{INIT_SIZE=8};
	Type *base;
	size_t capacity;
	size_t size;
};


template<class Type>
SeqList<Type>::SeqList(size_t sz)
{
	capacity = sz > INIT_SIZE ? sz : INIT_SIZE;
	base = new Type[capacity];
	size = 0;
}

template<class Type>
void SeqList<Type>::push_back(const Type &x)
{
	if(isfull())
	{
		cout<<"顺序表已满,不能插入!"<<endl;
		return;
	}
	base[size++] = x;
}

template<class Type>
void SeqList<Type>::push_front(const Type &x)
{
	if(isfull())
	{
		cout<<"顺序表已满,不能插入!"<<endl;
		return;
	}

	for(int i=size; i>0; --i)
	{
		base[i] = base[i-1];
	}
	base[0] = x;
	size++;
}

template<class Type>
void SeqList<Type>::show_list()
{
	for(int i=0; i<size; ++i)
	{
		cout<<base[i]<<" ";
	}
	cout<<endl;
}

template<class Type>
void SeqList<Type>::pop_back()
{
    if(isempty())
    {
        cout<<"顺序表已满"<<endl;
        return;
    }
    size = size-1;
}

template<class Type>
void SeqList<Type>::pop_front()
{
    int i;
    for(i = 0;i<size-1;i++)
    {
        base[i]=base[i+1];
    }
    size--;
}

template<class Type>
void SeqList<Type>::insert_pos(int pos,const Type &x)
{
	if(pos<0 || pos>size)
	{
		cout<<"要插入的位置非法!"<<endl;
		return;
	}
	if(isfull())
	{
		cout<<"顺序表已满,不能插入!"<<endl;
		return;
	}

	for(int i=size; i>pos; --i)
	{
		base[i] = base[i-1];
	}
	base[pos] = x;
	size++;
}

template<class Type>
void SeqList<Type>::insert_val(const Type &x)
{
    int pos;
    pos = find(x);
    insert_pos(pos,x);
}

template<class Type>
void SeqList<Type>::delete_pos(int pos)
{
    int i;
    for(i = pos;i<size-1;++i)
    {
        base[i] = base[i+1];
    }
    size--;
}

template<class Type>
void SeqList<Type>::delete_val(const Type &x)
{
	int pos = find(x);
	if(pos == -1)
	{
		return;
	}

	for(int i=pos; i<size; ++i)
	{
		base[i] = base[i+1];
	}
	size--;
}

template<class Type>
int SeqList<Type>::find(const Type &key)
{
	for(int i=0; i<size; ++i)
	{
		if(base[i] == key)
			return i;
	}
	return -1;
}

template<class Type>
int SeqList<Type>::length()const
{
    cout<<"表长是:"<<size<<endl;
    return size;
}

template<class Type>
void SeqList<Type>::clear()
{
    while(size)
    {
        base[size--] = NULL;
    }
}

template<class Type>
void SeqList<Type>::destroy()
{
    int i;
    delete base;
    base = NULL;
    capacity = 0;
    size = 0;
}

template<class Type>
void SeqList<Type>::reserv()
{
    int i = 0;
    int m = size - 1;
    for(;i<=((size-1)/2);++i)
        {
            int tmp = base[i];
            base[i] = base[m];
            base[m] = tmp;
            m--;
        }
}

template<class Type>
void SeqList<Type>::sort()//排序
{


	for (int i=0;i<=size;i++)
		for (int j=i+1;j<=size-1;j++)
		{
			if(base[i]>base[j])
			{
				int tmp = base[j];
				base[j]=base[i];
				base[i]=tmp;
			}
		}
}
/*
template<class Type>     ///快速排序
void SeqList<Type>::sort(int low,int high)
{
    if(low >= high)
    {
        return;
    }
    int first = low;
    int last = high;
    int key = base[first];             //用字表的第一个记录作为枢轴
    while(first < last)
    {
        while(first < last && base[last] >= key)
        {
            last--;
        }
        base[first] = base[last];//将比第一个小的移到低端
        while(first < last && base[first] <= key)
        {
            first++;
        }
            base[last] = base[first];//将比第一个大的移到高端
    }
            base[first] = key;//枢轴记录到位
            sort(low, first-1);
            sort(first+1, high);
}
*/


主函数:

#include"SeqList.h"

int main()
{
	SeqList<int> mylist;
	int select = 1;
	int Item;
	int pos;
	while(select)
	{
		cout<<"**************************************"<<endl;
		cout<<"* [1] push_back       [2] push_front *"<<endl;
		cout<<"* [3] show_list       [0] quit_system*"<<endl;
		cout<<"* [4] pop_back        [5] pop_front  *"<<endl;
		cout<<"* [6] insert_pos      [7] insert_val *"<<endl;
		cout<<"* [8] delete_pos      [9] delete_val *"<<endl;
		cout<<"* [10] find           [11]length     *"<<endl;
		cout<<"* [12] clear          [13]destroy    *"<<endl;
		cout<<"* [14] reserv         [15]sort       *"<<endl;
		cout<<"**************************************"<<endl;
		cout<<"请选择:>";
		cin>>select;
		switch(select)
		{
		case 1:
			cout<<"请输入要插入的值(-1结束):>";
			while(cin>>Item, Item!=-1)
			{
				mylist.push_back(Item);
			}
			break;
		case 2:
			cout<<"请输入要插入的值(-1结束):>";
			while(cin>>Item, Item!=-1)
			{
				mylist.push_front(Item);
			}
			break;
		case 3:
			mylist.show_list();
			break;
        case 4:
            mylist.pop_back();
            break;
        case 5:
            mylist.pop_front();
            break;
		case 6:
			cout<<"请输入要插入的位置:>";
			cin>>pos;
			cout<<"请输入要插入的值:>";
			cin>>Item;
			mylist.insert_pos(pos,Item);
			break;
        case 7:
            cout<<"请输入要插入的值:>";
            cin>>Item;
            mylist.insert_val(Item);
        case 8:
            cout<<"请输入要删除的位置:>";
            cin>>pos;
            mylist.delete_pos(pos);
            break;
		case 9:
			cout<<"请输入要删除的值:>";
			cin>>Item;
			mylist.delete_val(Item);
			break;
		case 10:
			cout<<"请输入要查找的值:>";
			cin>>Item;
			int pos;
			pos = mylist.find(Item);
			break;
        case 11:
            mylist.length();
            break;
        case 12:
            mylist.clear();
            break;
        case 13:
            mylist.destroy();
            break;
        case 14:
            mylist.reserv();
            break;
        case 15:
            //int a;
           //a = mylist.length();
            mylist.sort(/*0,a-1*/);
            break;
		default:
			break;
		}
	}
}



C++数据结构 顺序表的实现(模板类的实现)

标签:数据结构   顺序表   c++   模板类   

原文地址:http://blog.csdn.net/zhongqi0808/article/details/45952137

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!