在之前写的静态顺序表中,我们完成了基本的操作,包括初始化,头插,尾插,头删,尾删,任意位置上的插入,删除和输出的功能;在这里我们接着完善功能,本次将会添加指定数据的删除和排序功能。
新添功能:
1. 在顺序表中查找data元素
2. 判断链表是否为空
3. 删除顺序表中第一个值为data的元素
4. 删除顺序表中所有值为data的元素
5. 顺序表的冒泡排序和选择排序
1. 函数定义:
// 在顺序表中查找值为data的元素,返回该元素在顺序表中的下标
int SeqListFind(PSeqList pSeq, DataType data);
// 判断顺序表是否为空,为空返回true,否则返回false
int SeqListEmpty(PSeqList pSeq);
// 删除顺序表中第一个值为data的元素
void Remove(PSeqList pSeq, DataType data);
// 删除顺序表中所有值为data的元素
void RemoveAll(PSeqList pSeq, DataType data);
//冒泡排序
void BubbleSort(PSeqList pSeq);
//选择排序
void SelectSort(PSeqList pSeq);
2. 各部分功能实现
- 在顺序表中查找值为data的元素,返回该元素在顺序表中的下标
// 1.在顺序表中查找值为data的元素,返回该元素在顺序表中的下标
int SeqListFind(PSeqList pSeq, DataType data)
{
assert(pSeq);
int n = -1;
while (++n < pSeq->size)
{
if (pSeq->array[n] == data)
{
return n;
}
}
printf("This data non-existent\n");
return -1;
}
- 判断顺序表是否为空,为空返回true,否则返回false
// 2. 判断顺序表是否为空,为空返回true,否则返回false
int SeqListEmpty(PSeqList pSeq)
{
if (pSeq == NULL)
return 1;
else return 0;
}
- 删除顺序表中第一个值为data的元素
void SeqListErase(PSeqList pSeq, int pos)// 删除顺序表pos位置上的元素
{
assert(pSeq);
if (!(pSeq->size > MAX_SIZE) && pos >= 1 && pos <= pSeq->size)
{
for (int i = pos - 1; i < pSeq->size; i++)
{
pSeq->array[i] = pSeq->array[i + 1];
}
pSeq->size--;
}
else printf("The number of people exceeds!\n");
}
void Remove(PSeqList pSeq, DataType data)
{
SeqListErase(pSeq, SeqListFind(pSeq, data)+1);
}
- 删除顺序表中所有值为data的元素
// 4. 删除顺序表中所有值为data的元素
void RemoveAll(PSeqList pSeq, DataType data)
{
assert(pSeq);
int i = 0;
int count = 0;//记录删除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;
}
- 冒泡排序
// 5. 冒泡排序
void BubbleSort(PSeqList pSeq)
{
int flag = 0;
DataType ex = 0;
assert(pSeq);
for(int i=0; i< pSeq->size - 1; i++)
{
flag = 0;
for (int j = 0; j < pSeq->size - 1 - i; j++)
{
if (pSeq->array[j] > pSeq->array[j + 1])
{
flag = 1;
ex = pSeq->array[j + 1];
pSeq->array[j + 1] = pSeq->array[j ];
pSeq->array[j] = ex;
}
}
if (flag == 0)
{
return;
}
}
printf("BubbleSort completion\n");
}
- 选择排序
// 6. 选择排序
void SelectSort(PSeqList pSeq)
{
int flag = 0;
int minData = 0;
int min_ = 0;
assert(pSeq);
for (int i = 0; i < pSeq->size - 1; i++)
{
minData = pSeq->array[i];
min_ = i;
for (int j = i + 1; j < pSeq->size; j++)
{
if (pSeq->array[j] < minData)
{
minData = pSeq->array[j];
min_ = j;
}
}
if (min_ != i)
{
pSeq->array[min_] = pSeq->array[i];
pSeq->array[i] = minData;
}
}
}
代码
代码实现(Visual Studio 2017)
- test.c 文件
int main()
{
SeqList_test();
system("pause");
return 0;
}
- SeqList.h 文件
#pragma once
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
#define MAX_SIZE 10
typedef int DataType;
typedef struct SeqList// 静态顺序表
{
DataType array[MAX_SIZE];
int size; // 表示顺序表中有效元素的个数
}SeqList, *PSeqList;
////////////////////////////////////// 1.
void InitSeqList(PSeqList pSeq);// 初始化顺序表
void SeqListPushBack(PSeqList pSeq, DataType data);// 在顺序表的尾部插入元素data
void SeqListErase(PSeqList pSeq, int pos);// 删除顺序表pos位置上的元素
void SeqListPrintf(PSeqList pSeq);//输出整个静态表
/////////////////////////////////////// 2.
// 在顺序表中查找值为data的元素,返回该元素在顺序表中的下标
int SeqListFind(PSeqList pSeq, DataType data);
// 判断顺序表是否为空,为空返回true,否则返回false
int SeqListEmpty(PSeqList pSeq);
// 删除顺序表中第一个值为data的元素
void Remove(PSeqList pSeq, DataType data);
// 删除顺序表中所有值为data的元素
void RemoveAll(PSeqList pSeq, DataType data);
//冒泡排序
void BubbleSort(PSeqList pSeq);
//选择排序
void SelectSort(PSeqList pSeq);
#endif // !_SEQLIST_H_
- SeqList.c 文件
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include "SeqList.h"
#pragma warning (disable : 4996)
void InitSeqList(PSeqList pSeq)//初始化
{
assert(pSeq);
if (!(pSeq->size > 10))
{
for (int i = 0; i < MAX_SIZE; i++)
{
pSeq->array[i] = 0;
}
pSeq->size = 0;
}
else printf("The number of people exceeds!\n");
}
void SeqListPushBack(PSeqList pSeq, DataType data)//尾插
{
assert(pSeq);
if (pSeq->size < 10)
{
pSeq->array[pSeq->size++] = data;
}
else printf("The number of people exceeds!\n");
}
void SeqListPrintf(PSeqList pSeq)//输出整个静态
{
for (int i = 0; i < pSeq->size; i++)
{
printf("%d ", pSeq->array[i]);
}
printf("\n");
}
// 1.在顺序表中查找值为data的元素,返回该元素在顺序表中的下标
int SeqListFind(PSeqList pSeq, DataType data)
{
assert(pSeq);
int n = -1;
while (++n < pSeq->size)
{
if (pSeq->array[n] == data)
{
return n;
}
}
printf("This data non-existent\n");
return -1;
}
// 2. 判断顺序表是否为空,为空返回true,否则返回false
int SeqListEmpty(PSeqList pSeq)
{
if (pSeq == NULL)
return 1;
else return 0;
}
// 3. 删除顺序表中第一个值为data的元素
void SeqListErase(PSeqList pSeq, int pos)// 删除顺序表pos位置上的元素
{
assert(pSeq);
if (!(pSeq->size > MAX_SIZE) && pos >= 1 && pos <= pSeq->size)
{
for (int i = pos - 1; i < pSeq->size; i++)
{
pSeq->array[i] = pSeq->array[i + 1];
}
pSeq->size--;
}
else printf("The number of people exceeds!\n");
}
void Remove(PSeqList pSeq, DataType data)
{
SeqListErase(pSeq, SeqListFind(pSeq, data)+1);
}
// 4. 删除顺序表中所有值为data的元素
void RemoveAll(PSeqList pSeq, DataType data)
{
assert(pSeq);
int i = 0;
int count = 0;//记录删除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;
}
// 5. 冒泡排序
void BubbleSort(PSeqList pSeq)
{
int flag = 0;
DataType ex = 0;
assert(pSeq);
for(int i=0; i< pSeq->size - 1; i++)
{
flag = 0;
for (int j = 0; j < pSeq->size - 1 - i; j++)
{
if (pSeq->array[j] > pSeq->array[j + 1])
{
flag = 1;
ex = pSeq->array[j + 1];
pSeq->array[j + 1] = pSeq->array[j ];
pSeq->array[j] = ex;
}
}
if (flag == 0)
{
return;
}
}
printf("BubbleSort completion\n");
}
// 6. 选择排序
void SelectSort(PSeqList pSeq)
{
int flag = 0;
int minData = 0;
int min_ = 0;
assert(pSeq);
for (int i = 0; i < pSeq->size - 1; i++)
{
minData = pSeq->array[i];
min_ = i;
for (int j = i + 1; j < pSeq->size; j++)
{
if (pSeq->array[j] < minData)
{
minData = pSeq->array[j];
min_ = j;
}
}
if (min_ != i)
{
pSeq->array[min_] = pSeq->array[i];
pSeq->array[i] = minData;
}
}
}
void SeqList_mian(PSeqList pSeq)
{
InitSeqList(pSeq);
SeqListPushBack(pSeq, 2);
SeqListPushBack(pSeq, 1);
SeqListPushBack(pSeq, 3);
SeqListPushBack(pSeq, 0);
SeqListPushBack(pSeq, 5);
SeqListPushBack(pSeq, 7);
SeqListPushBack(pSeq, 3);
SeqListPushBack(pSeq, 9);
SeqListPushBack(pSeq, 8);
SeqListPushBack(pSeq, 3);
SeqListPrintf(pSeq);
//BubbleSort(pSeq);//冒泡排序
//SeqListPrintf(pSeq);
SelectSort(pSeq);//选择排序
SeqListPrintf(pSeq);
Remove(pSeq, 5);//删除data
SeqListPrintf(pSeq);
RemoveAll(pSeq, 3);//删除所有data
SeqListPrintf(pSeq);
}
void SeqList_test()
{
SeqList Seq = {0};
PSeqList pSeq = &Seq;
SeqList_mian(pSeq);
}