【C语言】静态顺序表

    xiaoxiao2021-03-25  38

    要实现的接口如下:

    #define _CRT_SECURE_NO_DEPRECATE #ifndef _SeqList__H__ #define _SeqList__H__ #include <stdio.h> #include <string.h> #include <assert.h> #define MAX 100 typedef int DataType; typedef struct SeqList { DataType arr[MAX]; int count; }SeqList, *pSeqList; void InitSeqList(pSeqList p);//初始化 void PushBack(pSeqList p, DataType d);//后插 void PopBack(pSeqList p);//后删 void PushFront(pSeqList p, DataType d);//前插 void PopFront(pSeqList p);//前删 int Find(pSeqList p, DataType d);//查找 void Remove(pSeqList p, DataType d);//移除一个结点 void RemoveAll(pSeqList p, DataType d);//移除多个相同的结点 void Show(pSeqList p);//打印 void Sqrt(pSeqList p);//排序 int BinarySearch(pSeqList p, DataType d);//二分法查找 #endif

    具体实现:

    #define _CRT_SECURE_NO_DEPRECATE #include "SeqList.h" void InitSeqList(pSeqList p) { p->count = 0; memset(p->arr, 0, MAX*sizeof(DataType)); } void PushBack(pSeqList p, DataType d) { assert(p != NULL); if (p->count == MAX) { printf("表已满"); return; } p->arr[p->count] = d; p->count++; printf("插入成功\n"); } void PopBack(pSeqList p) { assert(p); int i = 0; if (0 == p->count) { printf("表为空\n"); return; } p->count--; printf("删除成功\n"); } void PushFront(pSeqList p, DataType d) { assert(p); int i = 0; if (p->count == MAX) { printf("表已满"); return; } for (i = p->count; i > 0; i--) { p->arr[i] = p->arr[i - 1]; } p->arr[0] = d; p->count++; printf("插入成功\n"); } void PopFront(pSeqList p) { assert(p); int i = 0; if (0 == p->count) { printf("顺序表为空\n"); return; } for (i = 0; i < p->count - 1; i++) { p->arr[i] = p->arr[i + 1]; } p->count--; printf("删除成功\n"); } int Find(pSeqList p, DataType d) { assert(p); int i = 0; for (i = 0; i < p->count; i++) { if (d == p->arr[i]) { return i+1; } } return -1; } void Remove(pSeqList p, DataType d) { assert(p); DataType ret = Find(p, d); int i = 0; if (ret == -1) { printf("该列表中不存在该数\n"); } else { for (i = ret; i < p->count - 1; i++) { p->arr[i] = p->arr[i + 1]; } p->count--; printf("删除成功\n"); } } void RemoveAll(pSeqList p, DataType d) { assert(p); int i = 0; int j = 0; int count = 0; if (0 == p->count) { printf("顺序表为空\n"); return; } while (i<p->count) { if (d == p->arr[i]) { j = i; while (j<p->count) { p->arr[j] = p->arr[j + 1]; j++; } p->count--; } i++; } printf("删除成功\n"); } void Show(pSeqList p) { assert(p); int i = 0; if (0 == p->count) { printf("顺序表为空\n"); return; } for (i = 0; i < p->count; i++) { printf("%d ", p->arr[i]); } printf("\n"); } void Sqrt(pSeqList p) { assert(p); int i = 0; int j = 0; for (i = 0; i < p->count; i++) { for (j = i + 1; j < p->count; j++) { if (p->arr[i]>p->arr[j]) { DataType tmp = p->arr[i]; p->arr[i] = p->arr[j]; p->arr[j] = tmp; } } } printf("排序成功\n"); } int BinarySearch(pSeqList p, DataType d) { assert(p); int left = 0; int right = p->count - 1; while (left <= right) { int mid = left - ((left - right) >> 1); if (d < p->arr[mid]) { right = mid - 1; } else if (d>p->arr[mid]) { left = mid + 1; } else { return mid + 1; } } return -1; }

    转载请注明原文地址: https://ju.6miu.com/read-26105.html

    最新回复(0)