Static sequential tables and dynamic sequential tables

Source: Internet
Author: User

Implement a static order table, first of all, to define an array of data to be saved in the structure, with a size to store the number of elements in the array,

typedef struct SEQLIST{DATATYPE array[max_size];size_t SIZE;} Seqlist;

First of all to implement the static order table initialization function, you can borrow the system memset function to achieve, open a space all initialized to 0, no data stored so size is also 0

void Initseqlist (Seqlist *pseq) {assert (PSEQ); memset (pseq->array, 0, sizeof (DataType) *max_size);p seq->size = 0;}

Then simply implement the function of the tail plug, and pass the order table and the data that need to be inserted to the function

Void pushback (seqlist *pseq, datatype x) {    assert (PSEQ);//Assertion Order table     if  (pseq->size >= max_size)//Determine if the order table is full, the output of this order table is full and returns the program {printf ( "the seqlist is full.\n"); return;} pseq->array[pseq->size++] = x;//Insert the data that needs to be interpolated into the next element at the end of the order table} tail Delete function, simply pass the order table to the function, Void popback ( SEQLIST&NBSP;*PSEQ) {assert (PSEQ);//assert, develop good code habits, convenient for error checking work if  (Pseq->size array[pseq->size]  = 0;//the last element of the sequential table to 0,--pseq->size;//size minus one} to implement a simple header interpolation function Void pushfront (SEQLIST&NBSP;*PSEQ, &NBSP;DATATYPE&NBSP;X) {int begin = pseq->size;//The number of elements in the Save Order table assert (PSEQ);if  (pSeq-> Size >= max_size) {printf ("the seqlist is full.\n"); return;} for  (;  begin >= 0; --begin)//Moves the element from the end of the order table, emptying the position of the first element {pseq->array[begin]  =&NBSP;PSEQ-&GT;ARRAY[BEGIN&NBSP;-&NBSP;1];} pseq->array[0] = x;//inserts the first position into the element that needs to be inserted pseq->size++;//size plusA simple head-deletion function, similar in principle to head interpolation, moves the element from the first position, overwrites the first position, and void popfront the size by one (SEQLIST*&NBSP;PSEQ) {int begin =  0;assert (PSEQ);if  (pseq->size <= 0) {printf ("the seqlist is null"); return;} for  (;  begin size; begin++) {pseq->array[begin] = pseq->array[begin + &NBSP;1];} pseq->size--;} Implement a Lookup function, if found, return its subscript, if not found, return to -1int find (Seqlist* pseq, size_t pos, datatype  x) {Int i = 0;assert (PSEQ);for  (;  i < pseq->size; ++i) {if   (pseq->array[i] == x) {return i;}} Return -1;} Insert the function to move the element backward from the end of the order table until the POS position, empty the POS position into the element Void insert (seqlist* pseq, int pos,  datatype x) {Int begin = pseq->size-1;assert (PSEQ); assert (Pos size);if  (pos  >= max_size) {printf ("the seqlist is full"); return;} for  (;  begin >= pos; begin--) {PSEQ-&GT;array[begin+1] = pseq->array[begin];} pseq->array[pos] = x;pseq->size++;} Delete function Void erase (seqlist* pseq, size_t pos) {assert (PSEQ);if  (pseq->size<0) { printf ("the seqlist is empty\n"); return;} ASSERT (pos < pseq->size); int i = pos;//defines an i to save the current position for  (; i  < pseq->size; i++)//start from the current position backward, followed by the element to overwrite the previous element, to remove its role {pseq->array[i] = pseq-> ARRAY[I&NBSP;+&NBSP;1];} --(pseq->size);} Deletes the specified element Int remove (seqlist* pseq, datatype x) {Int pos;assert (PSEQ);if  (pSeq-> size size <= 0) {printf ("the seqlist is empty\n"); return;} Pos = find (pseq, 0, x);while  (pos != -1) {Erase (pseq, pos);p os =  find (pseq, pos,x);//Pass the Order table, the current position and the element to be deleted to the Find function, loop find Delete, until the element is removed}}//but the above method is not efficient, Not deleted once you need to move the elements all the way forward once, frequent moving causes the function to be relatively inefficient, count counting, the number of elements to be deleted before each element is calculated, the element is moved forward several bitsPlace Void removeall (seqlist* pseq, datatype x) {Int count = 0;int begin  = 0;assert (PSEQ);for  (;  begin < pseq->size; ++begin) {if  (pSeq- >array[begin] == x) {++count;} Else{pseq->array[begin-count] = pseq->array[begin];}} Pseq->size -= count;} Bubble sort function, reference array of bubble sort Void bubblesort (seqlist* pseq)//bubble Sort {assert (PSEQ); int i = 0;int  j = 0;for  (;  i < pseq->size;i++) {for  (j = 0; j  < pseq->size - i; j++) {if  (pseq->array[j] < pseq-> Array[j - 1]) {datatype temp;temp = pseq->array[j - 1];p seq->array[j-1 ] = pseq->array[j] ;p seq->array[j] = temp;}}} Select Sort function Void selectsort (seqlist* pseq) {assert (PSEQ);int i = 0;int j =  0;int min = 0; for  (j = 0; j < pseq->size - 1; ++j) {min =  j;for  (i = j + 1; i < pseq->size; ++i) {if  (pSeq- >array[i] < pseq->array[min]) {min = i;}} Swap (&pseq->array[min], &pseq->array[j]);}} But the above function is relatively inefficient, I implemented a select sorting function below, each cycle can find the maximum and minimum value, effectively reduce the number of cycles, the function efficiency Void selectsort_op (SEQLIST*&NBSP;PSEQ) {int  i = 0;int min = 0;int max = 0;int left = 0;int  right = pseq->size - 1;assert (PSEQ);while  (left < right) {min=  left;max = right;for  (i = left; i array[i] < pseq-> Array[min]) {Swap (&pseq->array[i], &pseq->array[left]);} if  (Pseq->array[i] > pseq->array[max]) {Swap (&pseq->array[i], &pseq- >array[right]);}} left++;right--;}}//below, I simply implemented a bit of binary search, be sure to pay attention to the loop condition Int binarysearch (seqlist* pseq, datatype x) {assert (PSEQ); int  left = 0;int right = pSeq->size - 1;while  (left <=  right) {int mid = left -  (left - right)  / 2;//avoids the problem of overflow if   (X < pseq->array[mid]) {right = mid;} else if  (X > pseq->array[mid]) {left = mid + 1;} Else{return mid;}} Return -1;} The following is a simple implementation of the various functions of the dynamic sequential table typedef struct seqlist{datatype* array;   //data block pointers size_t  size;       //Current number of valid data size_t capicity;   //capacity} seqlist;//this is the function of checking the capacity, not enough, the dynamic opening up space, with the help of the system's Relloc function void checkcapicity (seqlist* pseq) {if  (pseq-> size >= pseq->capicity) {pseq->capicity = 2 * pseq->capicity;pseq- >array =  (datatype *) realloc (pSeQ->array, pseq->capicity*sizeof (DataType));}} The implementation of other functions is roughly similar, but dynamically opens up space and requires checking capacity when it comes to element insertion and deletion

This article is from the "11423494" blog, please be sure to keep this source http://11433494.blog.51cto.com/11423494/1762475

Static sequential tables and dynamic sequential tables

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.