这部分东西没什么难点 直接上代码叭
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
const int N = 10000;//空间
typedef struct student
{
int data;
}stu;
typedef struct
{
stu* elem;
int length;//当前表长
}Sqlist;
void InitList(Sqlist& L)//初始化顺序表
{
L.elem = new stu[N];
if (!L.elem)
{
cout<<"初始化失败"<<endl;
return;
}
L.length = 0;
return;
}
void Insertlist(int i, int k, Sqlist& L)//在第i处插入k
{
if (L.length == N)
{
cout << "顺序表已满 插入失败" << endl;
return;
}
if (i > L.length+1 || i < 1)
{
cout << "插入处不合法" << endl;
return;
}
for (int j = L.length; j > i - 1; j--)
{
L.elem[j] = L.elem[j - 1];
}
L.elem[i-1].data = k;
L.length++;
return;
}
void Traversal_List(Sqlist& L)//遍历表
{
for (int i = 0; i < L.length; i++)
{
cout << L.elem[i].data << "->";
}
cout << "NULL"<<endl<<"遍历结束" << endl;
}
void Get_Kth_num(int k, Sqlist& L)//找到第K个数
{
if (k<1 || k>L.length)
{
cout << "查找数据不合法"<<endl;
return;
}
cout <<"第"<<k<<"个元素为"<< L.elem[k - 1].data << endl;
}
void Find_Ones_first(int k, Sqlist& L)//找到K第一次出现的序号
{
for (int i = 0; i < L.length; i++)
{
if (L.elem[i].data == k)
{
cout << k << "第一次出现的序号为" << i + 1 << endl;
return;
}
}
cout << "未找到" << endl;
}
void Delete(int k, Sqlist& L)//删除第K个值
{
if (k<1 || k>L.length)
{
cout << "数据不合法 删除失败" << endl;
return;
}
cout<<"第"<<k<<"个元素已经删除 删除值为"<<L.elem[k-1].data<<endl;
for (int i = k-1; i < L.length-1; i++)
{
L.elem[i] = L.elem[i +1];
}
L.length--;
return;
}
int main()
{
Sqlist L;
InitList(L);
Insertlist(1, 8, L);
Insertlist(1, 6, L);
Insertlist(1, 3, L);
Insertlist(1, 6, L);
Insertlist(1, 7, L);
Traversal_List(L);
Get_Kth_num(3,L);
Find_Ones_first(3, L);
Delete(1, L);
Traversal_List(L);
}
效果如下