/*
* Copyright (c) 2012 Fang Ying (Y. Fang), NEU Electric Engineering 20092725 ,fangying712@gmail.com
* Copyright (c) GPLv3
* All Rights Reserved.
* This program is free software;
*you can redistribute it and/or modify it under the terms of the GNU General Public License V3 as published by the
* Free Software Foundation, either version 2 or any later version.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details. A copy of the GNU General Public License is available at:
* http://www.fsf.org/licensing/licenses
*/
#ifndef _SQ_LIST_H_
#define _SQ_LIST_H_
#include <iostream>
using namespace std;
//定义一个类模版
template <class T>
class sq_list
{
private :
int max; // 存储空间长度
int n; // 顺序链表长度v
T *v; // 顺序链表存储空间首地址
public :
sq_list(){max = 0; n = 0; return ;}
sq_list(int); // 建立空链表,申请存储空间
~sq_list(); // 析构函数
void print(); // 打印顺序链表的元素和长度
int flag(); // 检测链表状态
void insert(int , T); // 在表中的指定位置插入新的元素
void remove(int ); // 在表中的指定位置删除一个元素
int find(T ); // 在表中查找一个指定的数据
int size(); // 返回线性表的长度
T get(int); // 获得某个元素
void sort(); // 对顺序表进行排序,使用冒泡法
};
//建立一个空的顺序链表
template <class T>
sq_list<T>::sq_list(int max)
{
this->max = max; // 存储空间容量
v = new T[max]; // 动态申请存储空间
n = 0; // 初始长度为0
return ;
}
//顺序输链表中的元素和顺序表的长度
template <class T>
void sq_list<T>::print()
{
int i;
cout<<"n = "<<n<<endl;
for (i = 0; i < n; i++)
cout<<"v"<<"["<<i<<"]"<<"\t:"<<v[i]<<endl; // 输出表中的元素
return ;
}
//检测顺序链表的状态
template <class T>
int sq_list<T>::flag()
{
if (n == max) return -1; // 存储空间已满,返回-1
if (n == 0) return 0; // 链表为空,返回0
return 1; // 正常,返回1
}
//在链表指定位置插入新的元素
template <class T>
void sq_list<T>::insert(int i, T b)
{
int k;
if (n == max)
{cout<<"sq_list overflow !"<<endl; return;}
if (i > n) i = n + 1; // 默认在最后一个元素之后插入
if (i < 1) i = 1; // 默认在第一个元素之后插入
for (k = n; k >= i; k--)
v[k] = v[k-1]; // 从最后一个元素直到第i个元素后移一位
v[i-1] = b; // 插入新的元素
n += 1; // 链表长度加1
return ;
}
//在顺序链表指定位置删除元素
template <class T>
void sq_list<T>::remove(int i)
{
int k;
if (n == 0)
{cout<<"sq_list underflow"; return ;}
if ((i < 0)||(i > n))
{
cout<<"No this element in the list!"<<endl; // 要删除的元素不存在
return ;
}
for (k = i; k < n ; k++)
v[k-1] = v[k]; // 从第i个元素直到最后一个元素均向前移动一个位置
n--;
return ;
}
//顺序表中的元素进行排序,使用冒泡法,小泡泡在下面大泡泡在上面
template <class T>
void sq_list<T>::sort()
{
int i,j;
T temp;
if(0 == n)
{cout<<"sq_list is empty !"<<endl; return;} // 顺序表为空的情况
for(i = 0; i < n-1; i++ )
{
for(j = 0; j < n-i; j++)
{
//冒泡排序
if(v[j] < v[j+1])
{
temp = v[j];
v[j] = v[j+1];
v[j+1] = temp;
}
}
}
return ;
}
//查找一个数
template <class T>
int sq_list<T>::find(T t)
{
int i = 0;
for(i; i < n; i++)
{
if(t == v[i]) return i;
}
return -1;
}
//返回线性表的长度
template <class T>
int sq_list<T>::size()
{
return n;
}
//返回线性表的元素
template <class T>
T sq_list<T>::get(int i)
{
return v[i];
}
//线性表空间回收
template <class T>
sq_list<T>::~sq_list()
{
delete v;
}
#endif
#include "sq_list.h"
int main()
{
sq_list<double> s_double(10); //建立一个容量为100的顺序链表
cout<<"第一个输出顺序表对象 s_double:"<<endl;
s_double.print();
s_double.insert(0, 1.5);
s_double.insert(1, 2.3);
s_double.insert(2, 1.0);
s_double.insert(3, 1.2);
s_double.insert(4, 3.5);
cout<<"第二次输出的顺序表对象:"<<endl;
s_double.print();
cout<<"对顺序表进行排序:"<<endl;
s_double.sort();
cout<<"第3次输出的顺序表对象:"<<endl;
s_double.print();
s_double.sort();
cout<<"删除下列下列元素:"<<endl;
s_double.remove(0);
s_double.remove(1);
cout<<"第三次输出的顺序表:"<<endl;
s_double.print();
return 0;
}