C Implementing a static sequential table

Source: Internet
Author: User

Static storage # define for #include  <stdio.h> #include  <assert.h> #include  <string.h>//sequential tables  maxsize 5typedef int datatype;typedef struct seqlist{ datatype array[ maxsize]; //Array Size  size_t size;              //number of valid elements}seqlist;void initseqlist (SEQLIST&NBSP;*PSEQ); Void pushback (SeqList *Pseq, &NBSP;DATATYPE&NBSP;X); Void pushfront (seqlist *pseq, datatype x); Void PopBack (SeqList &NBSP;*PSEQ); Void popfront (SEQLIST&NBSP;*PSEQ); Void printseqlist (SEQLIST&NBSP;*PSEQ);void  Insertseqlist (seqlist *pseq, size_t pos, datatype x); Int Find (SeqList *Pseq ,  datatype x);  //find element position by value void erase (seqlist *pseq, size_t pos);// Removes the Found element void remove (seqlist *pseq, datatype x); Void remove2 (Seqlist *pseq, &NBSP;DATATYPE&NBSP;X);//no use of Find and ERAsevoid removeall (seqlist *pseq, datatype x); Void removeall2 (SeqList *Pseq, &NBSP;DATATYPE&NBSP;X)///No use of find and erase, using the count tag, higher efficiency void bubblesort (SEQLIST&NBSP;*PSEQ);             //Bubble sort Void bubblesort1 (SEQLIST&NBSP;*PSEQ);             //Bubble Sort Optimization 1void bubblesort2 (SeqList &NBSP;*PSEQ);             //Bubble Sort Optimization 2void  Selectsort (SEQLIST&NBSP;*PSEQ);             // Select Sort Void selectsort1 (seqlist *pseq);             //Select Sort optimization Int binarysearch1 (seqlist *pseq, datatype x);   //binary Find int &NBSP;BINARYSEARCH2 (seqlist *pseq, datatype x);   //binary search   boundary discussion void  Initseqlist (SEQLIST&NBSP;*PSEQ) { memset (pseq->array, 0, sizeof (DataType) *maxsize);  pseq->size = 0;} 1. Check the parameter//2. Check//3 for boundary conditions. Complete function Logic void pushback (SEQLIST&NBSP;*PSEQ,&NBSP;DATATYPE&NBSP;X)       //judge whether the table is full { assert (PSEQ); if  (pseq->size >= maxsize)  {   printf ("Seqlist is full");  return; } else {  pseq-> Array[pseq->size] = x;  pseq->size++; } }void popback (SeqList * PSEQ)     //Determine if the table is empty { assert (pseq); if  (pseq->size<= 0)  {   printf ("Seqlist is empty");  return; } //pseq->array[pseq-> size-1] = 0; pseq->size--;} Void printseqlist (SEQLIST&NBSP;*PSEQ) { assert (PSEQ); int i = 0; for  (i  = 0; i < pseq->size; i++)  {  printf ("%d ",  Pseq- >arraY[i]);   } printf ("\ n");} Void pushfront (seqlist *pseq, datatype x) { int begin = pseq->size  - 1;       //Learn to write the logo  assert (pseq); if  (Pseq-> size >= maxsize)  {  printf ("Seqlist is full");   return; }   for  (;  begin >= 0; --begin)  {  pseq->array[begin  + 1] = Pseq->array[begin]; } Pseq->array[0] = x; Pseq-> size++;} Void popfront (SEQLIST&NBSP;*PSEQ) { assert (PSEQ); int begin = 1; if  ( pseq->size <= 0)  {  printf ("Seqlist is empty");  return;  } for  (Begin = 1; begin <= pseq->size-1; ++begin)  {   Pseq->array[begin-1 ] = Pseq->array[begin]; } pseq->size--;} Void insertseqlist (seqlist *pseq, size_t pos, datatype x) { assert (PSEQ);  assert (pos <= pseq->size); if  (pseq->size >= maxsize)  {   printf ("Seqlist is full");  return; } int begin =  pseq->size - 1; for  (begin = pseq->size - 1; begin  >= pos; begin--)  {  pseq->array[begin + 1] = pseq->array [begin]; } pseq->array[pos] = x; pseq->size++;} Int find (seqlist *pseq, datatype x) { assert (PSEQ); int i = 0;  for  (i = 0; i <= pseq->size - 1; i++)  {   if  (pseq->array[i] == x)    return i+1; } return -1;} Void erase (seqlist *pseq, sIze_t pos) { assert (PSEQ);  assert (pos < pseq->size);  int begin =  pos ; for  (begin = pos ; begin <= pseq->size ;  ++begin)  {  Pseq->array[begin -1] = Pseq->array[begin]; }  pseq->size--;} Multiplexing Find and Erasevoid remove (seqlist *pseq, datatype x) { assert (PSEQ);  int pos ;  pos = find (pseq, x); if  (pos != -1)  {  erase (Pseq,  pos);  }}//no longer uses find and Erasevoid remove2 (seqlist *pseq,datatype x) { assert (PSEQ);  size_t i = 0; size_t j = 0; for  (i = 0; i  < pseq->size ; ++i)  {  if  (pseq->array[i] == x)   {   for  (J&NBSP;=&NBSP;I;&NBSP;J&NBSP;&LT;&NBSP;PSEQ-&GT;SIZE-1;&NBSP;++J)    {    Pseq->array[j] = Pseq->array[j + 1];    }   pseq->size--;  } }}//multiplexing Find and Erasevoid removeall (SeqList * pseq, datatype x) { assert (PSEQ);  int pos=0; pos = find (Pseq, x);  while  (pos != -1)  {  erase (pseq, pos);  pos =  Find (pseq, x);  }}//no longer uses find and Erasevoid removeall2 (seqlist *pseq, datatype x) {  assert (PSEQ); size_t i = 0; size_t count = 0; for  (i  = 0; i < pseq->size; ++i)  {  if  (Pseq->array[i] &NBSP;==&NBSP;X)        //as soon as you encounter the same x,count++, the number that follows it moves the count forward, overwriting it.   {   count++;  }  else  {   Pseq-> Array[i - count] = pseq->array[i];  } } pseq->size = pseq->size - count;} Void bubblesort (SEQLIST&NBSP;*PSEQ)              //Bubble Sort { size_t i = 0; size_t j = 0; assert (Pseq);  for   (i = 0; i < pseq->size; ++i)  {  for  (j =0; &NBSP;J&NBSP;&LT;&NBSP;PSEQ-&GT;SIZE&NBSP;-&NBSP;I;&NBSP;++J)   {   if  (pseq-> ARRAY[J]&GT;PSEQ-&GT;ARRAY[J+1])    {    datatype tmp = pseq- >array[j];    Pseq->array[j] = Pseq->array[j+1];      pseq->array[j+1] = tmp;   }  } } }//Bubble Sort Optimization 1: Set a flag If this trip has been exchanged, then true, otherwise false.//obviously if there is a trip that does not occur, the ordering is complete. Void bubblesort2 (SEQLIST&NBSP;*PSEQ)                  //Bubble Sort Optimization 1, using an identity to reduce the number of sort times { size_t i = 0 ; size_t j = 0; bool flag = true; size_t length =  Pseq->size; assert (PSEQ); while  (flag)  {  flag = false;   for  (J&NBSP;=&NBSP;0;&NBSP;J&NBSP;&LT;&NBSP;LENGTH-1&AMP;&AMP;LENGTH&GT;0;&NBSP;++J)   {    if  (pseq->array[j] > pseq->array[j + 1])    {     datatype tmp = pseq->array[j];    pseq->array [j] = pseq->array[j + 1];    pseq->array[j + 1] =  tmp;    flag = true;   }  }  length--;  }}//Bubble Sort Optimization 2: If there are 100 number of arrays, only the first 10 unordered, and the next 90 are ordered,//and are larger than the previous 10 digits, then after the initial traversal, the position of the last interchange must be less than 10,//and the data after this position is ordered, Record this position, and the second time, simply walk from the array head to theThis location. Void bubblesort3 (SEQLIST&NBSP;*PSEQ)                   //Bubble Sort Optimization 2, records the last interchange position of the first traversal { size_t k = 0; size_t  j = 0; size_t flag = pseq->size; assert (PSEQ);   while   (flag)  {  k=flag;  flag = 0;  for  (j = 0; &NBSP;J&NBSP;&LT;&NBSP;K;&NBSP;++J)   {   if  (pseq->array[j] >  PSEQ-&GT;ARRAY[J&NBSP;+&NBSP;1])    {    datatype tmp = pseq- >array[j];    Pseq->array[j] = Pseq->array[j + 1];     Pseq->array[j + 1] = tmp;    flag =j;    }  } }}void selectsort (SEQLIST&NBSP;*PSEQ)               //Select Sort { size_t i = 0,j=0; size_t min = 0; for  ( i = 0; i < pseq->size-1 ; i++)  {  min = i;   for  (j = i + 1; j < pseq->size; j++)    {   if  (Pseq->array[min] > pseq->array[j])    {     min = j;   }  }  if  (min !=  i)   {   DataType tmp = Pseq->array[min];    pseq->array[min] = pseq->array[i];   pseq->array[i] = tmp;   } }}void selectsort2 (SEQLIST&NBSP;*PSEQ)              //Select the sorting optimization, selecting the maximum minimum row at the end of the sequence { size_t i = 0, j = 0; size_t  min = 0, max = 0; size_t left = 0, right = 0; for  (left =  0, right = pseq->size - 1; left <= right; left++,  right--)  {  min = left;  max = right;  for  ( I = left; i < right; ++i)   {   if  (pseq-> Array[i] < pseq->array[min])    {    min = i;    }   if  (Pseq->array[i]>pseq->array[max])    {     max = i;   }  }  if  (min !=  left)   {   DataType tmp = Pseq->array[min];    pseq->array[min] = pseq->array[left];   pseq->array[left] = tmp;   }  if  (Max == left)   {   max = min;  }  if  (max  != right)   {   DataType tmp = Pseq->array[max];    pseq->array[max] = pseq->array[right];   pseq->array[right]  = tmp;  } }}int binarysearch1 (seqlist *pseq, datatype x)     //Two-point search, two-point search boundary discussion    left closed right <=,right=mid-1{ int left = 0; int  right = Pseq->size-1; while  (left <=right)  {  int mid  = left +  (Right - left)  / 2;  if  (Pseq->array[mid] &NBSP;&GT;&NBSP;X)   {   right = mid-1;  }  if  ( PSEQ-&GT;ARRAY[MID]&NBSP;&LT;&NBSP;X)   {   left = mid + 1;   }  if  (PsEQ-&GT;ARRAY[MID]&NBSP;==&NBSP;X)   {   return mid;  } }  Return -1;} INT&NBSP;BINARYSEARCH2 (seqlist *pseq, datatype x)    //Two-point search, two-point search boundary discussion      left open right open,right=mid{ int left = 0; int right = pseq->size ;  while  (left < right)  {  int mid = left +  ( Right - left)  / 2;  if  (pseq->array[mid] > x)   {    right = mid ;  }  if  (pseq->array[mid] < &NBSP;X)   {   left = mid + 1;  }  if  ( PSEQ-&GT;ARRAY[MID]&NBSP;==&NBSP;X)   {   return mid;  } }  Return -1;}

This article is from the "printf Return Values" blog, so be sure to keep this source http://10741125.blog.51cto.com/10731125/1753158

C Implementing a static sequential table

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.