#include<stdio.h>//頭文件 #ifndef __SEQ_LIST__ #define __SEQ_LIST__ #define MIX_SIZE 5 typedef int DataType; typedef struct SeqList { DataType seqList[MIX_SIZE]; size_t size; }SeqList; void InitSeqList(SeqList* seq); void PrintSeqList(SeqList* seq); void PushBack(SeqList* seq, DataType x); void PopBack(SeqList* seq); void PushFront(SeqList* seq, DataType x); void PopFront(SeqList* seq); void Insert(SeqList* seq, size_t pos, DataType x); //// 返回-1表示未找到數據 int Find(SeqList* seq, DataType x); void Erase(SeqList* seq, size_t pos); void Remove(SeqList* seq, DataType x); void RemoveAll(SeqList* seq, DataType x); void Modify(SeqList* seq, size_t pos, DataType x); #endif #include<stdio.h>//函數文件 #include<assert.h> #include<string.h> #include"SeqList.h" void InitSeqList(SeqList* seq)//初始化 默認為零 { assert(seq); memset(seq, 0, MIX_SIZE); seq->size = 0; } void PrintSeqList(SeqList* seq)//遍歷順序表 { assert(seq); size_t index = 0; if (seq->size == 0) { printf("順序表為空!\n"); return; } for (; index < seq->size ; ++index) { printf("%d\n",seq->seqList[index]); } } void PushBack(SeqList* seq, DataType x)//尾插 { assert(seq); if (seq->size > MIX_SIZE) seq->size = 0; if (seq->size == MIX_SIZE) { printf("順序表已滿!\n"); return; } seq->seqList[(seq->size)++] = x; } void PopBack(SeqList* seq)//尾刪 { assert(seq); if (seq->size ==0) { printf("順序表已空!\n"); return; } --(seq->size); } void PushFront(SeqList* seq, DataType x)//頭插 { int end; assert(seq); if (seq->size > MIX_SIZE) seq->size = 0; if (seq->size == MIX_SIZE) { printf("順序表已滿!\n"); return; } end = (seq->size)++; for (; end > 0; --end) { seq->seqList[end] = seq->seqList[end - 1]; } seq->seqList[0] = x; } void PopFront(SeqList* seq)//頭刪 { size_t start = 0; assert(seq); if (seq->size == 0) { printf("順序表已空!\n"); return; } for (; start <seq->size; ++start) { seq->seqList[start] = seq->seqList[start+1]; } --seq->size; } void Insert(SeqList* seq, size_t pos, DataType x) { int end; assert(seq); if (seq->size > MIX_SIZE) seq->size = 0; if (pos > seq->size) { printf("不合法的插入位置!\n"); return; } if (seq->size == MIX_SIZE) { printf("順序表已滿!\n"); return; } end = (seq->size)++; for (; end >= pos-1; --end) { seq->seqList[end] = seq->seqList[end - 1]; } seq->seqList[pos-1] = x; } int Find(SeqList* seq, DataType x) { size_t pos = 0; assert(seq); for (; pos < seq->size; ++pos) { if (seq->seqList[pos] == x) return pos; } return -1; } void Erase(SeqList* seq, size_t pos)//按位置刪除 { size_t start = 0; assert(seq); if (seq->size == 0) { printf("順序表已空!\n"); return; } start = pos - 1; for (; start <seq->size; ++start) { seq->seqList[start] = seq->seqList[start + 1]; } --seq->size; } void Remove(SeqList* seq, DataType x) { int tag = 0; assert(seq); if (seq->size == 0) { printf("順序表為空!\n"); return; } size_t index = 0; for (; index < seq->size; ++index) { if (seq->seqList[index] == x) { for (; index < seq->size; ++index) { seq->seqList[index] = seq->seqList[index + 1]; } --seq->size; tag = 1; } } if (tag == 0) printf("未找到該元素!\n"); } void RemoveAll(SeqList* seq, DataType x) { size_t pos = 0,start=0; assert(seq); if (seq->size == 0) { printf("順序表為空!\n"); return; } for (; pos < seq->size; ++pos) { if (seq->seqList[pos] == x) { for (start = pos; start < seq->size; ++start) { seq->seqList[start] = seq->seqList[start + 1]; } --seq->size; --pos; } } } void Modify(SeqList* seq, size_t pos, DataType x) { assert(seq); if (pos > seq->size) { printf("不合法的插入位置!\n"); return; } seq->seqList[pos-1] = x; } #include"SeqList.h"//主函數文件 //void test1(SeqList * seq) //{ // PushFront(seq, 0); // PushFront(seq, 1); // PushFront(seq, 2); // PushFront(seq, 3); // PushFront(seq, 3); // PrintSeqList(seq); // PopFront(seq); // PopFront(seq); // PopFront(seq); // PrintSeqList(seq); // PopFront(seq); // PopFront(seq); // PrintSeqList(seq); //} void test2(SeqList * seq) { PushFront(seq, 0); PushFront(seq, 3); PushFront(seq, 3); PushFront(seq, 3); Insert(seq, 2, 4); PrintSeqList(seq); printf("%d\n",Find(seq, 5)); /*Modify(seq, 2, 5); Erase(seq, 2);*/ PrintSeqList(seq); RemoveAll(seq, 3); PrintSeqList(seq); } int main() { SeqList seq; /*test1(&seq);*/ test2(&seq); return 0; }