标签:数据结构 顺序表
静态顺序表
实现尾部插入、尾部删除、头部插入、头部删除、任意位置插入、任意位置删除、排序、查找、反转等功能
#ifndef __SEQ_TABLE_S_H__ #define __SEQ_TABLE_S_H__ #define MAX 100 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<assert.h> typedef int DataType; //创建结构体类型对象 typedef struct SeqList { DataType arr[MAX]; int size ; }SeqList,*pSeqList; void InitSeqList(pSeqList pSeq);//初始化 void PrintSeqList(SeqList Seq);//打印 void PushBack(pSeqList pSeq,DataType x);//尾插 void PopBack(pSeqList pSeq);//尾删 void PushFront(pSeqList pSeq, DataType x);//头插 void PopFront(pSeqList pSeq);//头删 void Insert(pSeqList pSeq, int pos, DataType x);//插入元素 void Remove(pSeqList pSeq, DataType x);//删除元素 void RemoveAll(pSeqList pSeq, DataType x);//删除所有指定元素 int Find(SeqList Seq,DataType x);//遍历查找 void ReverseList(pSeqList pSeq);//反转 void SortList(pSeqList pSeq);//排序 int BinarySearch(SeqList Seq,DataType x);//二分查找 void InitSeqList(pSeqList pSeq) { assert(pSeq); memset(pSeq->arr, 0, sizeof(pSeq->arr));//设置为全零 pSeq->size = 0; } void PrintSeqList(SeqList Seq) { int i = 0; for (i = 0; i < Seq.size; i++) { printf("%d ", Seq.arr[i]); } printf("over\n"); } void PushBack(pSeqList pSeq, DataType x)//尾插 { assert(pSeq); if (pSeq->size >= MAX) { printf("顺序表已满\n"); return; } // pSeq->arr[pSeq->size] = x; // pSeq->size++; pSeq->arr[pSeq->size++]=x; } void PopBack(pSeqList pSeq)//尾删 { assert(pSeq); if (pSeq->size == 0) { printf("顺序表已空\n"); return; } pSeq->size--; } void PushFront(pSeqList pSeq, DataType x)//头插 { assert(pSeq); int i = 0; if (pSeq->size >= MAX) { printf("顺序表已满\n"); return; } for (i = pSeq->size; i > 0; i--) { pSeq->arr[i] = pSeq->arr[i - 1]; } pSeq->arr[0] = x; pSeq->size++; } void PopFront(pSeqList pSeq)//头删 { assert(pSeq); int i = 0; if (pSeq->size == 0) { printf("顺序表已空\n"); return; } for (i = 0; i < pSeq->size; i++) { pSeq->arr[i] = pSeq->arr[i + 1]; } pSeq->size--; } void Insert(pSeqList pSeq, int pos, DataType x)//插入元素 { int i = 0; assert(pSeq); assert((pos < pSeq->size) && (pos >= 0)); if (pSeq->size == MAX) { printf("顺序表已满\n"); return; } for (i = pSeq->size; i>pos; i--) { pSeq->arr[i] = pSeq->arr[i - 1]; } pSeq->arr[pos] = x; pSeq->size++; } int Find(SeqList Seq, DataType x) { int i = 0; for (i = 0; i < Seq.size; i++) { if (x == Seq.arr[i]) { return i; } } return -1; } void Remove(pSeqList pSeq, DataType x)//删除元素 { int pos = Find(*pSeq, x); int i = 0; assert(pSeq); if (pos != -1) { for (i = pos; i < pSeq->size; i++) { pSeq->arr[i] = pSeq ->arr[i + 1]; } pSeq->size--; } } void RemoveAll(pSeqList pSeq, DataType x)//删除所有指定元素 { while (Find(*pSeq, x) != -1) { Remove(pSeq, x); } } void ReverseList(pSeqList pSeq)//反转 { int start = 0; int end = pSeq->size - 1; assert(pSeq); while (start < end) { DataType tmp = pSeq->arr[start]; pSeq->arr[start] = pSeq->arr[end]; pSeq->arr[end] = tmp; start++; end--; } } void SortList(pSeqList pSeq)//排序 { assert(pSeq); int i = 0; int j = 0; for (i = 0; i < pSeq->size-1; i++) { for (j = 0; j < pSeq->size - i - 1; j++) { if (pSeq->arr[j]>pSeq->arr[j + 1]) { DataType tmp = pSeq->arr[j]; pSeq->arr[j] = pSeq->arr[j + 1]; pSeq->arr[j + 1] = tmp; } } } } int BinarySearch(SeqList Seq, DataType x)//二分查找 { int left = 0; int right = Seq.size - 1; while (left <= right) { int mid = left - ((left - right) >> 1); if (Seq.arr[mid] > x) right = mid - 1; else if (Seq.arr[mid] == x) return mid; else left = mid + 1; } return -1; } #endif//__SEQ_TABLE_S_H__
本文出自 “无以伦比的暖阳” 博客,请务必保留此出处http://10797127.blog.51cto.com/10787127/1735822
标签:数据结构 顺序表
原文地址:http://10797127.blog.51cto.com/10787127/1735822