C Language Dynamic sequence table

Source: Internet
Author: User

#ifndef  __seqlist_d__//Header File #define __seqlist_d__#define capicity 4typedef int  datatype;typedef struct seqlist_d{datatype* _array;size_t    _size;size_t     _capicity;} Seql,*pseql;void initseqlist (PSEQL&NBSP;SEQ); void printfseqlist (PSEQL&NBSP;SEQ); Void PushBack ( PSEQL&NBSP;SEQ,DATATYPE&NBSP;X); Void popback (PSEQL&NBSP;SEQ); Void bubblesort (PSEQL&NBSP;SEQ); void  selectsort (PSEQL&NBSP;SEQ); Int binarysearch (pseql seq, datatype x);void  Addcapicity (PSEQL*&NBSP;SEQ); #endif #include<stdio.h>//function file #include<assert.h> #include "SeqList_D.h" # Include<malloc.h>void initseqlist (PSEQL&NBSP;SEQ) {assert (seq);seq->_array =  (DataType* ) malloc (sizeof (DataType) *capicity); seq->_size = 0;seq->_capicity = capicity;} Void printfseqlist (PSEQL&NBSP;SEQ) {size_t index = 0;assert (seq);if  (seq->_size == 0) printf ("Order table is empty!") \ n ");for  (;  index < seq->_size; ++index) {printf ("%d  ", Seq->_array[index ]);} printf ("\ n");} Tail plug Void pushback (pseql seq, datatype x) {assert (SEQ); Addcapicity (&AMP;SEQ); seq->_array[(seq->_size) ++] = x;} Tail Delete void popback (pseql seq) {assert (seq);if  (seq->_size == 0) printf ("Order table is empty!") \ n "); else--seq->_size;} Bubble Sort __ Ascending void bubblesort (pseql seq) {size_t i=0,j=0;//loop variable assert (seq);for  (; i  < seq->_size; ++i) {int count = 0;//counter for  (j=0; j < seq- &GT;_SIZE&NBSP;-&NBSP;1&NBSP;-&NBSP;I;&NBSP;++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 __ Descending void selectsort (PSEQL&NBSP;SEQ) {size_t i = 0, j = 0,min=0,max=seq->_size-1;//loop variable for  (;  i < seq->_size - 1; ++i) {for  (j = i + 1; j &NBSP;&LT;&NBSP;SEQ-&GT;_SIZE-I;&NBSP;++J) {if  (Seq->_array[min] < seq->_array[max])// Swap head   Tail {datatype tmp = seq->_array[min];seq->_array[min] = seq->_array[ Max];seq->_array[max] = tmp;} if  (Seq->_array[min] < seq->_array[j])//switch head   in {datatype tmp = seq- >_array[min];seq->_array[min] = seq->_array[j];seq->_array[j] = tmp;} if  (Seq->_array[j] < seq->_array[max])//exchange in   tail {datatype tmp = seq- >_array[max];seq->_array[max] = seq->_array[j];seq->_array[j] = tmp;} Printfseqlist (seq);} max--;min++;if  (max <= min) break;}} Two-point lookup Int binarysearch (PSEQL&NBSP;SEQ,&NBSP;DATATYPE&NBSP;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;} Increment void addcapicity (pseql* seq) {assert (seq), assert (*SEQ);if  ((*SEQ)->_capicity ==   (*SEQ)->_size) {(*SEQ)->_array =realloc ((*SEQ)->_array,sizeof (DataType) * ((*SEQ)->_ capicity);//increase capacity with realloc function (*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 (&NBSP;&AMP;SEQ,&NBSP;&NBSP;2); Printfseqlist (&AMP;SEQ); Pushback ( &seq,  5); Printfseqlist (&AMP;SEQ); Popback (&NBSP;&AMP;SEQ); Popback (&NBSP;&AMP;SEQ); Popback (&NBSP;&AMP;SEQ); Popback (&NBSP;&AMP;SEQ); Popback (&AMP;SEQ); Popback (&NBSP;&AMP;SEQ); Printfseqlist (&AMP;SEQ);} Void test2 () {seql seq;initseqlist (&AMP;SEQ); Pushback (&seq, 1); Pushback (&seq, 4); Pushback (&seq, 3); Pushback (&AMP;SEQ,&NBSP;2); Pushback (&seq, 9); Pushback (&AMP;SEQ,&NBSP;6); Pushback (&AMP;SEQ,&NBSP;7); Pushback (&seq, 5); Printfseqlist (&AMP;SEQ); Printfseqlist (&AMP;SEQ);     bubblesort (&AMP;SEQ); Printfseqlist (&AMP;SEQ); Selectsort (&AMP;SEQ); Printfseqlist (&AMP;SEQ);p rintf ("%d\n", BinarySearch (&AMP;SEQ,&NBSP;2));} Int  main () {test2 (); return 0;}


C Language Dynamic sequence table

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.