Basic operations on static sequence tables in c Language

Source: Internet
Author: User

Basic operations on static sequence tables in c Language

# Include <stdio. h> // header file # 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 * se Q, size_t pos, DataType x); // return-1 indicates that no data int Find (SeqList * seq, DataType x) is found; 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> // function file # include <assert. h> # include <string. h> # include "SeqList. h "void InitSeqList (SeqList * seq) // The default initialization value is 0. {assert (seq); memset (seq, 0, MIX_SIZE); seq-> size = 0;} void PrintSeqList (SeqList * seq) // traverse the sequence table {assert (seq); size_t index = 0; if (seq-> size = 0) {printf ("the sequence table is empty! \ N "); return ;}for (; index <seq-> size; ++ index) {printf (" % d \ n ", seq-> seqList [index]);} void PushBack (SeqList * seq, DataType x) // end inserts {assert (seq); if (seq-> size> MIX_SIZE) seq-> size = 0; if (seq-> size = MIX_SIZE) {printf ("the sequence table is full! \ N "); return;} seq-> seqList [(seq-> size) ++] = x;} void PopBack (SeqList * seq) // end deletion {assert (seq); if (seq-> size = 0) {printf ("the sequence table is empty! \ N "); return;} -- (seq-> size);} void PushFront (SeqList * seq, DataType x) // Insert the header {int end; assert (seq ); if (seq-> size> MIX_SIZE) seq-> size = 0; if (seq-> size = MIX_SIZE) {printf ("the sequence table is full! \ N "); return;} end = (seq-> size) ++; for (; end> 0; -- end) {seq-> seqList [end] = seq-> seqList [end-1];} seq-> seqList [0] = x;} void PopFront (SeqList * seq) // header deletion {size_t start = 0; assert (seq); if (seq-> size = 0) {printf ("the sequence table is empty! \ 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 ("invalid Insert Location! \ N "); return;} if (seq-> size = MIX_SIZE) {printf (" the sequence table is full! \ 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) // Delete {size_t start = 0; assert (seq); if (seq-> size = 0) {printf ("the sequence table is empty! \ 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 ("the sequence table is empty! \ 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 ("this element is not found! \ N ");} void RemoveAll (SeqList * seq, DataType x) {size_t pos = 0, start = 0; assert (seq ); if (seq-> size = 0) {printf ("the sequence table is empty! \ 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 ("invalid Insert Location! \ N "); return;} seq-> seqList [pos-1] = x;} # include" SeqList. h "// main function file // void test1 (SeqList * seq) // {// PushFront (seq, 0); // PushFront (seq, 1 ); // PushFront (seq, 2); // PushFront (seq, 3); // PushFront (seq, 3); // PrintSeqList (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 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.