1.静态顺序表的定义
//静态顺序表
typedef int DataType;
typedef struct SeqList
{
DataType data[MAX]; //顺序表中的数据
int sz;//表示当前顺序表有效数据的个数
}SeqList, *pSeqList;
2.初始化
//初始化
void InitSeqList(pSeqList ps)
{
assert(ps != NULL);
ps->sz = 0;
}
3.打印顺序表
//打印顺序表
void PrintSeqList(const pSeqList ps)
{
int i = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
printf("顺序表为空\n");
return;
}
else
{
for (i = 0; i < ps->sz; i++)
{
printf("%d ", ps->data[i]);
}
printf("\n");
}
}
4.增->尾插法
void PushBack(pSeqList ps, DataType d)
{
assert(ps != NULL);
if (ps->sz == MAX)
{
printf("顺序表已满");
return;
}
else
{
ps->data[ps->sz] = d;
ps->sz++;
}
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PrintSeqList(&Seq);
PushBack(&Seq, 2);
PrintSeqList(&Seq);
PushBack(&Seq, 3);
PrintSeqList(&Seq);
PushBack(&Seq, 4);
PrintSeqList(&Seq);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
}
5.增->头插法
void PushFront(pSeqList ps, DataType d)
{//头插
int i = 0;
assert(ps != NULL);
if (ps->sz == MAX)
{
printf("顺序表已满,无法插入\n");
return;
}
else
{
for (i = ps->sz; i > 0; i--)
{
ps->data[i] = ps->data[i-1];
}
ps->data[0] = d;
ps->sz++;
}
}
测试
void testPushFront()
{
PushFront(&Seq, 1);
PrintSeqList(&Seq);
PushFront(&Seq, 2);
PrintSeqList(&Seq);
PushFront(&Seq, 3);
PrintSeqList(&Seq);
PushFront(&Seq, 4);
PrintSeqList(&Seq);
PushFront(&Seq, 5);
PrintSeqList(&Seq);
}
6.增->指定位置插入
void Insert(pSeqList ps, int pos, DataType d)
{//指定位置插入
int i = 0;
assert(ps != NULL);
assert(ps->sz < MAX);
assert(pos >= 0 && pos <= ps->sz);
for (i = ps->sz - 1; i >= pos; i--)
{
ps->data[i + 1] = ps->data[i];
}
ps->data[pos] = d;
ps->sz++;
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
PopBack(&Seq);
PrintSeqList(&Seq);
PopFront(&Seq);
PrintSeqList(&Seq);
Insert(&Seq, 2, 9);
PrintSeqList(&Seq);
}
7.删->尾删法
void PopBack(pSeqList ps)
{//尾删
assert(ps != NULL);
if (ps->sz == 0)
{
printf("顺序表为空,无法删除\n");
return;
}
else
{
ps->sz--;
}
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
PopBack(&Seq);
PrintSeqList(&Seq);
}
8.删->头删
void PopFront(pSeqList ps)
{//头删
int i = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
printf("顺序表为空 \n");
return;
}
else
{
for (i = 0; i < ps->sz-1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->sz--;
}
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
PopBack(&Seq);
PrintSeqList(&Seq);
PopFront(&Seq);
PrintSeqList(&Seq);
}
9.查找指定位置元素
int Find(pSeqList ps, DataType d)
{//查指定元素,找到了返回下标,没找到返回-1
int i = 0;
assert(ps != NULL);
for (i = 0; i < ps->sz; i++)
{
if (ps->data[i] == d)
return i;
}
return -1;
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
PopBack(&Seq);
PrintSeqList(&Seq);
PopFront(&Seq);
PrintSeqList(&Seq);
Insert(&Seq, 2, 9);
PrintSeqList(&Seq);
int i=Find(&Seq, 3);
printf("%d\n", i);
}
10.删除指定位置的元素
void Erase(pSeqList ps, int pos)
{//删除指定位置的元素
int i = 0;
assert(ps != NULL);
assert(ps->sz < MAX);
assert(pos >= 0 && pos <= ps->sz);
for (i = pos; i < ps->sz-1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->sz--;
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
//PopBack(&Seq);
//PrintSeqList(&Seq);
//PopFront(&Seq);
//PrintSeqList(&Seq);
//Insert(&Seq, 2, 9);
//PrintSeqList(&Seq);
//int i=Find(&Seq, 3);
//printf("%d\n", i);
Erase(&Seq, 3);
PrintSeqList(&Seq);
}
11.删除元素
void Remove(pSeqList ps, DataType d)
{//删除元素
assert(ps != NULL);
int pos = Find(ps, d);
if (pos == -1)
{
printf("要删除的元素不存在\n");
return;
}
else
{
Erase(ps, pos);
}
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
//PopBack(&Seq);
//PrintSeqList(&Seq);
//PopFront(&Seq);
//PrintSeqList(&Seq);
//Insert(&Seq, 2, 9);
//PrintSeqList(&Seq);
//int i=Find(&Seq, 3);
//printf("%d\n", i);
//Erase(&Seq, 3);
//PrintSeqList(&Seq);
Remove(&Seq, 2);
PrintSeqList(&Seq);
}
12.排序
void Sort(pSeqList ps)
{//冒泡排序
assert(ps != NULL);
int i = 0;
int j = 0;
int flag = 0;
DataType tmp = 0;
for (i = 0; i < ps->sz - 1; i++)
{
flag = 0;
for (j = 0; j < ps->sz - 1 - i; j++)
{
if (ps->data[j] > ps->data[j + 1])
{
tmp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = tmp;
flag = 1;
}
}
if (flag == 0)
return;
}
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
PushBack(&Seq, 0);
PrintSeqList(&Seq);
//PopBack(&Seq);
//PrintSeqList(&Seq);
//PopFront(&Seq);
//PrintSeqList(&Seq);
//Insert(&Seq, 2, 9);
//PrintSeqList(&Seq);
//int i=Find(&Seq, 3);
//printf("%d\n", i);
//Erase(&Seq, 3);
//PrintSeqList(&Seq);
//Remove(&Seq, 2);
//PrintSeqList(&Seq);
Sort(&Seq);
PrintSeqList(&Seq);
}
13.二分查找
int BinarySearch(pSeqList ps, DataType d)
{//二分查找
assert(ps != NULL);
int left = 0;
int right = ps->sz - 1;
int mid = left + (right - left) / 2;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (d<ps->data[mid])
{
right = mid - 1;
}
if (d > ps->data[mid])
{
left = mid + 1;
}
if(d==ps->data[mid])
return mid;
}
return -1;
}
测试
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
PushBack(&Seq, 6);
PrintSeqList(&Seq);
//PopBack(&Seq);
//PrintSeqList(&Seq);
//PopFront(&Seq);
//PrintSeqList(&Seq);
//Insert(&Seq, 2, 9);
//PrintSeqList(&Seq);
//int i=Find(&Seq, 3);
//printf("%d\n", i);
//Erase(&Seq, 3);
//PrintSeqList(&Seq);
//Remove(&Seq, 2);
//PrintSeqList(&Seq);
//Sort(&Seq);
//PrintSeqList(&Seq);
int j=BinarySearch(&Seq, 4);
printf("%d\n", j);
}
SeqList.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MAX 1000
//#define DEFAULT_SZ 3
//#define DEFAULT_INC 2
//
typedef int DataType;
typedef struct SeqList
{
DataType *data;//数据区
int sz;//有效个数
int capacity;//容量
}SeqList, *pSeqList;
//静态顺序表
typedef int DataType;
typedef struct SeqList
{
DataType data[MAX];
int sz;//表示当前顺序表有效数据的个数
}SeqList, *pSeqList;
void InitSeqList(pSeqList ps);
void DestroySeqList(pSeqList ps);
void PushBack(pSeqList ps, DataType d);
void PrintSeqList(const pSeqList ps);
void PopBack(pSeqList ps);
void PushFront(pSeqList ps, DataType d);
void PopFront(pSeqList ps);
void Insert(pSeqList ps, int pos, DataType d);
void Erase(pSeqList ps, int pos);
void Sort(pSeqList ps);
int BinarySearch(pSeqList ps, DataType d);
int Find(pSeqList ps, DataType d);
void Remove(pSeqList ps, DataType d);
// RemoveAll();
#endif //__SEQLIST_H__
SeqList.c
#include"SeqList.h"
//初始化
void InitSeqList(pSeqList ps)
{
assert(ps != NULL);
ps->sz = 0;
}
void PushBack(pSeqList ps, DataType d)
{//尾插
assert(ps != NULL);
if (ps->sz == MAX)
{
printf("顺序表已满");
return;
}
else
{
ps->data[ps->sz] = d;
ps->sz++;
}
}
void PrintSeqList(const pSeqList ps)
{//打印顺序表
int i = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
printf("顺序表为空\n");
return;
}
else
{
for (i = 0; i < ps->sz; i++)
{
printf("%d ", ps->data[i]);
}
printf("\n");
}
}
void PushFront(pSeqList ps, DataType d)
{//头插
int i = 0;
assert(ps != NULL);
if (ps->sz == MAX)
{
printf("顺序表已满,无法插入\n");
return;
}
else
{
for (i = ps->sz; i > 0; i--)
{
ps->data[i] = ps->data[i-1];
}
ps->data[0] = d;
ps->sz++;
}
}
void PopBack(pSeqList ps)
{//尾删
assert(ps != NULL);
if (ps->sz == 0)
{
printf("顺序表为空,无法删除\n");
return;
}
else
{
ps->sz--;
}
}
void PopFront(pSeqList ps)
{//头删
int i = 0;
assert(ps != NULL);
if (ps->sz == 0)
{
printf("顺序表为空 \n");
return;
}
else
{
for (i = 0; i < ps->sz-1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->sz--;
}
}
void Insert(pSeqList ps, int pos, DataType d)
{//指定位置插入
int i = 0;
assert(ps != NULL);
assert(ps->sz < MAX);
assert(pos >= 0 && pos <= ps->sz);
for (i = ps->sz - 1; i >= pos; i--)
{
ps->data[i + 1] = ps->data[i];
}
ps->data[pos] = d;
ps->sz++;
}
void Erase(pSeqList ps, int pos)
{//删除指定位置的元素
int i = 0;
assert(ps != NULL);
assert(ps->sz < MAX);
assert(pos >= 0 && pos <= ps->sz);
for (i = pos; i < ps->sz-1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->sz--;
}
int Find(pSeqList ps, DataType d)
{//查指定元素,找到了返回下标,没找到返回-1
int i = 0;
assert(ps != NULL);
for (i = 0; i < ps->sz; i++)
{
if (ps->data[i] == d)
return i;
}
return -1;
}
void Remove(pSeqList ps, DataType d)
{//删除元素
assert(ps != NULL);
int pos = Find(ps, d);
if (pos == -1)
{
printf("要删除的元素不存在\n");
return;
}
else
{
Erase(ps, pos);
}
}
void Sort(pSeqList ps)
{//冒泡排序
assert(ps != NULL);
int i = 0;
int j = 0;
int flag = 0;
DataType tmp = 0;
for (i = 0; i < ps->sz - 1; i++)
{
flag = 0;
for (j = 0; j < ps->sz - 1 - i; j++)
{
if (ps->data[j] > ps->data[j + 1])
{
tmp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = tmp;
flag = 1;
}
}
if (flag == 0)
return;
}
}
int BinarySearch(pSeqList ps, DataType d)
{//二分查找
assert(ps != NULL);
int left = 0;
int right = ps->sz - 1;
int mid = left + (right - left) / 2;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (d<ps->data[mid])
{
right = mid - 1;
}
if (d > ps->data[mid])
{
left = mid + 1;
}
if(d==ps->data[mid])
return mid;
}
return -1;
}
test.c
#include "SeqList.h"
SeqList Seq;
void testPushBack()
{
PushBack(&Seq, 1);
PushBack(&Seq, 2);
PushBack(&Seq, 3);
PushBack(&Seq, 4);
PushBack(&Seq, 5);
PrintSeqList(&Seq);
PushBack(&Seq, 6);
PrintSeqList(&Seq);
//PopBack(&Seq);
//PrintSeqList(&Seq);
//PopFront(&Seq);
//PrintSeqList(&Seq);
//Insert(&Seq, 2, 9);
//PrintSeqList(&Seq);
//int i=Find(&Seq, 3);
//printf("%d\n", i);
//Erase(&Seq, 3);
//PrintSeqList(&Seq);
//Remove(&Seq, 2);
//PrintSeqList(&Seq);
//Sort(&Seq);
//PrintSeqList(&Seq);
int j=BinarySearch(&Seq, 4);
printf("%d\n", j);
}
void testPushFront()
{
PushFront(&Seq, 1);
PrintSeqList(&Seq);
PushFront(&Seq, 2);
PrintSeqList(&Seq);
PushFront(&Seq, 3);
PrintSeqList(&Seq);
PushFront(&Seq, 4);
PrintSeqList(&Seq);
PushFront(&Seq, 5);
PrintSeqList(&Seq);
}
int main()
{
testPushBack();
//testPushFront();
return 0;
}
下一篇:顺序表(动态)https://blog.youkuaiyun.com/qq_40550018/article/details/82683958