//MyList.h
//注意:模板(template)类的实现和定义要放在一个文件中,否则会出现链接失败,STL也是这样实现的
#pragma once
#include "stdafx.h"
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
template<class T>
class CSqList
{
private:
T *m_elem;//表首地址
int m_length;//表长
int m_listsize;//表容量
public:
CSqList(int m);
~CSqList();
void CreateList(int n);//创建具有n个元素的线性表
void Insert(int i,T e);//在表中的第i个位置插入元素e
T Delete(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>
CSqList<T>::CSqList(int m)
{
m_elem=new T[m];
m_length=0;
m_listsize=m;
}
template<class T>
CSqList<T>::~CSqList()
{
delete [] m_elem;
m_length=0;
m_listsize=0;
}
template<class T>
void CSqList<T>::CreateList(int n)
{
if (n>m_listsize)
{
throw "参数非法!";
}
cout<<"请一次输入"<<n<<"个元素值:"<<endl;
for (int i=1;i<=n;i++)
{
cin>>m_elem[i-1];
}
m_length=n;
}
template<class T>
void CSqList<T>::Insert(int i,T e)
{
if (m_length>=m_listsize)
{
throw "上溢!";
}
if (i<1 || i>m_length+1)
{
throw "插入位置异常!";
}
for (int j=m_length;j>=i;j--)
{
m_elem[j]=m_elem[j-1];
}
m_elem[i-1]=e;
m_length++;
}
template<class T>
T CSqList<T>::Delete(int i)
{
T x;
if (m_length==0)
{
throw "下溢!";
}
if (i<1 || i>m_length+1)
{
throw "删除位置异常!";
}
x=m_elem[i-1];
for (int j=i;j<m_length;j++)
{
m_elem[j-1]=m_elem[j];
}
m_length--;
return x;
}
template<class T>
int CSqList<T>::Locate(T e)
{
for (int i=0;i<m_length;i++)
{
if (m_elem[i]==e)
{
return i+1;
}
}
return 0;
}
template<class T>
void CSqList<T>::Clear()
{
m_length=0;
}
template<class T>
T CSqList<T>::GetElem(int i)
{
T e;
if (i<1 || i>m_length)
{
throw "位置不合法!";
}
e=m_elem[i-1];
return e;
}
template<class T>
int CSqList<T>::Full()
{
if (m_length==m_listsize)
{
return 1;
}
return 0;
}
template<class T>
int CSqList<T>::Empty()
{
if (m_length==0)
{
return 1;
}
return 0;
}
template<class T>
int CSqList<T>::Length()
{
return m_length;
}
template<class T>
void CSqList<T>::ListDisp()
{
for (int i=0;i<m_length;i++)
{
cout<<i+1<<"\t";
cout<<m_elem[i]<<endl;
}
}
#endif
// SqList(线性表).cpp : 定义控制台应用程序的入口点。
//
#pragma once
#include "stdafx.h"
#include <process.h>
#include<iostream>
using namespace std;
#include "Mylist.h"
char pause;
int main()
{
int i;
int e;
CSqList<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--元素定位"<<endl;
cout<<"6--清空表"<<endl;
cout<<"7--判表空"<<endl;
cout<<"8--判表满"<<endl;
cout<<"9--判表长"<<endl;
cout<<"10--显示链表"<<endl;
cout<<"11--退出"<<endl;
cout<<"Enter Choice:";
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.Delete(i);
cout<<"被删除的元素为:"<<e<<endl;
}
catch(char *err)
{
cout<<err<<endl;
}
cin.get(pause);
system("pause");
break;
}
case 4:
{
cout<<"请输入要查询的元素的位置:";
cin>>i;
try
{
e=L.GetElem(i);
cout<<"第"<<i<<"个元素的值为"<<e<<endl;
}
catch(char *err)
{
cout<<err<<endl;
}
break;
}
case 5:
{
cout<<"请输入要查询的元素值:";
cin>>e;
cout<<endl;
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:
{
exit(0);
break;
}
default:
{
cout<<"Invaild choice!"<<endl;
break;
}
}
} while (choice!=11);
return 0;
}