顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组 上完成数据的增删查改;在这份代码里分别完成了一些线性表的功能;在我理解,线性表就是将数组结构化。
list.h
#ifndef _LIST_H_
#define _LIST_H_
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct SeqList
{
int *arr;
size_t size;
size_t capicity;
}List;
void ShowList(List *s);//打印
void InitializationList(List *s);//初始化
void CapacityExpansionList(List *s);//扩容
void FrountPushInsert(List *s , int add);//头插
void FrountDeleteList(List *s);//头删
void BackPushList(List *s, int add);//尾插
void BackDeleteList(List *s);//尾删
void ArbitrarilyInsertList(List *s, int num, int add);//任意插
void ArbitrarilyDeleteList(List *s, int num);//任意删
void BubbleSortList(List *s);//冒泡排序
void BinarySearchList(List *s);//二分查找
#endif
list.c
#include"list.h"
void InitializationList(List *s)//初始化
{
assert(s != NULL);
s->arr = NULL;
s->capicity = 0;
s->size = 0;
}
void CapacityExpansionList(List *s)//扩容
{
assert(s != NULL);
if (s->size == s->capicity)
{
size_t size = ((s->capicity > 0) ? (2 * (s->capicity)) : 5);
s->capicity = size;
}
s->arr = (int*)realloc(s->arr, (s->capicity * sizeof(int)));
}
void FrountPushInsert(List *s, int add)//头插
{
assert(s != NULL);
ArbitrarilyInsertList(s,s->size+1,add);
}
void FrountDeleteList(List *s)//头删
{
assert(s != NULL);
ArbitrarilyDeleteList(s, s->size);
}
void ShowList(List *s)//打印
{
assert(s != NULL);
int i = 0;
for (i = 0; i < s->size; i++)
{
printf("%d", s->arr[i]);
}
printf("\n");
}
void BackPushList(List *s, int add) //尾插
{
assert(s != NULL);
ArbitrarilyInsertList(s, 1, add);
}
void BackDeleteList(List *s)//尾删
{
assert(s != NULL);
ArbitrarilyDeleteList(s, 1);
}
void ArbitrarilyInsertList(List *s, size_t num, int add)//任意插
{
assert(s != NULL);
CapacityExpansionList(s);
size_t size = s->size;
while (size >= num)
{
s->arr[size] = s->arr[size - 1];
size--;
}
s->arr[size] = add;
s->size++;
}
void ArbitrarilyDeleteList(List *s, size_t num)//任意删
{
assert(s != NULL);
if (num > 0)
{
num--;
while (num < s->size)
{
s->arr[num] = s->arr[num + 1];
num++;
}
s->size--;
}
}
void BubbleSortList(List *s)//冒泡排序
{
assert(s != NULL);
size_t size = s->size-1;
int i = 0;
int count = 0;
while (size)
{
for (i = 0; i < size; i++)
{
if (s->arr[i + 1] < s->arr[i])
{
int tmp = s->arr[i + 1];
s->arr[i + 1] = s->arr[i];
s->arr[i] = tmp;
count++;
}
}
if (count == 0)
{
break;
}
size--;
}
}
void BinarySearchList(List *s,int se)//二分查找
{
int left = 0;
int right = (s->size) - 1;
while (right >= left)
{
int mid = (left+right)/2;
if (s->arr[mid] == se)
{
printf("Find the subscript is %d\n", mid);
break;
}
else if (s->arr[mid] > se)
{
right = mid;
}
else if (s->arr[mid] < se)
{
left = mid;
}
}
}
test.c
#include"list.h"
void Funs()
{
List pList;
InitializationList(&pList);
FrountPushInsert(&pList, 4);//头插
FrountPushInsert(&pList, 2);//头插
FrountPushInsert(&pList, 5);//头插
FrountPushInsert(&pList, 3);//头插
FrountPushInsert(&pList, 8);//头插
ShowList(&pList);//打印
FrountDeleteList(&pList);//头删
ShowList(&pList);//打印
BackPushList(&pList, 9);//尾插
BackPushList(&pList, 2);//尾插
BackPushList(&pList, 5);//尾插
BackPushList(&pList, 0);//尾插
ShowList(&pList);//打印
BackDeleteList(&pList);//尾删
ShowList(&pList);//打印
ArbitrarilyInsertList(&pList, 3, 8);//任意插
ShowList(&pList);//打印
ArbitrarilyDeleteList(&pList, 3);
ShowList(&pList);//打印
BubbleSortList(&pList);
ShowList(&pList);
BinarySearchList(&pList, 3);
}
int main()
{
Funs();
system("pause");
return 0;
}