给出顺序表的定义:
typedef int DataType;
// 静态顺序表
typedef struct SeqList
{
DataType array[MAX_SIZE];
int size; // 表示顺序表中有效元素的个数
/*
size的值为数组下标,从零开始。例如:size = 2,里面存了两个元素,
第一个元素的下标为0,第二个元素的下标为1;size在两个元素后面,下标为3的位置
*/
}SeqList, *PSeqList;
将函数的声明放在head.h的头文件里面:
#ifndef __SQELIST_H_
#define __SQELIST_H__
#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAX_SIZE 10
typedef int DataType;
// 静态顺序表
typedef struct SeqList
{
DataType array[MAX_SIZE];
int size; // 表示顺序表中有效元素的个数
/*
size的值为数组下标,从零开始。例如:size = 2,里面存了两个元素,
第一个元素的下标为0,第二个元素的下标为1;size在两个元素后面,下标为3的位置
*/
}SeqList, *PSeqList;
//
// 初始化顺序表
void InitSeqList(PSeqList pSeq);
// 在顺序表的尾部插入元素data
void SeqListPushBack(PSeqList pSeq, DataType data);
// 删除顺序表尾部的元素
void SeqListPopBack(PSeqList pSeq);
// 在顺序表的头部插入元素data
void SeqListPushFront(PSeqList pSeq, DataType data);
// 删除顺序表头部元素
void SeqListPopFront(PSeqList pSeq);
// 在顺序表的pos位置插入元素data
void SeqListInsert(PSeqList pSeq, int pos, DataType data);
// 删除顺序表pos位置上的元素
void SeqListErase(PSeqList pSeq, int pos);
// 打印顺序表里的元素
void PrintSeqList(PSeqList pSeq);
//查找顺序表中第一个值为data的元素
int Find(PSeqList pSeq, DataType data);
//删除顺序表中第一个值为data的元素
void Remove(PSeqList pSeq, DataType data);
//删除顺序表中所有值为data的元素
void RemoveAll(PSeqList pSeq, DataType data);
//对顺序表进行冒泡排序操作
void BubbleSort(PSeqList pSeq);
//对顺序表进行选择排序操作
//void SelectSort(PSeqList pSeq, Compare com);
void SelectSort(PSeqList pSeq);
//对顺序表进行二分查找操作
int BinarySearch(PSeqList pSeq, DataType data);
//
void funtion_1();
#endif //__SQELIST_H__
函数的定义(具体实现):
#include "seqlist.h" void InitSeqList(PSeqList pSeq) { assert(pSeq); //函数原型:void *memset(void *s, int ch, size_t n); //函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回s。 //memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。 memset(pSeq, 0, sizeof(DataType)*MAX_SIZE); //先清零,再初始化。 pSeq->size = 0; } void SeqListPushBack(PSeqList pSeq, DataType data) { assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("顺序表已满\n"); return ; } pSeq->array[pSeq->size++] = data; } void SeqListPopBack(PSeqList pSeq) { assert(pSeq); if (pSeq->size == 0) { printf("顺序表为空\n"); return; } pSeq->size--; } void SeqListPushFront(PSeqList pSeq, DataType data) { assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("顺序表已满\n"); return; } int i = 0; for (i = pSeq->size - 1; i >= 0; i--) //搬移元素 { pSeq->array[i + 1] = pSeq->array[i]; } pSeq->array[0] = data; //插入元素 pSeq->size++; } void SeqListPopFront(PSeqList pSeq) { assert(pSeq); if (pSeq->size == 0) { printf("顺序表为空\n"); return; } int i = 0; for (i = 1; i < pSeq->size; i++) { pSeq->array[i - 1] = pSeq->array[i]; } pSeq->size--; } void SeqListInsert(PSeqList pSeq, int pos, DataType data) //插入的是下标位置(从0到size所在的下标位置) { assert(pSeq); if (pSeq->size >= MAX_SIZE) { printf("顺序表已满\n"); return; } //检查pos if (!(pos >= 0 && pos <= pSeq->size)) { printf("%d 插入位置不合法。", pos); //要保证数据元素插入后所有元素都是连续的,否则不能叫作顺序表 return; } int i = 0; for (i = pSeq->size - 1; i >= pos; i--) { pSeq->array[i + 1] = pSeq->array[i]; } pSeq->array[pos] = data; pSeq->size++; } void SeqListErase(PSeqList pSeq, int pos) { assert(pSeq); if (pSeq->size == 0) { printf("顺序表为空\n"); return; } if (!(pos >= 0 && pos < pSeq->size)) { printf("%d 位置不合法。", pos); return; } int i = 0; for (i = pos; i < pSeq->size - 1; i++) { pSeq->array[i] = pSeq->array[i + 1]; } pSeq->size--; } void PrintSeqList(PSeqList pSeq) { assert(pSeq); int i = 0; for (i = 0; i < pSeq->size; i++) { printf("%d ", pSeq->array[i]); } printf("\n"); } int Find(PSeqList pSeq, DataType data) { assert(pSeq); int i = 0; for (i = 0; i < pSeq->size; i++) { if (pSeq->array[i] == data) { return i; } } return -1; //注意!此处不能返回0,有歧义 } void Remove(PSeqList pSeq, DataType data) { SeqListErase(pSeq, Find(pSeq, data)); } void RemoveAll(PSeqList pSeq, DataType data) { //int pos = 0; //防止Find(pSeq, data)调用两次,代码如下注释 //while (-1 != (pos = Find(pSeq, data))) //{ // SeqListErase(pSeq, pos); //} /* while ((pos = Find(pSeq, data) != -1)) { SeqListErase(pSeq, Find(pSeq, data)); } */ //以上方法时间复杂度为0(N^2),太大,以下代码为改进代码,时间复杂度为O(N) //方法为:创建一个新空间,把不删除的元素放到新空间,再把需要删除的元素删除掉,最后把新空间的里元素在挪回顺序表 //但是这样的方法空间复杂度从O(1)变为O(N) /*DataType* pTemp = (DataType*)malloc(sizeof(DataType)*pSeq->size); int i = 0; int count = 0; for (i = 0; i < pSeq->size; i++) { if (pSeq->array[i] != data) { pTemp[count++] = pSeq->array[i]; } } memcpy(pSeq->array, pTemp, sizeof(DataType)*count); pSeq->size = count; free(pTemp);*/ //这个代码的时间复杂度为O(N),空间复杂度为O(1),较优 int i = 0; int count = 0; //count用来记录要删除的data出现了几次 for (i = 0; i < pSeq->size; i++) { if (pSeq->array[i] != data) { pSeq->array[i - count] = pSeq->array[i]; } else { count++; } } pSeq->size -= count; } void BubbleSort(PSeqList pSeq) { assert(pSeq); int i = 0; int j = 0; int flag = 0; for (i = 0; i < pSeq->size - 1; i++) { flag = 0; for (j = 0; j < pSeq->size - i - 1; j++) { if (pSeq->array[j] > pSeq->array[j + 1]) { DataType temp = pSeq->array[j]; pSeq->array[j] = pSeq->array[j + 1]; pSeq->array[j + 1] = temp; flag = 1; } } if (!flag) { return; } } } void SelectSort(PSeqList pSeq) { assert(pSeq); int i = 0; int j = 0; int MaxPos = 0; for (i = 0; i < pSeq->size - 1; i++) { MaxPos = 0; for (j = 1; j < pSeq->size - i; j++) { if (pSeq->array[j] > pSeq->array[MaxPos]) { MaxPos = j; } /* if (com(pSeq->array[j], pSeq->array[MaxPos])) { MaxPos = j; }*/ DataType temp = pSeq->array[MaxPos]; pSeq->array[MaxPos] = pSeq->array[pSeq->size - i - 1]; pSeq->array[pSeq->size - i - 1] = temp; } } } int BinarySearch(PSeqList pSeq, DataType data) { assert(pSeq); int left = 0; int right = 0; int mid = 0; right = pSeq->size - 1; while (left <= right) { mid = left + ((right - left) >> 1); if (pSeq->array[mid] == data) { return mid; } else if (pSeq->array[mid] > data) { right = mid - 1; } else { left = mid + 1; } } return -1; }
测试代码:
#define _CRT_SECURE_NO_WARNINGS 0 #include "seqlist.h" int main() { SeqList s; //Compare c; InitSeqList(&s); // 初始化顺序表 SeqListPushBack(&s, 1); // 在顺序表的尾部插入元素data SeqListPushBack(&s, 2); SeqListPushBack(&s, 3); SeqListPushBack(&s, 4); SeqListPushBack(&s, 5); PrintSeqList(&s); SeqListInsert(&s, 2, 6); // 在顺序表的pos位置插入元素data PrintSeqList(&s); SeqListErase(&s, 2); // 删除顺序表pos位置上的元素 PrintSeqList(&s); SeqListInsert(&s, 3, 4); // 在顺序表的pos位置插入元素data PrintSeqList(&s); RemoveAll(&s, 4); //删除顺序表中所有值为data的元素 PrintSeqList(&s); SeqListPopBack(&s); // 删除顺序表尾部的元素 PrintSeqList(&s); SeqListPushFront(&s, 0); // 在顺序表的头部插入元素data PrintSeqList(&s); SeqListPopFront(&s); // 删除顺序表头部元素 PrintSeqList(&s); int pos = Find(&s, 2); //查找顺序表中第一个值为data的元素 printf("pos = %d\n", pos); Remove(&s, 2);//删除顺序表中第一个值为data的元素 PrintSeqList(&s); SeqListPopBack(&s);// 删除顺序表尾部的元素 SeqListPopBack(&s); SeqListPushBack(&s, 10); // 在顺序表的尾部插入元素data SeqListPushBack(&s, 2); SeqListPushBack(&s, 4); SeqListPushBack(&s, 3); SeqListPushBack(&s, 1); SeqListPushBack(&s, 5); SeqListPushBack(&s, 7); SeqListPushBack(&s, 8); SeqListPushBack(&s, 9); SeqListPushBack(&s, 6); PrintSeqList(&s); // BubbleSort(&s); //对顺序表进行冒泡排序操作 SelectSort(&s); //对顺序表进行选择排序操作 PrintSeqList(&s); pos = BinarySearch(&s, 8); printf("pos = %d\n", pos); system("pause"); return 0; }
结果如下: