标签:顺序表的实现
seqlist.h
#ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include<stdio.h> #include<stdlib.h> #include<assert.h> #define INIT_SIZE 2 #define ADD_SIZE 3 typedef int DataType; typedef struct Seqlist { DataType *data; int size; //当前空间存储的元素个数 int capacity; //当前空间所能存储的最大容量 }Seqlist,*pSeqlist; void InitSeqlist(pSeqlist pSeq); void DestorySeqlist(pSeqlist pSeq); void PushBack(pSeqlist pSeq,DataType x); void PopBack(pSeqlist pSeq); void PushFront(pSeqlist pSeq,DataType x); void PopFront(pSeqlist pSeq); void Remove(pSeqlist pSeq,DataType x); void RemoveAll(pSeqlist pSeq,DataType x); void BubbleSort(pSeqlist pSeq); void InsertSort(pSeqlist pSeq); void SelectSort(pSeqlist pSeq); int BinarySearch(pSeqlist pSeq,DataType x); void Erase(pSeqlist pSeq,int pos); void PrintSeqlist(pSeqlist pSeq); #endif //SEQLIST_D_H__
seqist.c
#include"seqlist.h" void InitSeqlist(pSeqlist pSeq) { pSeq->data = (DataType *)malloc(INIT_SIZE*sizeof(DataType)); if (pSeq->data == NULL) { printf("out of memory\n"); exit(1); } pSeq->size = 0; pSeq->capacity = INIT_SIZE; //将容量置为当前空间所能存储的最大值 } void DestorySeqlist(pSeqlist pSeq) { free(pSeq->data); pSeq->data = NULL; pSeq->size = 0; pSeq->capacity = 0; } void CheckCapacity(pSeqlist pSeq) //查看当前空间是否已满 { assert(pSeq); if (pSeq->size == pSeq->capacity) //如果满了则进行扩容 { DataType *tmp = NULL; tmp = (DataType *)realloc(pSeq->data, (pSeq->capacity += ADD_SIZE)*sizeof(DataType)); if (NULL == tmp) { printf("out of memory\n"); exit(1); } pSeq->data = tmp; } } void PushBack(pSeqlist pSeq, DataType x) { assert(pSeq); CheckCapacity(pSeq); //只要插入元素,首先就要检查空间是否以满 pSeq->data[pSeq->size++] = x; //插入元素后size也要变化 } void PopBack(pSeqlist pSeq) { assert(pSeq); if (pSeq->size == 0) //异常情况,表已空 { printf("表已空\n"); return; } pSeq->size--; } void PushFront(pSeqlist pSeq, DataType x) { int i = 0; assert(pSeq); CheckCapacity(pSeq); //只要插入元素,首先就要检查空间是否以满 for (i = pSeq->size; i > 0; i--) //从后往前先将数据移动 { pSeq->data[i] = pSeq->data[i-1]; } pSeq->data[0] = x; pSeq->size++; } void PopFront(pSeqlist pSeq) { int i = 0; assert(pSeq); if (pSeq->size == 0) //异常情况,表空 { printf("表已空\n"); return; } for (i = 0; i < pSeq->size-1; i++) //直接从第二个元素依次向前覆盖 { pSeq->data[i] = pSeq->data[i + 1]; } pSeq->size--; } void Remove(pSeqlist pSeq, DataType x) //删除第一个出现的x { int i = 0; int j = 0; assert(pSeq); for (i = 0; i < pSeq->size; i++) { if (pSeq->data[i] == x) { for (j = i; j < pSeq->size-1; j++) //删除的时候从这个元素的后面向前覆盖 { pSeq->data[j] = pSeq->data[j + 1]; } pSeq->size--; return; } } } void RemoveAll(pSeqlist pSeq, DataType x) { int i = 0; int j = 0; assert(pSeq); for (i = 0; i < pSeq->size; i++) { if (pSeq->data[i] == x) { for (j = i; j < pSeq->size - 1; j++) //删除的时候从这个元素的后面向前覆盖 { pSeq->data[j] = pSeq->data[j + 1]; } pSeq->size--; } } } void BubbleSort(pSeqlist pSeq) { int flag = 0; int i = 0; int j = 0; int k = pSeq->size-1; assert(pSeq); for (i = 0; i < pSeq->size - 1; i--) { int m = 0; flag = 1; //将标记置1 for (j = 0; j < k; j++) { if (pSeq->data[j]>pSeq->data[j + 1]) { DataType tmp = pSeq->data[j]; pSeq->data[j] = pSeq->data[j + 1]; pSeq->data[j + 1] = tmp; flag = 0; m = j; //记录最后一次交换的位置 } } if (flag) //标记位1表示已经有序 { return; } m = k; //将k设置成最后一次交换的位置 } } void InsertSort(pSeqlist pSeq) //插入排序 { int i = 0; int j = 0; assert(pSeq); for (i = 1; i < pSeq->size; i++) { DataType tmp = pSeq->data[i]; for (j = i-1; j >=0; j--) { if (pSeq->data[j]>tmp) { pSeq->data[j+1] = pSeq->data[j]; } else { break; } } pSeq->data[j+1] = tmp; } } void SelectSort(pSeqlist pSeq) { int i = 0; int j = 0; int k = 0; assert(pSeq); for (i = 0; i < pSeq->size; i++) { k = i; for (j = i + 1; j < pSeq->size; j++) { if (pSeq->data[k]>pSeq->data[j]) { k = j; } } if (k != i) { DataType tmp = pSeq->data[k]; pSeq->data[k] = pSeq->data[i]; pSeq->data[i] = tmp; } } } int BinarySearch(pSeqlist pSeq, DataType x) { int left = 0; int right = pSeq->size - 1; int mid=0; assert(pSeq); mid = (left&right) + ((left^right) >> 1); //求平均值 while (left <= right) { if (pSeq->data[mid]>x) { right = mid - 1; } else if (pSeq->data[mid] < x) { left = mid + 1; } else { return mid; } } return -1; } void Erase(pSeqlist pSeq, int pos) { int i = 0; assert(pSeq); if (pos>= pSeq->size&&pos < 0) { printf("删除位置不合法\n"); return; } for (i = pos; i < pSeq->size - 1; i++) //从pos之后依次向前覆盖 { pSeq->data[i] = pSeq->data[i + 1]; } pSeq->size--; } void PrintSeqlist(pSeqlist pSeq) { int i = 0; assert(pSeq); if (pSeq->size == 0) { printf("表为空\n"); return; } for (i = 0; i < pSeq->size; i++) { printf("%d ", pSeq->data[i]); } printf("\n"); }
test.c
#include"seqlist.h" void menu() { printf("0.exit 1.PrintSeqlist \n"); printf("2.InitSeqlist 3.PushBack \n"); printf("4.Popback 5.PushFornt \n"); printf("6.PopFornt 7.Erase \n"); printf("8.Remove 9.RemoveAll \n"); printf("10.BubbleSort 11.BinarySearch\n"); printf("12.DestorySeqlist 13.InsertSort \n"); printf("14.SelectSort \n"); printf("请输入:>"); } void test(pSeqlist pSeq) { DataType x = 0; int n = 0; int pos = 0; int ret=0; while (1) { menu(); scanf("%d", &n); switch (n) { case 0: exit(1); break; case 1: PrintSeqlist(pSeq); break; case 2: InitSeqlist(pSeq); break; case 3: printf("请输入元素\n"); scanf("%d", &x); PushBack(pSeq, x); break; case 4: PopBack(pSeq); break; case 5: printf("请输入元素\n"); scanf("%d", &x); PushFront(pSeq, x); break; case 6: PopFront(pSeq); break; case 7: printf("请输入删除位置\n"); scanf("%d", &pos); Erase(pSeq, pos); break; case 8: printf("请输入元素\n"); scanf("%d", &x); Remove(pSeq, x); break; case 9: printf("请输入元素\n"); scanf("%d", &x); RemoveAll(pSeq, x); break; case 10: BubbleSort(pSeq); break; case 11: printf("请输入元素\n"); scanf("%d", &x); ret=BinarySearch(pSeq, x); if (ret == -1) printf("没有这个元素\n"); printf("下标为:%d\n", ret); break; case 12: DestorySeqlist(pSeq); break; case 13: InsertSort(pSeq); break; case 14: SelectSort(pSeq); break; default: printf("输入无效\n"); break; } } } int main() { Seqlist pSeq; test(&pSeq); system("pause"); return 0; }
本文出自 “10917138” 博客,请务必保留此出处http://10927138.blog.51cto.com/10917138/1786189
标签:顺序表的实现
原文地址:http://10927138.blog.51cto.com/10917138/1786189