Static sequential table of C implementation

Source: Internet
Author: User

The              sequential table is a linear table saved in the form of an array in computer memory. is to store the linear structure of the data elements in sequence with a contiguous set of storage cells. Linear tables are stored in sequential storage, which is called sequential tables. A sequential table is one in which nodes in a table are stored sequentially in a contiguous set of storage cells in the computer's memory. The storage feature of the       sequential table is that the address of any element in the table can be calculated as long as the starting position is determined.         when implementing the sequential table in C, the function is put into the header file, and the unit function is tested in the main function. sequencelist_static.h#ifndef __sequencelist_static_h__#define __sequencelist_static_h__#include< stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h>typedef int  datatype;//with typedef facilitates the modification of sequential table storage types #define max 100//  The use of macro definitions makes sequential table storage capacity modification convenient typedef struct  Seqlist{ datatype arr[max]; int size;} The basic operation of the seqlist, *pseqlist;//sequence table void initseqlist (PSEQLIST&NBSP;PSEQ); Void printseqlist (SeqList &NBSP;SEQ); Void pushback (pseqlist pseq, datatype x); Void popback (PSEQLIST&NBSP;PSEQ); Void pushfront (pseqlist pseq, datatype x);void Popfront (PSEQLIST&NBSP;PSEQ); Void insert (pseqlist pseq,int pos,datatype x); Int Find ( SEQLIST&NBSP;SEQ,DATATYPE&NBSP;X); Void remove (pseqlist pseq,datatype x); Void RemoveAll ( PSEQLIST&NBSP;PSEQ,DATATYPE&NBSP;X); void reverselist (PSEQLIST&NBSP;PSEQ); Void sortlist (pSeqList &NBSP;PSEQ); Int binarysearch (seqlist seq,datatype x);//initialization of the Order table Void initseqlist (pSeqList &NBSP;PSEQ) { assert (PSEQ);  memset (Pseq->arr,0,sizeof (Pseq->arr));  pseq->size =  0;} To make it easier to view the order table for printing void printseqlist (seqlist seq) { int i = 0; for (i =  0; i < seq.size; i++)  {  printf ("%d\t", Seq.arr[i]); }  printf ("over\n");} Store Data Void pushback (pseqlist pseq, datatype x) { assert (PSEQ);  if (pSeq->size  >= max)//order table storage should first determine whether the order table is full  {  printf ("sequence list is full\n");  } pseq->arr[pseq->size++] = x;} Void popback (PSEQLIST&NBSP;PSEQ) { assert (PSEQ);  if (pseq->size == 0)  {   printf ("the sequencelist is empty\n");  } pseq->size--;} Void pushfront (pseqlist pseq, datatype x) { int i = 0; assert (pSeq );  if (Pseq->size == max)  {  printf ("sequence list is full\n");   return; } for (i = pseq->size; i > 0; i--)  {   pSeq->arr[i] = pSeq->arr[i-1]; } pSeq->arr[0] = x;  pseq->size++;} Void popfront (PSEQLIST&NBSP;PSEQ) { int i = 0; assert (PSEQ);  if (pSeq-> size == 0)  {  printf ("the sequencelist is empty\n");   return ;  } for (i = 0; i < pseq->size-1; i++) &nbsp {  pseq->arr[i] = pseq->arr[i+1]; } pseq->size--;} Void insert (pseqlist pseq,int pos,datatype x) { int i = 0; assert ( PSEQ)///insert position should be legal  assert ((pos<pseq->size)  &&  (pos >= 0));  if ( Pseq->size == max)  {  printf ("the sequence list is full\n");   return; } for (i = pseq->size; i>pos; i--)  {   Pseq->arr[i] = pseq->arr[i-1]; } pseq->arr[pos] = x; pseq->size ++;} Int find (seqlist seq,datatype x) { int i = 0; for (i = 0;  i<seq.size; i++)  {  if (seq.arr[i] == x)   {    Return i;  }  return -1; }}void remove (PSeqList pSeq,DataType &NBSP;X) { int pos =&nbsP Find (*pseq,x);  int i = 0; assert (PSEQ);  if (pos != -1)  {   for (i = pos; i<pseq->size; i++)   {   pseq->arr[i]  = pseq->arr[i+1];  } } pseq->size--;} Void removeall (pseqlist pseq,datatype x) { int i = 0; int j =  0; assert (PSEQ);  for (i =0 ; i<pseq->size; i++)  {  if ( pseq->arr[i] == x)   {   for (j = i; j<pseq->size;  j++)    {    pSeq->arr[j] = pSeq->arr[j+1];    }   pseq->size--;  } }}void reverselist (PSEQLIST&NBSP;PSEQ) {  int start = 0; int end = pseq->size-1; datatype tmp  = 0; while (start < end) &NBSP;{&NBsp; tmp = pseq->arr[start];  pseq->arr[start] = pseq->arr[end];   pseq->arr[end] = tmp;  start++;  end--; }}//Bubble Sort void  Sortlist (PSEQLIST&NBSP;PSEQ) { int i = 0; int j = 0; assert (PSEQ);  for (i = 0; i<pseq->size-1; i++)  {  for (j = 0; j <pseq->size-1-i; j++)   {   if (pseq->arr[j] > pseq->arr[ J+1])    {    DataType tmp = pSeq->arr[j];     pseq->arr[j] = pseq->arr[j+1];    pseq->arr[j+1] =  tmp;   }  } }}int binarysearch (seqlist seq,datatype x) {  int left = 0; int right = seq.size-1; while (left <=  right)//should pay attention to the boundary conditions  {&nbsP; int mid = left-((left - right) >>1);   if (Seq.arr[mid]  &GT;&NBSP;X)   {   right = mid - 1;  }  else  if (seq.arr[mid] == x)   {   return mid;  }   Else  {   left = mid + 1;  } } return -1 ;} #endif//__sequencelist_static_h__ The following is a test of the function:test.c#include  "sequencelist_static.h" Void test1 () {  Seqlist myseqlist; initseqlist (&myseqlist);  pushback (&myseqlist,1);  PushBack (& myseqlist,2);  pushback (&myseqlist,3);  printseqlist (myseqlist);} Void test2 () { seqlist myseqlist; initseqlist (&myseqlist);  PushBack (&myseqlist , 1);  pushback (&myseqlist,2);  pushback (&myseqlist,3);  printseqlist (myseqlist);  Popback (&myseqlist);  printseqlist (Myseqlist);} Void test3 () { seqlist myseqlist; initseqlist (&myseqlist);  PushFront (& myseqlist,3);  pushfront (&myseqlist,2);  pushfront (&myseqlist,1);  pushfront (& myseqlist,0);  printseqlist (myseqlist);} Void test4 () { seqlist myseqlist; initseqlist (&myseqlist);  PushFront (& myseqlist,3);  pushfront (&myseqlist,2);  pushfront (&myseqlist,1);  pushfront (& myseqlist,0);  printseqlist (myseqlist);  popfront (&myseqlist);  popfront (&myseqlist);  popfront (&myseqlist);  popfront (&myseqlist);  popfront (&myseqlist);  Printseqlist (myseqlist);} Void test5 () { seqlist myseqlist; initseqlist (&myseqlist);  PushBack (&myseqlist , 1);  pushback (&myseqlist,2);  pushback (&myseqlist,3);  printseqlist (myseqlist);  Insert (&myseqlist,0,0);  printseqlist (myseqlist);} Void test6 () { seqlist myseQlist; initseqlist (&myseqlist);  pushback (&myseqlist,1);  pushback (&myseqlist,2);  pushback (&myseqlist,3);  printseqlist (myseqlist);  remove (&myseqlist,1);  Printseqlist (myseqlist);} Void test7 () { seqlist myseqlist; initseqlist (&myseqlist);  PushBack (&myseqlist , 1);  pushback (&myseqlist,2);  pushback (&myseqlist,3);  printseqlist (myseqlist);  Reverselist (&myseqlist);  printseqlist (myseqlist);} Void test8 () { seqlist myseqlist; initseqlist (&myseqlist);  PushBack (&myseqlist , 2);  pushback (&myseqlist,5);  pushback (&myseqlist,1);  pushback (&myseqlist,3);  Pushback (&myseqlist,4);  printseqlist (myseqlist);  sortlist (&myseqlist);  PrintSeqList ( myseqlist);} Void test9 () { int ret = 0; seqlist myseqlist; initseqlist (& myseqlist);  pushback (&myseqlist,1);  pusHback (&myseqlist,2);  pushback (&myseqlist,3);  pushback (&myseqlist,4);  PushBack (& myseqlist,5);  printseqlist (myseqlist);     ret = binarysearch (myseqlist,5);  printf ("%d\n", ret);} Void test10 () { seqlist myseqlist; initseqlist (&myseqlist);  PushBack (& myseqlist,1);  pushback (&myseqlist,2);  pushback (&myseqlist,3);  pushback (&myseqlist,1 );  pushback (&myseqlist,5);  printseqlist (myseqlist);     removeall (& myseqlist,1);  printseqlist (myseqlist);} Int main () { test10 ();  system ("pause");  return 0;}

Static sequential table of C implementation

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.