Seqlist.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
typedef int DataType;
typedef struct Seqlist
{
DataType data[MAX];//数据
DataType sz;//数据个数
}Seqlist,*pSeqlist;
//初始化顺序表
void InitSeqlist(pSeqlist pSeq);//pSeqlist=Seqlist *
//尾插
void Pushback(pSeqlist pSeq, DataType d);
//打印顺序表
void PrintSeqlist(pSeqlist pseq);
//头插
void PushFront(pSeqlist pSeq, DataType d);
//头删
void Popfront(pSeqlist pSeq);
//尾删
void Popback(pSeqlist pSeq);
//查找指定元素
int Find(pSeqlist pSeq, DataType d);
//在pos位置插入d元素
void Insert(pSeqlist pSeq,int pos, DataType d);
//删除pos位置的元素
void Del(pSeqlist pSeq, int pos);
//删除d元素
void Remove(pSeqlist pSeq, DataType d);
//删除所有的d元素
void Removeall(pSeqlist pSeq, DataType d);
//顺序表的大小
int Size(pSeqlist pSeq);
//判断顺序表是否为空
int Empty(pSeqlist pSeq);
//对顺序表的元素冒泡排序
void Bubblesort(pSeqlist pSeq);
void Swap(DataType* p1, DataType* p2);
//选择排序
void Selectsort1(pSeqlist pSeq);
//优化选择排序
void Selectsort2(pSeqlist pSeq);
//二分查找指定元素
int Binarysearch(pSeqlist pSeq, DataType d);
//递归二分查找
int Binarysearch2(pSeqlist pSeq,int left,int right, DataType d);
#endif
Seqlist.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Seqlist.h"
//初始化顺序表
void InitSeqlist(pSeqlist pSeq)
{
assert(pSeq != NULL);
pSeq->sz = 0;
memset(pSeq->data, 0, sizeof(pSeq->data));
}
//顺序表的尾插
void Pushback(pSeqlist pSeq, DataType d)
{
assert(pSeq != NULL);
if (pSeq->sz == MAX)
{
printf("顺序表已满");
return ;
}
pSeq->data[pSeq->sz] = d;
pSeq->sz++;
}
//打印顺序表
void PrintSeqlist(pSeqlist pSeq)
{
int i = 0;
assert(pSeq != NULL);
if (pSeq->sz == 0)
{
printf("顺序表为空");
return;
}
for (i = 0; i < pSeq->sz; i++)
{
printf("%d ", pSeq->data[i]);
}
printf("\n");
}
//顺序表的尾删
void Popback(pSeqlist pSeq)
{
assert(pSeq != NULL);
if (pSeq->sz == 0)
{
printf("顺序表为空无法删除");
}
pSeq->sz--;
}
//顺序表的头插
void PushFront(pSeqlist pSeq, DataType d)
{
int i = 0;
assert(pSeq != NULL);
if (pSeq->sz == MAX)
{
printf("顺序表已经满了");
return;
}
for (i = pSeq->sz - 1; i >= 0; i--)
{
pSeq->data[i+1] = pSeq->data[i];
}
pSeq->data[0] = d;
pSeq->sz++;
}
//头删
void Popfront(pSeqlist pSeq)
{
int i = 0;
assert(pSeq != NULL);
if (pSeq->sz == 0)
{
printf("顺序表为空无法删除");
return;
}
for (i = 0; i < pSeq->sz-1; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq -> sz--;
}
//查找某个元素
int Find(pSeqlist pSeq, DataType d)
{
int i = 0;
assert(pSeq != NULL);
for (i = 0; i < pSeq->sz; i++)
{
if (pSeq->data[i] == d)
{
return i;
}
}
return -1;
}
//在pos位置插入d
void Insert(pSeqlist pSeq, int pos, DataType d)
{
int i = 0;
assert(pSeq != NULL);
assert(pos >= 0 && pos <= pSeq->sz);
if (pSeq->sz == MAX)
{
printf("顺序表已经满了无法插入");
}
for (i = pSeq->sz - 1; i >= pos; i--)
{
pSeq->data[i + 1] = pSeq->data[i];
}
pSeq->data[pos] = d;
pSeq->sz++;
}
//删除某个位置的元素
void Del(pSeqlist pSeq, int pos)
{
int i = 0;
assert(pSeq != NULL);
assert(pos >= 0 && pos <= pSeq->sz);
if (pSeq -> sz == 0)
{
printf("顺序表是空的无法删除");
}
for (i = pos; i < pSeq->sz - 1; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->sz--;
}
//删除指定数据
void Remove(pSeqlist pSeq, DataType d)
{
int i = 0;
assert(pSeq != NULL);
if (pSeq->sz == 0)
{
printf("顺序表为空");
return;
}
for (i = 0; i < pSeq->sz; i++)
{
if ( pSeq->data[i] == d )
{
break;
}
}
if (i == pSeq->sz)
{
printf("删除的元素没有\n");
return;
}
for (; i < pSeq->sz - 1; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->sz--;
}
//删除所有指定数据
void Removeall(pSeqlist pSeq, DataType d)
{
#if 0
assert(pSeq!=NULL);
while ((pos = Find(pSeq, d)) != -1)
{
Del(pSeq, pos);
}
#endif
/*int i = 0;
int j = 0;
assert(pSeq != NULL);
if (pSeq->sz == 0)
{
printf("该顺序表为空无法删除 ");
return;
}
for (i = 0; i < pSeq->sz; i++)
{
if (pSeq->data[i] == d)
{
for (j = i; j < pSeq->sz - 1; j++)
{
pSeq->data[j] = pSeq->data[j + 1];
}
pSeq->sz--;
--i;
}
}*/
#if 0
int i = 0;
int count = 0;
DataType* tmp = (DataType*)malloc(sizeof(DataType) * pSeq->sz);
for (i = 0; i < pSeq->sz; i++)
{
if (pSeq->data[i] != d);
{
tmp[count++] = pSeq->data[i];
}
}
memcpy(pSeq->data, tmp, count*sizeof(DataType));
pSeq->sz = count;
free(tmp);
tmp = NULL;
}
#endif
int i = 0;
int count = 0;
for (i = 0; i < pSeq->sz; i++)
{
if (pSeq->data[i] != d)
{
pSeq->data[count++] = pSeq->data[i];
}
}
pSeq->sz = count;
}
//顺序表的大小
int Size(pSeqlist pSeq)
{
assert(pSeq != NULL);
return pSeq->sz;
}
//顺序表是否为空
int Empty(pSeqlist pSeq)
{
assert(pSeq != NULL);
return pSeq->sz == 0;
}
//冒泡排序
void Bubblesort(pSeqlist pSeq)
{
int i = 0;
int j = 0;
int flag = 0;
assert(pSeq != NULL);
for (i = 0; i < pSeq->sz-1; i++)
{
flag = 0;
for (j = 0; j < pSeq->sz-1-i; j++)
{
if (pSeq->data[j]>pSeq->data[j + 1])
{
Swap(pSeq->data + j, pSeq->data + j + 1);
flag = 1;
}
}
if (flag == 0)
{
return;
}
}
}
void Swap(DataType* p1, DataType* p2)
{
DataType tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
//选择排序
void Selectsort1(pSeqlist pSeq)
{
int i = 0;
int j = 0;
assert(pSeq != NULL);
for (i = 0; i < pSeq->sz - 1; i++)
{
int max = 0;
for (j = 1; j < pSeq->sz - i; j++)
{
if (pSeq->data[max] < pSeq->data[j])
{
max = j;
}
}
if (max != pSeq->sz - 1 - i)
{
Swap(pSeq->data + max, pSeq->data + pSeq->sz - 1 - i);
}
}
}
//选择排序的优化(选出最大值与最小值)
void Selectsort2(pSeqlist pSeq)
{
int start = 0;
int end = pSeq->sz - 1;
assert(pSeq != NULL);
while(start < end)
{
int max = start;
int min = start;
int i = 0;
for (i = start; i < end; i++)
{
if (pSeq->data[i]>pSeq->data[max])
{
max = i;
}
if (pSeq->data[i] < pSeq->data[min])
{
min = i;
}
}
if (min != start)
{
Swap(pSeq->data + min, pSeq->data + start);
}
if (max == start)
{
max = min;
}
if (max != end)
{
Swap(pSeq->data + max, pSeq->data + end);
}
start++;
end--;
}
}
//二分查找
int Binarysearch(pSeqlist pSeq, DataType d)
{
#if 0
int left = 0;
int right = pSeq->sz - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (pSeq->data[mid] < d)
{
left= mid +1;
}
else if (pSeq->data[mid] > d)
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
#endif
}
//二分查找的递归
int Binarysearch2(pSeqlist pSeq, int left, int right, DataType d)
{
int mid = 0;
if (left > right)
return -1;
mid = left + ((right - left) >> 1);
if (pSeq->data[mid] > d)
{
Binarysearch2(pSeq, left, mid - 1, d);
}
else if (pSeq->data[mid] < d)
{
Binarysearch2(pSeq, mid + 1, right, d);
}
else
return mid;
}
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Seqlist.h"
//
//int main()
//{
// Seqlist seq;
// InitSeqlist(&seq);
// Pushback(&seq, 1);
// Pushback(&seq, 2);
// Pushback(&seq, 3);
// Pushback(&seq, 4);
// PrintSeqlist(&seq);
// Popback(&seq);
// PrintSeqlist(&seq);
// system("pause");
// return 0;
//}
void TestPushBack()
{
Seqlist seq;
InitSeqlist(&seq);
Pushback(&seq, 1);
Pushback(&seq, 2);
Pushback(&seq, 3);
Pushback(&seq, 4);
PrintSeqlist(&seq);
Popback(&seq);
PrintSeqlist(&seq);
system("pause");
return ;
}
void TestPushFront()
{
Seqlist seq;
InitSeqlist(&seq);
PushFront(&seq, 1);
PushFront(&seq, 2);
PushFront(&seq, 3);
PushFront(&seq, 4);
PrintSeqlist(&seq);
Popfront(&seq);
PrintSeqlist(&seq);
}
void Testfind()
{
int pos = 0;
Seqlist seq;
InitSeqlist(&seq);
PushFront(&seq, 1);
PushFront(&seq, 2);
PushFront(&seq, 3);
PushFront(&seq, 4);
PrintSeqlist(&seq);
pos = Find(&seq, 3);
if (pos == -1)
{
printf("找不到\n");
}
else
{
printf("找到了,下标为: %d\n", pos);
}
}
void TestInsert()
{
int pos = 0;
Seqlist seq;
InitSeqlist(&seq);
PushFront(&seq, 1);
PushFront(&seq, 2);
PushFront(&seq, 3);
PushFront(&seq, 4);
PrintSeqlist(&seq);
pos = Find(&seq, 3);
if (pos != -1)
{
Insert(&seq, pos, 5);
}
PrintSeqlist(&seq);
}
void TestDel()
{
int pos = 0;
Seqlist seq;
InitSeqlist(&seq);
PushFront(&seq, 1);
PushFront(&seq, 2);
PushFront(&seq, 3);
PushFront(&seq, 4);
PrintSeqlist(&seq);
pos = Find(&seq, 3);
if (pos != -1)
{
Del(&seq, pos );
}
PrintSeqlist(&seq);
}
void TestRemove()
{
int pos = 0;
Seqlist seq;
InitSeqlist(&seq);
PushFront(&seq, 1);
PushFront(&seq, 2);
PushFront(&seq, 3);
PushFront(&seq, 4);
PrintSeqlist(&seq);
Remove(&seq, 6);
PrintSeqlist(&seq);
}
void TestRemoveall()
{
int pos = 0;
Seqlist seq;
InitSeqlist(&seq);
PushFront(&seq, 1);
PushFront(&seq, 1);
PushFront(&seq, 1);
PushFront(&seq, 1);
PushFront(&seq, 1);
PushFront(&seq, 2);
PushFront(&seq, 3);
PushFront(&seq, 4);
if (!Empty(&seq))
printf("大小为%d\n", Size(&seq));
PrintSeqlist(&seq);
Removeall(&seq, 1);
PrintSeqlist(&seq);
}
void Testbubblesort()
{
int i = 0;
Seqlist seq;
InitSeqlist(&seq);
for (i = 0; i < 10; i++)
{
PushFront(&seq, i);
}
PrintSeqlist(&seq);
Bubblesort(&seq);
PrintSeqlist(&seq);
}
void TestSelectsort()
{
int i = 0;
Seqlist seq;
InitSeqlist(&seq);
for (i = 0; i < 10; i++)
{
PushFront(&seq, i);
}
PrintSeqlist(&seq);
Selectsort2(&seq);
PrintSeqlist(&seq);
}
void TestBinarysearch()
{
int i = 0;
int pos = 0;
Seqlist seq;
InitSeqlist(&seq);
for (i = 0; i < 10; i++)
{
PushFront(&seq, i);
}
PrintSeqlist(&seq);
Selectsort2(&seq);
PrintSeqlist(&seq);
pos = Binarysearch2(&seq,0,seq.sz-1,7);
if (pos != -1)
{
printf("找到了下标为 %d\n", pos);
}
else
{
printf("找不到\n");
}
}
int main()
{
/*TestPushBack();*/
/*TestPushFront();*/
/* Testfind();*/
/*TestInsert();*/
/*TestDel();*/
//TestRemove();
/*TestRemoveall();*/
/*Testbubblesort();*/
//TestSelectsort();
TestBinarysearch();
system("pause");
}