1.顺序表的实现,静态分配
#include<stdio.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int length;
}SqList;
//基本操作-初始化一个顺序表
void InitList(SqList &L)
{
int i;
for(i =0; i<MaxSize; i++)
{
L.data[i] = 0;
}
L.length = 0;
}
//打印
void ListPrint(SqList L)
{
int i;
for(i =0; i<MaxSize; i++)
{
printf("%-5d",L.data[i]);
}
}
int main()
{
void InitList(SqList &L);
void ListPrint(SqList L);
SqList L;
InitList(L);
ListPrint(L);
return 0;
}
2.顺序表的实现-动态分配
#include<stdio.h>
#include<stdlib.h>
#define InitSize 10//默认最大长度
typedef struct
{
int *data;//指示动态分配数组的指针
int length;//顺序表的最大容量
int MaxSize;//顺序表的当前长度
}SqList;
//基本操作-初始化一个顺序表
void InitList(SqList &L)
{
L.data = (int *)malloc(InitSize*sizeof(int));
L.length = 0;
L.MaxSize = InitSize;
}
//增加动态数组的长度
void IncreaseSize(SqList &L, int len)
{
int *p = L.data;
L.data = (int *)malloc((L.MaxSize+len)*sizeof(int));
for(int i=0;i<L.length;i++)
{
L.data[i] = p[i];
}
L.MaxSize = L.MaxSize + len;
free(p);
}
int main()
{
SqList L;
InitList(L);
IncreaseSize(L,5);
return 0;
}
3.插入操作
#include<stdio.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int length;
} SqList;
void InitList(SqList &L)
{
int i;
for(i =0; i<MaxSize; i++)
{
L.data[i] = 0;
}
L.length = 0;
}
bool ListInsert(SqList &L, int i, int e)
{
if(i<1 || i>L.length+1)
{
return false;
}
if(L.length == MaxSize)
{
return false;
}
for(int j=L.length;j>=i;j--)
{
L.data[j] = L.data[j-1];
}
L.data[i-1] = e;
L.length++;
return true;
}
void ListPrint(SqList L)
{
int i;
for(i =0; i<MaxSize; i++)
{
printf("%-5d",L.data[i]);
}
}
int main()
{
SqList L;
InitList(L);
ListInsert(L,1,1);
ListInsert(L,2,2);
ListInsert(L,3,3);
ListInsert(L,4,4);
ListInsert(L,2,3);
ListPrint(L);
}
插入:
- 最好情况:插入到表位,0次循环。复杂度=O(1)
- 最坏情况:插入到表头,n次循环。复杂度=O(n)
- 平均情况:每个位置的概率为:p=1/(n+1),平均循环次数:(0+…+n)*p=n/2。复杂度=O(n)
4.删除操作
#include<stdio.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int length;
} SqList;
void InitList(SqList &L)
{
int i;
for(i =0; i<MaxSize; i++)
{
L.data[i] = 0;
}
L.length = 0;
}
bool ListInsert(SqList &L, int i, int e)
{
if(i<1 || i>L.length+1)
{
return false;
}
if(L.length == MaxSize)
{
return false;
}
for(int j=L.length;j>=i;j--)
{
L.data[j] = L.data[j-1];
}
L.data[i-1] = e;
L.length++;
return true;
}
bool ListDelete(SqList &L, int i, int &e)
{
if(i<1 || i>L.length)
{
return false;
}
e = L.data[i-1];
for(int j=i;j<=L.length;j++)
{
L.data[j-1] = L.data[j];
}
L.data[L.length-1] = 0;
L.length--;
return true;
}
void ListPrint(SqList L)
{
int i;
for(i =0; i<MaxSize; i++)
{
printf("%-5d",L.data[i]);
}
}
int main()
{
SqList L;
int r1;
InitList(L);
ListInsert(L,1,1);
ListInsert(L,2,2);
ListInsert(L,3,3);
ListInsert(L,4,4);
ListInsert(L,2,3);
ListPrint(L);
printf("\n");
if(ListDelete(L,2,r1))
{
ListPrint(L);
printf("\n");
printf("%d",r1);
}
}
复杂度分析:
- 最好情况:删除表尾元素,0次循环。复杂度:O(1)
- 最坏情况:删除表头元素,n-1次循环。复杂度:O(n)
- 平均情况:概率:1/n。次数:(0+…+n-1)*p=n(n-1)/2。平均次数:(n-1)/2。复杂度:O(n)
5.按值查找
#include<stdio.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int length;
} SqList;
void InitList(SqList &L)
{
int i;
for(i =0; i<MaxSize; i++)
{
L.data[i] = 0;
}
L.length = 0;
}
bool ListInsert(SqList &L, int i, int e)
{
if(i<1 || i>L.length+1)
{
return false;
}
if(L.length == MaxSize)
{
return false;
}
for(int j=L.length;j>=i;j--)
{
L.data[j] = L.data[j-1];
}
L.data[i-1] = e;
L.length++;
return true;
}
bool ListDelete(SqList &L, int i, int &e)
{
if(i<1 || i>L.length)
{
return false;
}
e = L.data[i-1];
for(int j=i;j<=L.length;j++)
{
L.data[j-1] = L.data[j];
}
L.data[L.length-1] = 0;
L.length--;
return true;
}
int LocateElem(SqList L,int e)
{
for(int i=0; i<L.length;i++)
{
if(L.data[i] == e)
{
return i+1;
}
}
return 0;
}
void ListPrint(SqList L)
{
int i;
for(i =0; i<MaxSize; i++)
{
printf("%-5d",L.data[i]);
}
}
int main()
{
SqList L;
int r1,r2,r3;
InitList(L);
ListInsert(L,1,1);
ListInsert(L,2,2);
ListInsert(L,3,3);
ListInsert(L,4,4);
ListInsert(L,2,3);
ListPrint(L);
printf("\n");
if(ListDelete(L,2,r1))
{
ListPrint(L);
printf("\n");
printf("%d\n",r1);
}
else
{
printf("Error ListDelete!\n");
}
if((r2 = LocateElem(L,2))>0)
{
printf("%d\n",r2);
}
else
{
printf("Error LocateElem!\n");
}
return 0;
}
- 最好时间复杂度:O(1)
- 最坏时间复杂度:O(n)
- 平均比较次数:(n+1)/2
- 平均时间复杂度:O(n)
6.按位查找
#include<stdio.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int length;
} SqList;
void InitList(SqList &L)
{
int i;
for(i =0; i<MaxSize; i++)
{
L.data[i] = 0;
}
L.length = 0;
}
bool ListInsert(SqList &L, int i, int e)
{
if(i<1 || i>L.length+1)
{
return false;
}
if(L.length == MaxSize)
{
return false;
}
for(int j=L.length;j>=i;j--)
{
L.data[j] = L.data[j-1];
}
L.data[i-1] = e;
L.length++;
return true;
}
bool ListDelete(SqList &L, int i, int &e)
{
if(i<1 || i>L.length)
{
return false;
}
e = L.data[i-1];
for(int j=i;j<=L.length;j++)
{
L.data[j-1] = L.data[j];
}
L.data[L.length-1] = 0;
L.length--;
return true;
}
int LocateElem(SqList L,int e)
{
for(int i=0; i<L.length;i++)
{
if(L.data[i] == e)
{
return i+1;
}
}
return 0;
}
int GetElem(SqList L,int i)
{
if(i>0 && i<=L.length)
{
return L.data[i-1];
}
return 0;
}
void ListPrint(SqList L)
{
int i;
for(i =0; i<MaxSize; i++)
{
printf("%-5d",L.data[i]);
}
}
int main()
{
SqList L;
int r1,r2,r3;
InitList(L);
ListInsert(L,1,1);
ListInsert(L,2,2);
ListInsert(L,3,3);
ListInsert(L,4,4);
ListInsert(L,2,3);
ListPrint(L);
printf("\n");
if(ListDelete(L,2,r1))
{
ListPrint(L);
printf("\n");
printf("%d\n",r1);
}
else
{
printf("Error ListDelete!\n");
}
if((r2 = LocateElem(L,2))>0)
{
printf("%d\n",r2);
}
else
{
printf("Error LocateElem!\n");
}
if((r3 = GetElem(L,1))>0)
{
printf("%d\n",r3);
}
else
{
printf("Error GetElem!\n");
}
return 0;
}