#include<stdio.h>
#include<iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
}SqList, * PSqList;
void InputList(SqList& L)
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> L.data[i];
}
L.length = n;
}
void OutputList(SqList L)
{
for (int i = 0; i < L.length; i++)
{
cout << L.data[i] << " ";
}
}
bool InsertList(SqList& L,int position,ElemType a)
{//position是位序
//if (position<1 || position>L.length)
//{
// return false;
//}
//if (L.length >= MaxSize)
//{
// return false;
//}
for (int j = L.length; j>=position; j--)
{
L.data[j] = L.data[j-1];
}
L.data[position - 1] = a;
L.length++;
return true;
}
int LocateElem(SqList L,ElemType a)
{
for (int i = 0; i < L.length; i++)
{
if (L.data[i] == a)
return i+1;//下标
}
return 0;
}
int main()
{
SqList A;
InputList(A);
int item;
cin >> item;
int ret= LocateElem(A, item);
int data;
cin >> data;
if (ret)
{
InsertList(A, ret, data);
}
else
{
A.data[A.length] = data;
A.length++;
}
OutputList(A);
}
bool ListDelete(SqList& L,int loc)
{
if (loc<1 || loc>L.length)
return false;
for (int i = loc; i < L.length; i++)
L.data[i - 1] = L.data[i];
L.length--;
return true;
}
943: 顺序表插入操作的实现
最新推荐文章于 2024-05-20 15:29:54 发布