C Language Implementation dynamic sequential table

Source: Internet
Author: User

#include <string.h> #include <stdio.h> #include <malloc.h> #include <assert.h> #include < stdlib.h>typedef int datatype;typedef struct seqlist{datatype* _array;size_t  _size;size_t capacity;} Seqlist;void initlist (seqlist *s) {assert (s); s->capacity =100;s->_array = (DataType*) malloc (sizeof (DataType) *s->capacity ); S->_size =0;memset (s->_array,0, (sizeof (DataType) *s- >capacity));} Void pushback (seqlist*s,datatype x) {assert (s); if (s->_size >=s->capacity ) { datatype *tmp=null;s->capacity *=2;tmp= (datatype *) malloc (sizeof (DataType) *s->capacity); memcpy (tmp,s->_array , (sizeof (DataType) *s->_size )); free (s->_array ); S->_array  =tmp;} S->_array [s->_size ++]=x;} Void popback (seqlist*s) {assert (s); if (s->_size ==0) {printf ("seqlist is null\n"); return;} s->_size--;} Void pushfront (seqlist*s,datatype x) {Int i;assert (s); if (s->_size >=s->capacity ) {DataType *tmp= null;s->capacity *=2;tmp= (datatype *) malloc (sizeof (DataType) *s->capacity); memcpy (tmp,s->_ array , (sizeof (DataType) *s->_size )); free (s->_array ); s->_array =tmp;} for (i= (int) s->_size ;i>0;i--) {s->_array [i]=s->_array [i-1];} s->_array [0]=x;s->_size ++;} Void popfront (seqlist*s) {Int i;assert (s); if (s->_size ==0) {printf ("seqlist is null\ n "); return;} for (i=0;i<s->_size-1 ;i++) {s->_array [i]=s->_array [i+1];} s->_size --;} Void insert (seqlist*s,size_t pos,datatype x) {Size_t i;assert (s); if (s->_size >= s->capacity ) {datatype *tmp=null;s->capacity *=2;tmp= (datatype *) malloc (sizeof ( DataType) *s->capacity) memcpy (tmp,s->_array , (sizeof (DataType) *s->_size )); Free (S->_array ); s->_array =tmp;} for (i=s->_size ;i>pos;i--) {s->_array [i]=s->_array [i-1];} s->_array [pos]=x;s->_size ++;} Void erase (Seqlist*s,size_t pos) {Int i;assert (s); assert (pos<=s->_size); if (s->_size  ==0) {printf ("seqlist is null\n"); return;} for (i=pos;i<s->_size-1 ;i++) {s->_array [i]=s->_array [i+1];} s->_size --;} Int find (seqlist*s,datatype x) {Int i;assert (s); for (i=0;i<s->_size ;i++) {if (s->_ array [i]==x) {return i;}} printf ("No value \ n");} Void remove (seqlist*s,datatype x) {Int i;assert (s); if (s->_size ==0) {printf ("Seqlist  is null\n "); return;} for (i=0;i<s->_size ;i++) {if (s->_array [i]==x) {  int begin=i;  for ( ; begin<s->_size-1;begin++)   {  s->_array[begin]=s->_array[begin+1];   }  s->_size --;  Break;}} if (i==s->_size) {printf ("No value \ n"); return;}} Void removeall (seqlist*s,datatype x) {datatype * first=s->_array ;D atatype *  second=s->_array ;D Atatype * end=second+s->_size;int count=0;assert (s); >_size ==0) {printf ("seqlist is null\n"); return;} for (; second<end;second++) {if (*second!=x) {*first=*second;first++;} else{count++;}} S->_size -=count;} Void modify (seqlist*s,size_t pos,datatype x) {assert (s), assert (pos<=s->_size ), if (s >_size ==0) {printf ("seqlist is null\n"); return;} S->_array [pos]=x;} Void print (seqlist*s) {Int i;assert (s); if (s->_size ==0) {printf ("seqlist is null"); return;} for (i=0;i<s->_size ;i++) {printf ("%d ", S->_array [i]);}} Void test () {seqlist s;initlist (&s); Print (&s); pushback  (&s,1); pushback  (&s,2); pushback  (&s,3); pushback  (&s, 2); pushback  (&s,2); pushback  (&s,7);/*print (&s); Popback (&s); Print (&s);*/pushfront  (&s,8);/*print (&s);*/popfront  (&s); Print (&s);p rintf ("\ n"); Remove (&s,2); Print (&s);p rintf ("\ n"); Insert (&s,1,2); Print (&s); RemoveAll (&s,2); Print (&s); Modify (&s,2,2); Print (&s);} Int main () {test (); System ("pause"); return 0;}


This article from "Liveyoung" blog, reproduced please contact the author!

C Language Implementation dynamic sequential 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.