标签:静态链表
静态链表的增删插改,注意方法中的边界检查和参数有效性检查。测试用例应全面!!!
#include<stdio.h> #include<stdlib.h> #include<assert.h> #define MAX_SIZE 10 typedef int DataType; typedef struct SeqList { DataType _array[MAX_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);//在指定位置插入x // 返回-1表示未找到数据 int Find(SeqList* seq, DataType x);//寻找x的下标 void Erase(SeqList* seq, size_t pos);//删除pos位置的置 void Remove(SeqList* seq, DataType x);//从头遍历,删除第一个x void RemoveAll(SeqList* seq, DataType x);//删除所有的x void Modify(SeqList* seq, size_t pos, DataType x);//修改指定位置的数为x void test(void);//测试用例 void InitSeqList(SeqList* seq) { assert(seq);//测试指针有效性 memset(seq->_array, 0, sizeof(DataType)*MAX_SIZE); seq->_size = 0; } void PrintSeqList(SeqList* seq) { size_t index = 0; assert(seq); for (; index < seq->_size; ++index) { printf("%d ", seq->_array[index]); } printf("\n"); } void PushBack(SeqList* seq, DataType x) { assert(seq); if (seq->_size >= MAX_SIZE) { printf("seqList is full\n"); return; } seq->_array[seq->_size++] = x; } void PopBack(SeqList* seq) { assert(seq); if (seq->_size == 0) { printf("seqList is empty\n"); return; } seq->_size--; } void PushFront(SeqList* seq, DataType x) { size_t index = seq->_size; assert(seq); if (seq->_size >= MAX_SIZE) { printf("seqList is full\n"); return; } for (; index > 0; index--) { seq->_array[index] = seq->_array[index - 1]; } seq->_array[0] = x; seq->_size++; } void PopFront(SeqList* seq) { size_t index = 0; assert(seq); if (seq->_size == 0) { printf("seqList is empty\n"); return; } for (; index<seq->_size - 1; index++) { seq->_array[index] = seq->_array[index + 1]; } seq->_size--; } void Insert(SeqList* seq, size_t pos, DataType x) { size_t index = seq->_size; assert(seq); assert(pos <= seq->_size);//测试位置是否超出范围 for (; index >pos; index--) { seq->_array[index] = seq->_array[index - 1]; } seq->_array[pos] = x; seq->_size++; } // 返回-1表示未找到数据 int Find(SeqList* seq, DataType x) { size_t find_index = 0; assert(seq); for (; find_index < seq->_size; find_index++) { if (seq->_array[find_index] == x) return find_index; } return -1; } void Erase(SeqList* seq, size_t pos) { size_t index = 0; assert(seq); assert(pos <= seq->_size); for (index = pos; index < seq->_size - 1; index++) { seq->_array[index] = seq->_array[index + 1]; } seq->_size--; } void Remove(SeqList* seq, DataType x) { size_t find_index = 0; size_t index = 0; assert(seq); for (; find_index < seq->_size; find_index++) { if (seq->_array[find_index] == x) { for (index = find_index; index < seq->_size - 1; index++) { seq->_array[index] = seq->_array[index + 1]; } seq->_size--; break; } } if (find_index == seq->_size) { printf("no search\n"); } } void RemoveAll(SeqList* seq, DataType x) { size_t index = 0; size_t dx = 0; size_t count = 0; assert(seq); /*for (; dx < seq->_size;dx++)//法一 { if (seq->_array[dx] == x) { for (index =dx; index < seq->_size - 1; index++) { seq->_array[index] = seq->_array[index + 1]; } count++; dx--; } }*/ for (; index < seq->_size;)//法二:简单方法:边拷贝边比较 { if (seq->_array[index] == x) { index++; count++; } else { seq->_array[dx++] = seq->_array[index++]; } } seq->_size -= count; } void Modify(SeqList* seq, size_t pos, DataType x) { assert(seq); assert(pos <= seq->_size); seq->_array[pos] = x; } void test() { SeqList seq; size_t index = 0; InitSeqList(&seq);//初始化 PushBack(&seq, 1);//尾插 PushBack(&seq, 2); PushBack(&seq, 3); PrintSeqList(&seq); PopBack(&seq);//尾删 PrintSeqList(&seq); PushFront(&seq, 4);//头插 PushFront(&seq, 5); PushFront(&seq, 6); PrintSeqList(&seq); PopFront(&seq);//头删 PrintSeqList(&seq); Insert(&seq, 0, 7);//在下标0位置插入7 PrintSeqList(&seq); index = Find(&seq, 4);//找数字4的下标 printf("4的下标为:%d\n删除后\n", index); Erase(&seq, index);//删除找到的下标位置的数 PrintSeqList(&seq); Remove(&seq, 1);//删掉第一个1 PrintSeqList(&seq); PushBack(&seq, 2);//尾插2 PushBack(&seq, 3); PushBack(&seq, 2); PushBack(&seq, 10); PushBack(&seq, 8); PushBack(&seq, 2); PushBack(&seq, 10); PrintSeqList(&seq); RemoveAll(&seq, 2);//删除所有的2 PrintSeqList(&seq); Modify(&seq, 0, 9);//将下标1的数改为9 PrintSeqList(&seq); } int main() { test(); system("pause"); return 0; }
本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1703847
标签:静态链表
原文地址:http://10541556.blog.51cto.com/10531556/1703847