C Language: "Dynamic sequential table" Dynamic sequence table initialization, printing, tail plug pushback, tail delete popback

Source: Internet
Author: User

#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 (&AMP;SEQ);     pushback (&seq,1);     pushback (&seq, 2);     pushback (&seq, 3);     pushback (&seq, 4);     Printseq (&AMP;SEQ);     popback (&seq);     printseq (&AMP;SEQ);     destroyseq (&AMP;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

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.