c语言实现线性表
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 10
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;
// 元素结点
typedef struct Node
{
ElemType* data;
int length;
}Node,SqList;
// 初始化线性表
Status InitList(SqList* s)
{
s->data = (ElemType*)malloc(sizeof(Node)*MAXSIZE);
if (s != NULL) {
s->length = 0;
return OK;
}
else
return ERROR;
}
// 像线性表中下标为i的地方插入元素e
Status ListInsert(SqList* s,int i, ElemType e)
{
int k;
// 首先判断位置的合法性
Status flag = (0 <= i && i <= s->length);
flag = (flag && (s->length + 1) <= MAXSIZE);
if (flag) {
// i位置之后的依次后移
for (k = s->length - 1; k > i; k--)
s->data[k + 1] = s->data[k];
// 赋值
s->data[i] = e;
s->length++;
return OK;
} else {
return ERROR;
}
}
// 在线性表的尾部插入元素,这里直接调用上方的函数就行了
Status ListInsertInTail(SqList* s, ElemType e)
{
return ListInsert(s, s->length, e);
}
// 删除下标为i的元素
Status ListDelete(SqList* s, int i)
{
int k;
// 判断位置合法性
Status flag = (0 <= i && i < s->length);
if (flag) {
// i位置后的元素依次前移
for (k = i; i < s->length - 1; k++)
s->data[k] = s->data[k + 1];
s->length--;
return OK;
} else {
return ERROR;
}
}
// 在线性表中查找元素e的下标,如果找到则返回下标否则返回-1
ElemType find(SqList* s, ElemType e) {
int ret = -1;
int i;
for (i = 0; i < s->length; i++) {
if (e == s->data[i]) {
ret = i;
break;
}
}
return ret;
}
// 获取下标为i的元素值
ElemType getElem(SqList* s, int i) {
Status flag = (0 <= i && i < s->length);
if (flag) {
return s->data[i];
} else {
return -1;
}
}
// 设置下标为i的元素的值为e
void setElem(SqList* s, int i, ElemType e) {
Status flag = (0 <= i && i < s->length);
if (flag) {
s->data[i] = e;
}
}
// 返回线性表中元素的个数
int length(SqList* s) {
return s->length;
}
int main()
{
SqList L;
int i;
InitList(&L);
for (i = 0; i < 9; i++)
ListInsert(&L, i, i);
printf("此时线性表中元素个数为: %d\n", length(&L));
setElem(&L, 8, 50);
for (i = 0; i < 9; i++) {
printf ("从头到尾一次打印线性表中的元素: %d\n", getElem(&L, i));
}
ListDelete(&L, 8);
printf("删除操作完成之后元素个数为: %d\n", length(&L));
printf("在线性表中查找元素5的下标: %d\n", find(&L, 5));
return 0;
}
c++实现线性表
#include <iostream>
using namespace std;
template <typename T, int N>
class SqList
{
private:
T m_array[N];
int m_length;
public:
SqList() {
m_length = 0;
}
// 向顺序表位置i处插入元素e
bool insert(int i, const T& e) {
// 首先判断位置合法性
bool ret = (0 <= i && i <= m_length);
// 保证加入以为元素之后依然合法
ret = (ret && (m_length + 1) <= N);
if (ret) {
// 位置i及以后的元素都往后移让出位置i
for (int p = m_length-1; p > i; p--)
m_array[p + 1] = m_array[p];
// 插入元素
m_array[i] = e;
m_length++;
return true;
} else {
return false;
}
}
// 直接在尾部插入
bool insert(const T& e) {
return insert(m_length, e);
}
// 删除线性表中位置为i的元素
bool remove(int i) {
bool ret = (0 <= i && i < m_length);
if (ret) {
// 元素前移
for (int p = i; p < m_length - 1; p++)
m_array[p] = m_array[p + 1];
m_length--;
return true;
} else {
return false;
}
}
// 更改位置i的元素值为e
void set(int i, const T& e) {
if (!empty()) {
m_array[i] = e;
}
}
// 获取位置i的元素值
bool get(int i, T& e) {
if (!empty()) {
e = m_array[i];
return true;
} else {
return false;
}
}
// 重载get函数便于测试时打印数据
T get(int i) {
T ret;
if (get(i, ret)) {
return ret;
} else {
return -1;
}
}
// 查找线性表中是否有元素值为e的元素,如果有则返回下标,否则返回-1
int find(const T& e) {
int ret = -1;
for (int i = 0; i < m_length; i++) {
if (e == m_array[i]) {
ret = i;
break;
}
}
return ret;
}
bool empty() const {
return m_length == 0;
}
int length() const {
return m_length;
}
int capacity() const {
return N;
}
};
int main()
{
SqList<int, 10> s;
for (int i = 0; i < s.capacity(); i++)
s.insert(i, i);
cout << "此时顺序表中元素个数为: " << s.length() << endl;
for (int i = 0; i < s.capacity(); i++)
cout << "顺序表中的元素依次为: " <<s.get(i) << endl;
cout << "元素7的下标为: " << s.find(7) << endl;
s.remove(7);
for (int i = 0; i < s.length(); i++)
cout << "顺序表中的元素依次为: " <<s.get(i) << endl;
cout << "删除元素7之后查找元素7的下标为 " << s.find(7) << endl;
return 0;
}
参考:
大话数据结构 c语言版
数据结构、算法与应用 c++语言描述