#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> #include < Malloc.h>typedef int datetype;typedef struct seqlist{ datetype *arr; size_t capacility; size_t size;} seqlist;//Create Space Void checkcapa (SEQLIST&NBSP;*SEQ) { assert (SEQ); if (seq->size >= seq->capacility) { Seq->capacility = 2 * Seq->capacility + 3; Seq->arr = (datetype*) realloc (seq->arr, Seq->capacility * sizeof (datetype)) }}//Initialize dynamic sequence table Void initseqlist ( SEQLIST&NBSP;*SEQ) { seq->arr = null/* malloc (sizeof (DateType) * &NBSP;3) */; seq->capacility = 0; seq->size = 0;} Destruction of Space Void destroyseq (SEQLIST&NBSP;*SEQ) { seq->capacility = 0; seq->size = 0; free (Seq->arr); seq->arr = null;} Tail plug Void pushback (seqlist *seq,datetype x) { assert (SEQ); checkcapa (SEQ); seq->arr[seq->size++] = x;} Tail Delete void popback (seqlist *seq) { assert (SEQ); if ( seq->size <= 0) { printf (The current order table is empty!) Cannot continue deleting \ n "); return; } --seq->size; printf ("\ n");} Print Dynamic order table Void printseq (seqlist *SEQ) { assert (SEQ); int index = 0; if (seq->size == 0) { printf ("The current order table is empty! \ n "); return; } for (index = 0; index < seq->size; index++) { printf ("%d->", seq->arr[index]); } printf ("\ n");} Void test () { seqlist seq; initseqlist (&SEQ); pushback (&seq,1); pushback (&seq, 2); pushback (&seq, 3); pushback (&seq, 4); Printseq (&SEQ); popback (&seq); printseq (&SEQ); destroyseq (&SEQ);} Int main () { test (); system ("Pause"); return 0;}
This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1743504
C Language: "Dynamic sequential table" Dynamic sequence table initialization, printing, tail plug pushback, tail delete popback