The sequential table is a basic structure in C, which can store a variety of basic types of data, and can store not only basic types of data, but also a structure. So the order table is a learning C in the process must grasp the structure, through the study of finishing, the following to achieve:
First of all, the first thing to use what to achieve, the general implementation of the most basic order table directly with the array implementation , we use a structure to encapsulate this sequential table (encapsulation of this concept is the most commonly used in C + + concepts)
#define Array_empty-2#define array_full-1#define max_size 10000typedef int datatype;typedef struct Seqlist{datatype arr ay[max_size];size_t SIZE;} Seqlist;
For the inability to determine the size of the array, we can use a macro to represent it.
After the general structure has been set up, you can think of a sequence table should have the function, head plug, tail plug, fixed-point insertion, sorting and so on are some of the most basic functions:
void Printseqlist (seqlist* pseq); void Initseqlist (seqlist* pseq); void pushback (seqlist* pseq, Datatype x); void Pushfront (seqlist* pseq, Datatype x), void Popback (seqlist* pseq), void Popfront (seqlist* pseq), void Insert (seqlist* pseq , size_t Pos, Datatype x), int Find (seqlist* pseq, size_t pos, Datatype x), void Erase (seqlist* pseq, size_t pos), int Remove ( seqlist* pseq, Datatype x); int RemoveAll (seqlist* pseq, Datatype x), void Bubblesort (seqlist* pseq); void Selectsort ( seqlist* pseq); int BinarySearch (seqlist* pseq, Datatype x);
Here are the implementation of some key functions:
Void printseqlist (SEQLIST*&NBSP;PSEQ)//output order Table {assert (PSEQ);size_t i = 0;if (pSeq-> size <= 0) {printf ("array is empty\n"); return;} else if (pseq->size >= max_size) {printf ("array is full\n");} for (; i < pseq->size; ++i) {printf ("%-4d", pseq->array[i]);}} Void initseqlist (SEQLIST*&NBSP;PSEQ)//initialization order Table {pseq->size = 0;//printf ("Initialization is complete\n ");} Void pushback (SEQLIST*&NBSP;PSEQ,&NBSP;DATATYPE&NBSP;X)//after inserting {assert (PSEQ);if (pseq->size >= max_size) {printf ("array is full, insert the failure\n"); return;} pseq->array[pseq->size] = x;pseq->size++;} Void pushfront (seqlist* pseq, datatype x)//before inserting {assert (PSEQ); size_t i = 0;if (pseq->size >= max_size) {printf ("array is full, insert the Failure\n ");return;} for (i = pseq->size; i > 0; --i) {pseq->array[i] = pseq- >ARRAY[I&NBSP;-&NBSP;1];} pseq->array[0] = x;pseq->size++;} Void popback (SEQLIST*&NBSP;PSEQ)//after deleting {assert (PSEQ);if (pseq->size <= 0) {printf (" Array is empty,popback is eeror\n "); return;} pseq->array[pseq->size-1] = 0;pseq->size--;} Void popfront (SEQLIST*&NBSP;PSEQ)//before deleting {assert (PSEQ);size_t i = 0;if (pSeq->size <= 0) {printf ("array is empty,popback is eeror\n"); return;} for (i = 1; i < pseq->size; i++) {pseq->array[i - 1] = pseq->array[i];} pseq->size--;} Void insert (seqlist* pseq, size_t pos, datatype x)//fixed-point Delete {assert (PSEQ); size_t i = 0;if (pseq->size >= max_size) {printf ("array is full, InserT the failure\n "); return;} for (i = pseq->size ; i > pos; i--) {pSeq->array[i] = &NBSP;PSEQ->ARRAY[I&NBSP;-&NBSP;1];} pseq->array[pos] = x;pseq->size++;} Int find (seqlist* pseq, size_t pos, datatype x)//Find data {assert (PSEQ); size_t i = 0;if (pseq->size <= 0) {printf ("array is empty,erase error\n") ; return array_empty;} if (pos < 0 | | pos >= max_size) {printf ("began to find the position error\n"); Return -1;} for (i = pos; i < pseq->size; i++) {if (pSeq->array[i] == x) {return i;}} Return -2;} Void erase (Seqlist* pseq, size_t pos)//delete specific data {assert (PSEQ); size_t i = 0;if (pseq->size <= 0) {printf ("array is empty,erase error\n"); return;} If (pos < 0 | | pos >= max_size) {printf ("began to find the position error\n"); return;} for (i = pos; i < pseq->size; i++) {pseq->array[i] = PSEQ->ARRAY[I&NBSP;+&NBSP;1];} pseq->size--;} Int remove (seqlist* pseq, datatype x) {assert (PSEQ);size_t i = 0;if ( pseq->size <= 0) {printf ("array is empt,remove error\n"); return array_empty ;} for (; i < pseq->size; i++) {if (pseq->array[i] == x) {size_t j = i + 1;for (; j < pseq->size; j++) {pseq->array[ J&NBSP;-&NBSP;1]&NBSP;=&NBSP;PSEQ->ARRAY[J];} pseq->size--;return 0;}} Return -1;} Int removeall (seqlist* pseq, datatype x) {assert (PSEQ); size_t i = 0;int flag = 0;if (PSEQ->SIZE&NBSP;≪= 0) {printf ("array is empt,remove error\n"); return array_empty;} for (; i < pseq->size; i++) {if (pseq->array[i] == x) {size_t j = i + 1;for (; j < pseq->size; j++) {pseq->array[ J&NBSP;-&NBSP;1]&NBSP;=&NBSP;PSEQ->ARRAY[J];} pseq->size--;flag++;if (pseq->size <= 0)//If the element of the sequential table is empty, it can no longer be deleted, return {return 0;} else{i--;}}} if (flag > 0) return 0;elsereturn -1;} Void bubblesort (SEQLIST*&NBSP;PSEQ)//bubble Sort {assert (PSEQ);size_t i = 0;for (; i < pseq->size; i++) {size_t j = 0;for (; j < pSeq- >size - i - 1; j++) {if (pseq->array[j] > pseq->array[j + 1]) {int tmp = pseq->array[j];p seq->array[j] = pseq->array[j &NBSP;+&NBSP;1];p seq->array[j + 1] = tmp;}}}} Void selectsort (SEQLIST*&NBSP;PSEQ)//select sort {assert (PSEQ);size_t i = 0;for (; i < pseq->size; i++) {int flag = pseq->array[i];size_t j = i;for (; j < pseq->size; j++) {if (pseq->array[j] < Flag) {Int tmp = pseq->array[j];p seq->array[j] = flag;flag = tmp;}} Pseq->array[i] = flag;}} Int binarysearch (seqlist* pseq, datatype x)//binary search {assert (PSEQ); size_t left = 0;size_t right = pSeq->size - 1;while (left <= right) {size _t mid = left + (Right - left) / 2;if (x > Pseq->array[mid]) {left = mid + 1;} else if (X < pseq->array[mid]) {right = mid - 1;} else if (x == p Seq->array[mid]) {return (int) mid;}} Return -1;}
This is only the most basic order table, can only store the basic type of data, if you want to store the structure or string of data, it will involve a C + + deep copy problem, and an array to save the unknown size of the data structure can not be done.
my blog will be updated on the order table of some of the optimization of the wording, I hope to help small friends.
C Language Implementation sequence table