线性表是具有相同数据类型的n个数据元素的有限序列。其中n为表长,当n = 0时该线性表是一个空表。
线性表的特点如下:
1.表中元素具有逻辑上的顺序性。
2.表中元素都是数据元素。
3.表中元素的数据类型都相同。
4.表中元素具有抽象性。
下面利用C++来实现数据结构中线性表的一些基本操作:
1.求表长
2.按值查找操作
3.按位查找操作
4.插入操作
5.删除操作
6.输出操作
7.判空操作
#include<iostream>
#include<algorithm>
#include<cmath>
#include<windows.h>
#define SIZE 1028
using namespace std;
class MyList
{
public:
MyList();
MyList(int mysize);
~MyList();
int ListLength(); /*求表长*/
int ListLocateElem(int e); /*按值查找*/
int ListGetElem(int i); /*按位查找*/
void ListInsert(int i, int e); /*在第i个位置插入元素e*/
int ListDelete(int i); /*删除表中第i位置的元素,并返回值e*/
void PrintList(); /*输出操作*/
bool ListEmpty(); /*判空操作*/
private:
int size;
int *my_buff;
int count;
};
MyList::MyList()
{
size = SIZE;
my_buff = new int[SIZE];
count = 0;
}
MyList::MyList(int mysize)
{
size = mysize;
my_buff = new int[mysize];
count = 0;
}
MyList::~MyList()
{
delete[]my_buff;
}
int MyList::ListLength()
{
return count;
}
int MyList::ListLocateElem(int e)
{
if (!ListEmpty())
{
for (int i = 0; i < count; i++)
{
if (my_buff[i] == e)
{
return i;
break;
}
}
}
else
cout << "List is empty!" << endl;
return -1;
}
int MyList::ListGetElem(int i)
{
if (i >= count || ListEmpty())
cout << "find fail!" << endl;
else
return my_buff[i];
return -1;
}
void MyList::ListInsert(int i, int e)
{
if (i > size || i > count)
cout << "out of size!" << endl;
else
{
for (int j = count; j >= i; j--)
{
my_buff[j] = my_buff[j - 1];
}
my_buff[i] = e;
cout << "insert success!" << endl;
count++;
}
}
int MyList::ListDelete(int i)
{
int temp;
if (i > count)
cout << "delete failed!" << endl;
else
{
temp = my_buff[i];
for (int j = i; j < count - 1; j++)
{
my_buff[j] = my_buff[j + 1];
}
count--;
return temp;
}
return -1;
}
void MyList::PrintList()
{
if (!ListEmpty())
{
for (int i = 0; i < count; i++)
{
cout << "my_buff[" << i << "] = " << my_buff[i] << endl;
}
}
else
cout << "List is empty!" << endl;
}
bool MyList::ListEmpty()
{
if (count == 0)
return 1;
return 0;
}
int main()
{
int cmd;
int temp, i, ans;
int end = 0;
cout << "输入需要创建线性表的长度: ";
cin >> temp;
/* if (temp == 0)
MyList list;
else*/
MyList list(temp);
while (1)
{
system("cls");
cout << endl << endl << endl << endl;
cout << "**************************************" << endl;
cout << "1.按值查找操作(输入关键字元素返回位置)" << endl;
cout << "2.按位查找操作(输入位置i返回元素值)" << endl;
cout << "3.插入操作(输入插入位置和插入元素)" << endl;
cout << "4.删除操作(输入删除位置返回删除值)" << endl;
cout << "5.返回表长值操作" << endl;
cout << "6.判空操作" << endl;
cout << "7.输出操作" << endl;
cout << "0.退出" << endl;
cout << "**************************************" << endl;
cout << endl << endl << endl << endl;
cout << "输入你需要进行的操作:";
cin >> cmd;
switch (cmd)
{
case 1:
{
cout << "输入关键字元素: " << endl;
cin >> temp;
ans = list.ListLocateElem(temp);
if (ans != -1)
cout << "关键字元素返回位置 :" << ans << endl;
else
cout << "find failed!" << endl;
break;
}
case 2:
{
cout << "输入位置 :";
cin >> temp;
ans = list.ListGetElem(temp);
if (ans != -1)
cout << "位置返回关键字: " <<ans << endl;
else
cout << "find failed!" << endl;
break;
}
case 3:
{
cout << "输入插入位置和插入元素: ";
cin >> i >> temp;
list.ListInsert(i, temp);
break;
}
case 4:
{
cout << "输入删除位置: ";
cin >> temp;
ans = list.ListDelete(temp);
if (ans != -1)
cout << "删除元素的值: " << ans << endl;
else
cout << "delete failed!" << endl;
break;
}
case 5:
{
cout <<"线性表长: "<< list.ListLength() << endl;
break;
}
case 6:
{
if (list.ListEmpty() == 1)
cout << "list is empty!" << endl;
else
cout << "list is not empty!" << endl;
break;
}
case 7:
{
list.PrintList();
break;
}
case 0:
{
end = 1;
break;
}
}
if (end == 1)
break;
Sleep(1000);
}
system("pause");
return 0;
}
作为自己熟悉线性表写的代码所以只是注重于实现这些操作,没有去实现数据的多样性,而是在程序中固定的设置为int类型。