单链表
图例说明:
LinkList是头文件,可以方便以后使用这个数据结构。。。。。。
#pragma once
template<class T>
class SqList
{
private:
T*elem; //表首地址
int length; //表长
int listsize; //表容
public:
SqList(int m); //构造函数,穿件容量为m的空表
~SqList(); //析构函数
void CreateList(int n); //创建n个元素的线性表
void Insert(int i, T e); //第i个位置插入元素
T Delate(int i); //删除第i个元素
T GetElem(int i); //获取第i个元素
int Locate(T e); //元素定位
void Clear(); //清空
int Empty(); //测表空
int Full(); //测表满
int Length(); //测表长
void ListDisp(); //输出表元素
};
template<class T>
SqList<T>::SqList(int m) //创建表容为m的空表
{
elem = new T[m]; //申请表空间
length = 0; //表长为0
listsize = m; //表容为m
}
template<class T>
SqList<T>::~SqList() //析构函数
{ //释放表空间
delete[] elem;
length = 0;
listsize = 0;
}
template<class T>
void SqList<T>::CreateList(int n)
{ //创建长度为n的顺序表
if (n>listsize)throw"参数非法";
cout << "请依次输入" << "n个元素值:" << endl;
for (int i = 1; i <= n; i++)
{
cin >> elem[i - i];
}
length = n;
}
template<class T>
void SqList<T>::Insert(int i, T e)//第i个位置插入元素
{
if (length == 0)throw"上溢";
if (i<1 || i>length + 1)throw"插入位置异常";
for (int j = length; j >= i; j--)
{
elem[j] = elem[j - 1];
}
elem[i - 1] = e;
length = length + 1;
}
template<class T>
T SqList<T>::Delate(int i) //第i个位置删除元素
{
T x;
if (length == 0)throw"下溢";
if (i<1 || i>length + 1)throw"删除位置异常";
x = elem[i - 1];
for (int j = i; j <= length; j++)
elem[j - 1] = elem[j];
length--;
return x;
}
template<class T>
int SqList<T>::Locate(T e)
{ //定位查找元素,若找到,返回该元素在表中的位置;未找到,返回0
for (int i = 0; i<length; i++)
if (elem[i] == e)
return i + 1;
return 0;
}
template<class T>
void SqList<T>::Clear() //清空表
{
length = 0;
}
template<class T>
T SqList<T>::GetElem(int i) //获取地i个元素的值
{
T e;
if (i<1 || i>length) throw"位置不合法";
e = elem[i - 1];
return e;
}
template<class T> //判断表是否为空
int SqList<T>::Empty()
{
if (length == 0)
return 1;
else
return 0;
}
template<class T>
int SqList<T>::Full() //判断表是否满
{
if (length == listsize)
return 1;
else
return 0;
}
template<class T>
int SqList<T>::Length()
{
return length;
}
template<class T>
void SqList<T>::ListDisp() //遍历输出表的内容
{
for (int i = 1; i<length; i++)
{
cout << i + 1 << "\t";
cout << elem[i] << endl;
}
}
此部分LinkList.cpp为测试代码,用来验证测试
// LinkList.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream> //cout,cin
#include <process.h> //exit()
#include "SqList.h"
using namespace std;
char pause;
typedef int T;
int main()
{
int i;
T e;
SqList<int>L(20);
system("cls");
int choice;
do
{
cout << "1-创建顺序表\n";
cout << "2-在链表地i个位置插入元素\n";
cout << "3-删除顺序边的i个位置元素\n";
cout << "4-返回单位i个元素的值\n";
cout << "5-定位元素\n";
cout << "6-清空表\n";
cout << "7-测表空\n";
cout << "8-测表满\n";
cout << "9-测表长\n";
cout << "10-显示表\n";
cout << "11-退出\n";
cout << "Enter chioce:";
cin >> choice;
switch (choice)
{
case 1: //创建表
cout << "请输入要创建的顺序表忠元素个数:";
cin >> i;
cout << endl;
try
{
L.CreateList(i);
}
catch (char*err)
{
cout << err << endl;
}
break;
case 2: //元素插入
cout << "请输入插入位置:";
cin >> i;
cout << endl;
cout << "请输入插入元素的值:";
cin >> e;
cout << endl;
try
{
L.Insert(i, e);
}
catch (char*err)
{
cout << err << endl;
}
break;
case 3: //元素删除
cout << "请输入删除位置:";
cin >> i;
cout << endl;
try
{
e = L.Delate(i);
cout << "被删除元素为:" << e << endl;
}
catch (char*err)
{
cout << err << endl;
}
cin.get(pause);
system("pause");
break;
case 4: //返回第i个元素值
cout << "请输入要查询元素的位置:";
cin >> i;
try
{
e = L.GetElem(i);
cout << "第" << i << "个元素值为:" << e << endl;
}
catch (char*err)
{
cout << err << endl;
}
cin.get(pause);
system("pause");
break;
case 5:
cout << "请输入要查询元素:";
cin >> e;
i = L.Locate(e);
cout << "元素查询" << e << "位于表中的位置为:" << i << endl;
cin.get(pause);
system("pause");
break;
case 6:
L.Clear();
cout << "表已清空" << endl;
cin.get(pause);
system("pause");
break;
case 7:
i = L.Empty();
if (i)
cout << "表空" << endl;
else
cout << "表不空" << endl;
cin.get(pause);
system("pause");
break;
case 8:
i = L.Full();
if (i)
cout << "表满" << endl;
else
cout << "表未满" << endl;
cin.get(pause);
system("pause");
break;
case 9:
i = L.Length();
cout << "表长为:" << i << endl;
cin.get(pause);
system("pause");
break;
case 10:
L.ListDisp();
cout << endl;
cin.get(pause);
system("pause");
break;
case 11:
break;
default: //非法选择
cout << "Invalid choice\n";
break;
}
} while (choice != 11);
return 0;
}//end