C language dynamic sequence table

Source: Internet
Author: User

C language dynamic sequence table

# Ifndef _ SEQLIST_D _ // header file # define _ SEQLIST_D __# define CAPICITY 4 typedef int DataType; typedef struct SeqList_D {DataType * _ Array; size_t _ size; size_t _ capicity;} SeqL, * pSeqL; void InitSeqList (pSeqL seq); void PrintfSeqList (pSeqL seq); void PushBack (pSeqL seq, DataType x ); void PopBack (pSeqL seq); void BubbleSort (pSeqL seq); void SelectSort (pSeqL seq); int BinarySearch (pSeqL seq, DataType x); void AddCapi City (logical QL * seq); # endif # include <stdio. h> // function file # include <assert. h> # include "SeqList_D.h" # include <malloc. h> void InitSeqList (pSeqL seq) {assert (seq); seq-> _ Array = (DataType *) malloc (sizeof (DataType) * CAPICITY ); seq-> _ size = 0; seq-> _ capicity = CAPICITY;} void PrintfSeqList (sequence QL seq) {size_t index = 0; assert (seq ); if (seq-> _ size = 0) printf ("the sequence table is empty! \ N "); for (; index <seq-> _ size; ++ index) {printf (" % d ", seq-> _ Array [index]);} printf ("\ n");} // void PushBack (pSeqL seq, DataType x) {assert (seq); AddCapicity (& seq ); seq-> _ Array [(seq-> _ size) + +] = x;} // Delete void PopBack (semi QL seq) {assert (seq ); if (seq-> _ size = 0) printf ("the sequence table is empty! \ N "); else -- seq-> _ size;} // bubble sort _ ascending void BubbleSort (sort QL seq) {size_t I = 0, j = 0; // cyclic variable assert (seq); for (; I <seq-> _ size; ++ I) {int count = 0; // counter for (j = 0; j <seq-> _ size-1-I; ++ j) {if (seq-> _ Array [j]> seq-> _ Array [j + 1]) {DataType tmp = seq-> _ Array [j]; seq-> _ Array [j] = seq-> _ Array [j + 1]; seq-> _ Array [j + 1] = tmp; ++ count ;}} if (count <= 1) return ;}} // select sort _ void SelectSort (sort QL seq) {size_t I = 0, j = 0, min = 0, max = seq-> _ size-1; // The cyclic variable for (; I <seq-> _ size-1; ++ I) {for (j = I + 1; j <seq-> _ size-I; ++ j) {if (seq-> _ Array [min] <seq-> _ Array [max]) // The End Of The switch header {DataType tmp = seq-> _ Array [min]; seq-> _ Array [min] = seq-> _ Array [max]; seq-> _ Array [max] = tmp;} if (seq-> _ Array [min] <seq-> _ Array [j]) // In the switch header, {DataType tmp = seq-> _ Array [min]; seq-> _ Array [min] = seq-> _ Array [j]; seq-> _ Array [j] = tmp;} if (seq-> _ Array [j] <seq-> _ Array [max]) // switch end {DataType tmp = seq-> _ Array [max]; seq-> _ Array [max] = seq-> _ Array [j]; seq-> _ Array [j] = tmp;} PrintfSeqList (seq);} max --; min ++; if (max <= min) break ;}} // Binary Search int BinarySearch (pSeqL seq, DataType x) {size_t lift = 0, right = seq-> _ size-1; while (lift <right) {if (seq-> _ Array [(lift + right)/2]> x) lift = (lift + right)/2 + 1; if (seq-> _ Array [(lift + right)/2] <x) right = (lift + right)/2; if (seq-> _ Array [(lift + right)/2] = x) return (lift + right)/2;} return-1 ;} // Add void AddCapicity (pSeqL * seq) {assert (seq); assert (* seq); if (* seq)-> _ capicity = (* seq) -> _ size) {(* seq)-> _ Array = realloc (* seq)-> _ Array, sizeof (DataType) * (* seq) -> _ capicity) * 2); // use the realloc function to Increase the capacity (* seq)-> _ capicity * = 2; size_t index = 0 ;}# include <stdio. h> // main function test function # include "SeqList_D.h" void test1 () {SeqL seq; InitSeqList (& seq); PushBack (& seq, 1); PushBack (& seq, 4); PushBack (& seq, 3); PushBack (& seq, 2); PrintfSeqList (& seq); PushBack (& seq, 5); PrintfSeqList (& seq ); popBack (& seq ); printfSeqList (& seq);} void test2 () {SeqL seq; InitSeqList (& seq); PushBack (& seq, 1); PushBack (& seq, 4 ); pushBack (& seq, 3); PushBack (& seq, 2); PushBack (& seq, 9); PushBack (& seq, 6); PushBack (& seq, 7 ); pushBack (& seq, 5); PrintfSeqList (& seq); BubbleSort (& seq); PrintfSeqList (& seq); SelectSort (& seq ); printfSeqList (& seq); printf ("% d \ n", BinarySearch (& seq, 2) ;}int main () {test2 (); 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.