顺序表:
指在计算机内存中以数组形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。
也就是,将表中元素一个接一个的存入一组连续的存储单元中,这种存储结构是顺序结构。采用顺序存储结构的线性表简称顺序表
下来直接上代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#define MAX 10//数组元素个数
typedef int DataType;//数组元素类型重命名
typedef struct SeqList
{
DataType data[MAX];//创建一个数组
int sz;//记录元素个数
}SeqList,*pSeqList;
void InitSeqList(pSeqList ps)//初始化函数
{
ps->sz = 0;
memset(ps->data, 0, MAX*sizeof(DataType));
}
void PushBack(pSeqList ps, DataType x)//从尾部插入的函数
{
assert(ps != NULL);
if (ps->sz == MAX)
{
return;
}
ps->data[ps->sz] = x;
ps->sz++;
}
void PrintSeqList(const pSeqList ps)//打印数组的函数
{
int i = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
for (i = 0; i < ps->sz; i++)
{
printf("%d", ps->data[i]);
}
printf("\n");
}
void PopBack(pSeqList ps)//尾部删除
{
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
ps->sz--;
}
void PushFront(pSeqList ps, DataType x)//头插函数
{
assert(ps != NULL);
if (ps->sz == MAX)
{
return;
}
memmove(ps->data + 1, ps->data, (ps->sz)*sizeof(DataType));
ps->data[0] = x;
ps->sz++;
}
void PopFront(pSeqList ps)//头删函数
{
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
memmove(ps->data, ps->data + 1, (ps->sz - 1)*sizeof(DataType));
ps->sz--;
}
void Insert(pSeqList ps, int pos, DataType x)
{
assert(ps != NULL);
if (ps->sz == MAX)
{
return;
}
memmove(ps->data + pos + 1, ps->data + pos, (ps->sz - pos)*sizeof(DataType));
ps->data[pos] = x;
ps->sz++;
}
int Find(pSeqList ps, DataType x)//查找函数
{
int i = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
return -1;
}
for (i = 0; i < ps->sz; i++)
{
if (ps->data[i] == x)
{
return i;
}
}
return -1;
}
void Remove(pSeqList ps, DataType x)
{
int ret = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
ret = Find(ps, x);
if (ret != -1)
{
memmove(ps->data + ret, ps->data + ret + 1, (ps->sz - ret -1)*sizeof(DataType));
ps->sz--;
}
}
void RemoveAll(pSeqList ps, DataType x)//删除所有某个指定值的函数
{
int i = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
for (i = 0; i < ps->sz; i++)
{
if (x == ps->data[i])
{
memmove(ps->data + i, ps->data + i + 1, (ps->sz - i - 1)*sizeof(DataType));
ps->sz--;
i--;
}
}
}
void ReverseList(pSeqList ps)//逆序函数
{
int left = 0;
int right = ps->sz - 1;
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
while (left < right)
{
int tmp = ps->data[left];
ps->data[left] = ps->data[right];
ps->data[right] = tmp;
left++;
right--;
}
}
int BinarySearch(pSeqList ps, DataType x)//二分查找函数
{
int left = 0;
int right = ps->sz - 1;
int mid = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
return -1;
}
while (left < right)
{
mid = left + (right - left) / 2;
if (ps->data[mid]>x)
{
right = mid - 1;
}
else if (ps->data[mid] < x)
{
left = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
void SortList(pSeqList ps)//排序函数
{
int i = 0;
int j = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
return;
}
for (i = 0; i < ps->sz - 1; i++)
{
for (j = 0; j < ps->sz - i - 1; j++)
{
if (ps->data[j] > ps->data[j + 1])
{
int tmp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = tmp;
}
}
}
}
void test1()
{
SeqList plist;
InitSeqList(&plist);
PushBack(&plist, 1);
PushBack(&plist, 2);
PushBack(&plist, 3);
PushBack(&plist, 4);
PushBack(&plist, 5);
PrintSeqList(&plist);
PopBack(&plist);
PrintSeqList(&plist);
PopBack(&plist);
PrintSeqList(&plist);
PopBack(&plist);
PrintSeqList(&plist);
PopBack(&plist);
PrintSeqList(&plist);
PopBack(&plist);
PrintSeqList(&plist);
printf("\n");
}
void test2()
{
SeqList plist;
InitSeqList(&plist);
PushFront(&plist, 1);
PrintSeqList(&plist);
PushFront(&plist, 2);
PushFront(&plist, 3);
PushFront(&plist, 4);
PushFront(&plist, 5);
PrintSeqList(&plist);
PopFront(&plist);
PrintSeqList(&plist);
PopFront(&plist);
PrintSeqList(&plist);
PopFront(&plist);
PrintSeqList(&plist);
PopFront(&plist);
PrintSeqList(&plist);
PopFront(&plist);
PrintSeqList(&plist);
printf("\n");
}
void test3()
{
SeqList plist;
InitSeqList(&plist);
PushFront(&plist, 1);
PushFront(&plist, 2);
PushFront(&plist, 3);
PushFront(&plist, 4);
PushFront(&plist, 5);
Insert(&plist, 2, 6);
PrintSeqList(&plist);
printf("\n");
}
void test4()
{
SeqList plist;
InitSeqList(&plist);
PushBack(&plist, 1);
PushBack(&plist, 2);
PushBack(&plist, 2);
PushBack(&plist, 4);
PushBack(&plist, 2);
PrintSeqList(&plist);
RemoveAll(&plist, 2);
PrintSeqList(&plist);
printf("\n");
}
void test5()
{
SeqList plist;
InitSeqList(&plist);
PushBack(&plist, 1);
PushBack(&plist, 2);
PushBack(&plist, 3);
PushBack(&plist, 4);
PushBack(&plist, 5);
PrintSeqList(&plist);
int ret = Find(&plist, 2);
printf("%d\n", ret);
Remove(&plist, 3);
PrintSeqList(&plist);
printf("\n");
}
void test6()
{
SeqList plist;
InitSeqList(&plist);
PushBack(&plist, 1);
PushBack(&plist, 3);
PushBack(&plist, 5);
PushBack(&plist, 4);
PushBack(&plist, 2);
PrintSeqList(&plist);
SortList(&plist);
PrintSeqList(&plist);
ReverseList(&plist);
PrintSeqList(&plist);
printf("\n");
}
void test7()
{
SeqList plist;
InitSeqList(&plist);
PushBack(&plist, 1);
PushBack(&plist, 2);
PushBack(&plist, 3);
PushBack(&plist, 4);
PushBack(&plist, 5);
int ret = BinarySearch(&plist, 4);
printf("%d\n", ret);
}
int main()
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
system("pause");
return 0;
}