/*顺序表*/
#include <iostream>
#define Elme_Max 10
using namespace std;
typedef struct {
int *elem;
int length;
}Sqlist;
bool Init_Sqlist(Sqlist &l) //初始化
{
l.elem = new int[Elme_Max];
if (!l.elem) return false;
l.length = 0;
return true;
}
bool IsFull(Sqlist &l) //判断是否存满
{
return l.length == Elme_Max;
}
void Resieve(Sqlist &l) //2倍扩容
{
//l.elem = (int *)realloc(l.elem, Elme_Max * 2 * sizeof(int));
int *p = new int[Elme_Max * 2];
for (int i = 0; i < l.length; i++)
{
p[i] = l.elem[i];
}
delete[] l.elem;
l.elem = p;
}
bool Insert(Sqlist &l, int val, int pos) //在pos位置插入
{
if (IsFull(l))
{
Resieve(l);
}
for (int i = l.length; i >= pos; --i)
{
l.elem[i] = l.elem[i - 1];
}
l.elem[pos - 1] = val;
l.length++;
return true;
}
int Search_value(Sqlist &l, int val) //查找val值返回下标
{
for (int i = 0; i < l.length; i++)
{
if (l.elem[i] == val)
{
return i;
}
}
return -1;
}
bool Delete_value(Sqlist &l, int pos) //删除下标pos位置的值
{
if (pos > l.length) return false;
for (int i = pos; i < l.length; i++)
{
l.elem[i] = l.elem[i + 1];
}
l.length--;
return true;
}
int Length_Sqlist(Sqlist &l)
{
return l.length;
}
int Locat_Val(Sqlist &l, int i)
{
return l.elem[i-1];
}
void Print(Sqlist &l)
{
for (int i = 0; i < l.length; i++)
{
cout << l.elem[i] << " ";
}
cout << endl;
}
int main()
{
Sqlist sq;
Init_Sqlist(sq);
Insert(sq, 3, 1);
Insert(sq, 6, 2);
Insert(sq, 9, 3);
Insert(sq, 8, 4);
Insert(sq, 3, 5);
Insert(sq, 6, 6);
Insert(sq, 9, 7);
Insert(sq, 8, 8);
Insert(sq, 3, 9);
Insert(sq, 6, 10);
Insert(sq, 9, 11);
Insert(sq, 8, 12);
Print(sq);
cout << Length_Sqlist(sq) << endl;
return 0;
}