静态顺序表(2)
来源:互联网 发布:软件需求分析 编辑:程序博客网 时间:2024/06/05 04:50
在之前写的静态顺序表中,我们完成了基本的操作,包括初始化,头插,尾插,头删,尾删,任意位置上的插入,删除和输出的功能;在这里我们接着完善功能,本次将会添加指定数据的删除和排序功能。
新添功能:
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);}
阅读全文
0 0
- 静态顺序表(2)
- 顺序表(静态)
- 顺序表(静态)
- 数据结构:静态查找表(顺序表)
- 顺序表(静态开辟内存)
- 静态顺序表(C语言实现)
- 静态顺序表(C语言实现)
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- 简易静态顺序表
- 静态顺序表
- 静态顺序表
- 静态顺序表
- JavaWeb——Servlet学习日志1
- 关于Python图片着色后存储为“JPEG”格式存在明显色差的测试
- sysinfo函数使用方法
- Distributed feedback laser (DFB)
- oracle--6.多表查询
- 静态顺序表(2)
- Python基本数据结构:列表,元组及字典
- Salesforce首席科学家:拜师吴恩达,投身深度学习,新生代AI领袖
- 常用正则
- 本地存储动态请求的数据
- Caffe-GPU编译问题:nvcc fatal : Unsupported gpu architecture 'compute_20'
- 15
- Java 获取本周,本月的开始和结束日期
- hadoop学习---运行第一个hadoop实例