#define _CRT_SECURE_NO_DEPRECATE
//#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include<iostream>
#include<string.h>
using namespace std;
#define listInitSize 5
#define listIncrement 1
typedef struct
{
int *elem;//此表的首地址
int length;//此表目前的长度
int listSize;//此表的总长度
}Sqlist;
//1.初始化此表
bool initListSq(Sqlist &L)
{
L.elem = new int[listInitSize];
if (!L.elem)
{
cout << "内存分配失败!\n";
exit(0);
}
L.length = 0;
L.listSize = listInitSize;
return true;
}
//2.插入数据
bool listInsertSq(Sqlist &L, int i, int &e)
{
if (i <= 0 || i >= L.length + 2) exit(0);
if (L.length >= L.listSize)
{
int *newBase = new int[L.listSize + listIncrement];
if (!newBase) exit(0);
memcpy(newBase, L.elem, sizeof(int)*L.listSize);
int *temp = L.elem;//
delete[]temp;
L.elem = newBase;
L.listSize += listIncrement;
}
for (int j = L.length - 1; j >= i - 1; j--)
L.elem[j + 1] = L.elem[j];
L.elem[i - 1] = e;
L.length++;
return true;
}
//3.删除数据
bool listDelete(Sqlist &L, int i, int &e)
{
if (i <= 0 || i >= L.length + 1) exit(0);
e = L.elem[i - 1];
for (int j = i - 1; j < L.length; j++)
L.elem[j] = L.elem[j + 1];
L.length--;
return true;
}
//4.
int locateElemSq(Sqlist &L, int e, bool(*compare)(int, int))//不理解请看代码后部分的“函数与指针应用”
{
int i = 1;
int *p = L.elem;
while (i <= L.length&&!(*compare)(*p++, e)) i++;
if (i <= L.length) return i;
return 0;
}
//5.合并两个已是递增的顺序表,按递增
bool mergeListSq(Sqlist &L1, Sqlist &L2, Sqlist &L)
{
L.listSize = L.length = L1.length + L2.length;
int *p = L.elem = new int[L.listSize];
if (!L.elem) exit(0);
int *p1 = L1.elem, *p2 = L2.elem;
int *p1Last = L1.elem + L1.length - 1, *p2Last = L2.elem + L2.length - 1;
while (p1 <= p1Last&&p2 <= p2Last)
{
if (*p1 <= *p2) *p++ = *p1++;
else *p++ = *p2++;
}
while (p1 <= p1Last) *p++ = *p1++;
while (p2 <= p2Last) *p++ = *p2++;
return true;
}
int main()
{
return 0;
}
/*函数与指针应用
#include<iostream>
using namespace std;
bool com(int i,int j,bool (*p)(int a,int b))//p是一个函数的指针
{
if((*p)(i,j))
{cout<<"yes";return true;}
return false;
}
bool com1(int i,int j)
{
if(i<j)
return true;
return false;
}
int main()
{
int aa=1,bb=3;
com(aa,bb,com1);
return 0;
}
OutPut:yes
*/
线性表--顺序表
最新推荐文章于 2022-10-06 16:58:10 发布