要实现顺序表,那么首先我们得知道什么是顺序表,顺序表有什么作用等等。。。。
将表中元素一个接一个的存入一组连续的存储单元中,这种存储结构是顺序结构。
采用顺序存储结构的线性表简称为“顺序表”。顺序表的存储特点是:只要确定了起始位置,表中任一元素的地址都通过上一个位置确定。
下面我们来具体实现一下。。。。
SeqListD.h //函数声明
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include <stdio.h>
#include <assert.h>
#include <string.h>
#define MAX 10
typedef int DataType;
typedef struct SeqList
{
DataType data[MAX];
int sz;
}SeqList, *pSeqList;
//声明
void InitSeqList(pSeqList ps);
void PushBack(pSeqList ps, DataType d);//尾部插入一个元素
void PopBack(pSeqList ps);//尾部删除
void Display(const pSeqList ps);//打印信息
void PushFront(pSeqList ps, DataType d);//头部插入
void PopFront(pSeqList ps);//头部删除
int Find(pSeqList ps, DataType d);//查找
void Insert(pSeqList ps, DataType d, int pos);//指定位置删除
void Remove(pSeqList ps, DataType d);//删除指定位置元素
void RemoveAll(pSeqList ps, DataType d); //删除指定元素的所有
void Reverse(pSeqList ps);//逆序
void Sort(pSeqList ps);//排序
int BinarySearch(pSeqList ps, DataType d);//二分查找
#endif //__SEQLIST_H__
SeqListD.c //函数具体实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void InitSeqList(pSeqList ps)
{
ps->sz = 0;
memset(ps->data, 0, sizeof(ps->data));
}
void Display(const pSeqList ps)
{
assert(ps != NULL);
int i = 0;
for (i = 0; i < ps->sz; i++)
{
printf("%d ", ps->data[i]);
}
printf("\n");
}
void PushBack(pSeqList ps, DataType d)
{
assert(ps != NULL);
if (ps->sz == MAX)
{
return;
}
ps->data[ps->sz] = d;
ps->sz++;
}
void PopBack(pSeqList ps, DataType d)
{
assert(ps->sz != NULL);
if (ps->sz == 0)
{
return 0;
}
ps->sz--;
}
void PushFront(pSeqList ps, DataType d)
{
assert(ps != NULL);
if (ps->sz == MAX)
{
return;
}
int i = 0;
memmove(ps->data + 1, ps->data, sizeof(DataType)*(ps->sz));
/*for (i = ps->sz; i > 0; i--)
{
ps->data[i] = ps->data[i - 1];
}*/
ps->data[0] = d;
ps->sz++;
}
void PopFront(pSeqList ps)
{
int i = 0;
assert(ps != NULL);
if (ps->sz==0)
{
return ;
}
for (i = 0; i < ps->sz-1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->sz--;
}
int Find(pSeqList ps, DataType d)
{
int i = 0;
assert(ps != NULL);
for (i = 0; i < ps->sz; i++)
{
if (d == ps->data[i])
{
return i;
}
}
return -1;
}
void Insert(pSeqList ps, DataType d, int pos)
{
int i = 0;
assert(ps != NULL);
if (ps->sz < MAX)
{
for (i = ps->sz; i>pos; i--)
{
ps->data[i] = ps->data[i - 1];
}
ps->data[pos] = d;
ps->sz++;
}
}
void Remove(pSeqList ps, DataType d)
{
int pos = 0;
assert(ps != NULL);
pos = Find(ps, d);
if (pos != -1)
{
memmove(ps->data + pos, ps->data + pos + 1, sizeof(DataType)*(ps->sz-pos-1));
ps->sz--;
}
}
void RemoveAll(pSeqList ps, DataType d)
{
int i = 0;
assert(ps != NULL);
for (i = 0; i < ps->sz; i++)
{
if (d == ps->data[i])
{
memmove(ps->data+i, ps->data+i+1, sizeof(DataType)*(ps->sz - i- 1));
ps->sz--;
}
}
}
void Reverse(pSeqList ps)
{
DataType *left = ps->data;
DataType *right = ps->data + ps->sz - 1;
assert(ps != NULL);
while (left < right)
{
DataType temp = *left;
*left = *right;
*right = temp;
left++;
right--;
}
}
void Sort(pSeqList ps)
{
int i = 0;
int j = 0;
assert(ps != NULL);
for (i = 0; i < ps->sz-1; i++)
{
for (j = 0; j < ps->sz-1; j++)
{
if (ps->data[j]>ps->data[j + 1])
{
DataType temp = 0;
temp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = temp;
}
}
}
}
int BinarySearch(pSeqList ps, DataType d)
{
int i = 0;
int left = 0;
int right = ps->sz-1;
assert(ps != NULL);
while (left < right)
{
int mid = left + (right - left) / 2;
if (ps->data[mid]>d)
{
right = mid - 1;
}
else if (ps->data[mid]<d)
{
left = mid + 1;
}
else
{
return mid;
}
/*for (i = 0; i < ps->sz - 1; i++)
{
if (ps->data[i] == d)
{
return i;
}
}*/
}
return -1;
}
test.c //函数测试
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
test1()
{
SeqList my_list;
InitSeqList(&my_list);
PushBack(&my_list, 1);
PushBack(&my_list, 2);
PushBack(&my_list, 3);
Display(&my_list);
PopBack(&my_list);
Display(&my_list);
PopBack(&my_list);
Display(&my_list);
PopBack(&my_list);
Display(&my_list);
}
test2()
{
SeqList my_list;
InitSeqList(&my_list);
PushFront(&my_list, 1);
PushFront(&my_list, 2);
PushFront(&my_list, 3);
Display(&my_list);
PopFront(&my_list);
PopFront(&my_list);
Display(&my_list);
}
test3()
{
int pos = 0;
SeqList my_list;
InitSeqList(&my_list);
PushFront(&my_list, 1);
PushFront(&my_list, 2);
PushFront(&my_list, 3);
PushFront(&my_list, 4);
Display(&my_list);
pos= Find(&my_list, 3);
if (pos ==-1)
{
printf("要查找的元素不存在\n");
}
else
{
Insert(&my_list, 7, pos);
}
Display(&my_list);
}
test4()
{
int pos = 0;
SeqList my_list;
InitSeqList(&my_list);
PushFront(&my_list, 1);
PushFront(&my_list, 2);
PushFront(&my_list, 3);
PushFront(&my_list, 4);
Display(&my_list);
Remove(&my_list, 1);
Display(&my_list);
}
test5()
{
int pos = 0;
SeqList my_list;
InitSeqList(&my_list);
PushFront(&my_list, 1);
PushFront(&my_list, 2);
PushFront(&my_list, 3);
PushFront(&my_list, 1);
PushFront(&my_list, 4);
PushFront(&my_list, 1);
Display(&my_list);
RemoveAll(&my_list, 1);
Display(&my_list);
}
test6()
{
SeqList my_list;
InitSeqList(&my_list);
PushFront(&my_list, 1);
PushFront(&my_list, 2);
PushFront(&my_list, 3);
PushFront(&my_list, 4);
Display(&my_list);
Reverse(&my_list);
Display(&my_list);
}
test7()
{
SeqList my_list;
InitSeqList(&my_list);
PushFront(&my_list, 1);
PushFront(&my_list, 2);
PushFront(&my_list, 3);
PushFront(&my_list, 6);
PushFront(&my_list, 0);
PushFront(&my_list, 4);
Display(&my_list);
Sort(&my_list);
Display(&my_list);
}
test8()
{
int ret = 0;
SeqList my_list;
InitSeqList(&my_list);
PushBack(&my_list, 1);
PushBack(&my_list, 2);
PushBack(&my_list, 3);
PushBack(&my_list, 4);
PushBack(&my_list, 5);
PushBack(&my_list, 6);
Display(&my_list);
ret=BinarySearch(&my_list,3);
printf("%d\n", ret);
}
int main()
{
//test1();
//test2();
//test3();
//test4();
//test5();
//test6();
//test7();
test8();
getchar();
return 0;
}