c 语言 静态顺序表 基本操作
程序员文章站
2022-05-03 17:52:17
#include//头文件
#ifndef __seq_list__
#define __seq_list__
#define mix_size...
#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; }
上一篇: 学习大数据必须了解的大数据开发课程大纲
下一篇: 队列之士兵报数