C language-dynamic sequence table
A sequence table is a linear table stored in the computer memory as an array. It stores the linear structure of data elements in sequence with a set of sequential storage units. Linear tables are stored in sequence. An ordered table stores nodes in a group of sequential address storage units in computer memory. Static sequence table and dynamic sequence table .. H file
# Pragma once # include <string. h> # include <assert. h> # include <malloc. h> typedef int DataType; typedef struct SeqList // define a struct {DataType * arry; size_t size; size_t capacity;} SeqList; void InitSeqList (SeqList * Seq ); // initialize the struct void PrintSeqList (SeqList * Seq); // output function void PushBack (SeqList * Seq, DataType x); // Insert the data void PopBack (SeqList * Seq) behind the struct ); // Delete the following data void PushFrant (SeqList * Seq, DataType x); // Insert the data void PopFrant (SeqList * Seq ); // Delete the data int Find (SeqList * Seq, DataType x); // query the data void Erase (SeqList * Seq, size_t pos ); // clear the number of data void Remove (SeqList * Seq, DataType x); // Delete the first data xvoid Removeall (SeqList * Seq, DataType x ); // Delete All xvoid firstRemoveall (SeqList * Seq, DataType x); // Delete the second method void Modify (SeqList * Seq, size_t pos, DataType x) of all x ); // change the pos data to xvoid Insert (SeqList * Seq, size_t pos, DataType x); // Insert the pos data into xvoid check (SeqList * Seq ); // determine whether the pointer is valid and dynamically open the memory void check (SeqList * Seq) {assert (Seq); if (Seq-> size> = Seq-> capacity) {DataType * tmp; Seq-> capacity * = 2; tmp = (DataType *) malloc (sizeof (DataType) * (Seq-> capacity); memcpy (tmp, seq-> arry, sizeof (DataType) * (Seq-> size); free (Seq-> arry); Seq-> arry = tmp ;}} void InitSeqList (SeqList * Seq) {assert (Seq); Seq-> capacity = 2; Seq-> arry = (DataType *) malloc (sizeof (DataType) * Seq-> capacity); Seq-> size = 0;} void PrintSeqList (SeqList * Seq) {int I = 0; for (I = 0; I <(int) seq-> size; I ++) {printf ("% d", Seq-> arry [I]) ;}} void PushBack (SeqList * Seq, DataType x) {check (Seq); Seq-> arry [Seq-> size ++] = x;} void PopBack (SeqList * Seq) {assert (Seq ); -- Seq-> size;} void PushFrant (SeqList * Seq, DataType x) {check (Seq); int I = Seq-> size-1; (; I >=0; I --) {Seq-> arry [I + 1] = Seq-> arry [I];} Seq-> arry [0] = x; ++ Seq-> size;} void PopFrant (SeqList * Seq) {assert (Seq); int I = 0; for (; I <= (int) Seq-> size; I ++) {Seq-> arry [I] = Seq-> arry [I + 1];} -- Seq-> size;} void Remove (SeqList * Seq, dataType x) {assert (Seq); int I = 0; for (; I <(int) Seq-> size; I ++) {if (Seq-> arry [I] = x) {Seq-> arry [I] = Seq-> arry [I + 1] ;}} -- Seq-> size;} void Removeall (SeqList * Seq, DataType x) {assert (Seq); int firstIndex = 0, secondIndex = 0; int count = 0; while (secondIndex <= (int) Seq-> size) {if (Seq-> arry [secondIndex] = x) {count ++ ;} else {Seq-> arry [firstIndex] = Seq-> arry [secondIndex]; firstIndex ++;} secondIndex ++;} Seq-> size-= count ;} void firstRemoveall (SeqList * Seq, DataType x) {assert (Seq); int I = 0; for (; I <(int) Seq-> size; I ++) {if (Seq-> arry [I] = x) {int begin = I; for (; begin <(int) Seq-> size-1; begin ++) {Seq-> arry [begin] = Seq-> arry [begin + 1];} -- Seq-> size; I -- ;}} int Find (SeqList * Seq, dataType x) {assert (Seq); int I = 0; for (; I <(int) Seq-> size; I ++) {if (Seq-> arry [I] = x) {return Seq-> arry [I];} else {return-1 ;}} return 0 ;} void Erase (SeqList * Seq, size_t pos) {assert (Seq); int I = pos-1; for (; I <(int) Seq-> size-1; I ++) {Seq-> arry [I] = Seq-> arry [I + 1];} -- Seq-> size;} void Modify (SeqList * Seq, size_t pos, DataType x) {check (Seq); int I = 0; for (; I <(int) Seq-> size-1; I ++) {if (I = pos-1) {Seq-> arry [I] = pos ;}} void Insert (SeqList * Seq, size_t pos, DataType x) {check (Seq ); int I = Seq-> size-1; for (; I> = (int) pos; I --) {Seq-> arry [I + 1] = Seq-> arry [I];} Seq-> arry [pos] = x; Seq-> size ++ ;}
. C file
#include<stdio.h>#include"test.h"SeqList Seq;void test(){ PushBack(&Seq,1); PushBack(&Seq,2); PushBack(&Seq,3); PushBack(&Seq,4); PopBack(&Seq); PrintSeqList(&Seq);}void test1(){ PushFrant(&Seq, 1); PushFrant(&Seq, 2);// PopFrant(&Seq); PushFrant(&Seq, 4); PushFrant(&Seq, 3);// Remove(&Seq, 2);// Removeall(&Seq, 2);y// firstRemoveall(&Seq, 2);// Erase(&Seq, 1);// Modify(&Seq, 3, 2); Insert(&Seq, 4, 6); PrintSeqList(&Seq);}int main(void){ InitSeqList(&Seq); test1();}