【c++版数据结构】之顺序表的实现

SeqList.h

#ifndef SEQLIST_H
#define SEQLIST_H
#include
using namespace std;

typedef enum{TRUE,FALSE}Status;
template
class SeqList
{
private:
	enum{DefaultSize = 10};  //顺序表的默认长度
	Type *base;              //存放元素数组的首地址
	int capacity;            //顺序表的容量
	int size;                //顺序表实际存放元素的个数
private://为内部函数所调用,不希望在类外访问
	bool IsFull()
	{
		if (size == capacity)
			return true;
		else
			return false;
	}
	bool IsEmpty()
	{
		if (size == 0)
			return true;
		else
			return false;
	}
	Status destory()
	{
		if (base == NULL)
			return FALSE;
		delete[] base;
		base = NULL;
		capacity = size = 0;
		return TRUE;
	}
public:
	SeqList(int sz = DefaultSize)
	{
		capacity = sz > capacity ? sz : capacity;
		base = new Type[capacity];
		size = 0;
	}
	~SeqList()
	{
		destory();
	}
	Status push_back(const Type &x)
	{
		if (IsFull())
		{
			cout << "空间已满" << x << "不能插入" << endl;
			return FALSE;
		}
		base[size++] = x;
		return TRUE;	
	}
	Status push_front(const Type &x)
	{
		if (IsFull())
		{
			cout << "空间已满" << x << "不能插入" << endl;
			return FALSE;
		}
		for (int i = size - 1; i >= 0; --i)  //------------------>从最后一个元素到第一个元素依次往后移动一个位置
			base[i+ 1] = base[i];
		base[0] = x;
		size++;
		return FALSE;
	}
	void show_list()
	{
		for (int i = 0; i < size; ++i)
			cout << base[i] << " ";
		cout << endl;
	}
	Status pop_back(Type &x)//将删除的元素存放在x中,以备不时之需
	{
		if (IsEmpty())
		{
		
			cout << "空间已空,不能删除" << endl;
			return FALSE;
		}
		x = base[--size];
		return TRUE;
	}
	Status pop_front(Type &x)
	{
		if (IsEmpty())
		{
			cout << "空间已空,不能删除" << endl;
			return FALSE;
		}
		x = base[0];//将删除的元素保存在x中,以备不时之需
		for (int i = 1; i <= size - 1; ++i)    //--------------->从第二个元素到最后一个元素,依次覆盖前一个元素
			base[i - 1] = base[i];
		size--;
		return TRUE;
	}
	Status insert_pos(int pos,const Type &x)
	{
		if (pos < 0 || pos >= size)
		{
			cout << "插入位置无效" << endl;
			return FALSE;
		}
		if (IsFull())
		{
			cout << "空间已满" << x << "不能插入" << endl;
			return FALSE;
		}
		for (int i = size - 1; i >= pos; --i)//--------------->从最后一个元素到所插入的位置的元素依次往后移动一个位置,为所要插入的元素留出位置
			base[i + 1] = base[i];
		base[pos] = x;
		size++;
		return TRUE;
	}
	Status delete_pos(int pos,Type &x)
	{
		if (pos < 0 || pos >= size)
		{
			cout << "删除位置无效" << endl;
			return FALSE;
		}
		if (IsEmpty())
		{
			cout << "空间已空,不能删除" << endl;
			return FALSE;
		}
		x = base[pos];
		for (int i = pos + 1; i < size; ++i)//从删除位置后面的第一个元素开始依次覆盖前一个元素
			base[i - 1] = base[i];
		size--;
		return TRUE;
	}
	int find(const Type &x)
	{
		for (int i = 0; i < size; ++i)
		{
			if (base[i] == x)
				return i;
		}
		return -1;
	}
	void sort()//冒泡排序
	{
		for (int i = 0; i < size - 1; ++i)//排序size-1次
		{
			for (int j = 0; j < size - 1 - i; ++j)
			{
				if (base[j] > base[j + 1])
				{
					Type tmp = base[j];
					base[j] = base[j + 1];
					base[j + 1] = tmp;
				}
			}
		}
		
	}
	void reserve()//左右对称位置交换
	{
		int left = 0;
		int right = size - 1;
		while (left < right)
		{
			Type tmp = base[left];
			base[left] = base[right];
			base[right] = tmp;
			left++;
			right--;
		}
	}
	Status insert_val(const Type &x)
	{
		if (IsFull())
		{
			cout << "空间已满" << x << "不能按值插入" << endl;
			return FALSE;
		}
		sort();
		for (int i = 0; i < size; ++i)
		{
			if (base[i] > x)//存在比所要插入元素大的元素,在该位置插入它
			{
				insert_pos(i, x);
				return TRUE;
			}
		}
		base[size++] = x;//不存在比所要插入元素大的元素,在最后位置插入它
		return TRUE;
	}
	/*
	Status insert_val(const Type &x)//用while代替for
	{
		if (IsFull())
		{
			cout << "空间已满" << x << "不能按值插入" << endl;
			return FALSE;
		}
		sort();
		int i = 0;
		while(i base[i])
		{
			i++;
		}
		insert_pos(i,x);
		return TRUE;
	}
	*/
	Status delete_val(const Type &x)
	{
		int n = find(x);
		if (n == -1)
		{
			cout <
main.cpp

#include"SeqList.h"

int main()
{
	SeqList mylist;
	int item;
	int n;
	int select = 1;
	while (select)
	{
		cout << "*************************************** *" << endl;
		cout << "*[1] push_back           [2] push_front *" << endl;
		cout << "*[3] show_list           [4] pop_back   *" << endl;
		cout << "*[5] pop_front           [6] insert_val *" << endl;
		cout << "*[7] insert_pos          [8] find       *" << endl;
		cout << "*[9] delete_pos          [10] delete_val*" << endl;
		cout << "*[11] sort               [12] reserve   *" << endl;
		cout << "*[13] destory            [14] clear     *" << endl;
		cout << "*[0] quit_system         [0] quit_system*" << 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(item);
			break;
		case 5:
			mylist.pop_front(item);
			break;
		case 6:
			cout << "请输入要插入的元素:";
			cin >> item;
			mylist.insert_val(item);
			break;
		case 7:
			cout << "请输入要插入的位置:";
			cin >> n;
			cout << "请输入要插入的元素:";
			cin >> item;
			mylist.insert_pos(n,item);
			break;
		case 8:
			cout << "请输入要查找的元素:";
			cin >> item;
			cout << mylist.find(item) << endl;
			break;
		case 9:
			cout << "请输入要删除的位置:";
			cin >> n;
			mylist.delete_pos(n,item);
			break;
		case 10:
			cout << "请输入要删除的元素:";
			cin >> item;
			mylist.delete_val(item);
			break;
		case 11:
			mylist.sort();
			break;
		case 12:
			mylist.reserve();
			break;
		case 14:
			mylist.clear();
			break;
		default:
			break;
		}
	}
	system("pause"); 
	return 0;
}
【c++版数据结构】之顺序表的实现_第1张图片



你可能感兴趣的:(c++版数据结构)