Representation and implementation of recursive data structures for generalized tables--writing data structures by themselves

Source: Internet
Author: User

The file Glist.h header file is as follows

#ifndef _glist_h_#define _glist_h_typedef enum {atom,list}elemtag;typedef struct _glist{    elemtag tag;        Union     {        char data;        struct _glist *sublist;         } Val;        struct _glist *next;} Glist,*pglist;int glist_length (pglist PGL); int glist_depth (pglist pgl);p GList creat_glist (char **ch); void Printf_glist (PGList PGL); #endif


Data structure implementation GLIST.C as follows

/************************ Time: 2014.12.15xiao_ping_ping compilation environment: dev-c++ 4.9.9.2 Content: Recursive representation and implementation of generalized tables: learning to write data structures *************** /#include <string.h> #include <stdlib.h> #include "glist.h"/* Generalized table length */int glist_length (pglist        PGL) {int length = 0;              PGL = pgl->val.sublist;        while (PGL) {length++;          PGL = pgl->next; } return length;}        /* Depth of the generalized table */int glist_depth (pglist pgl) {int max = 0,depth;         if (0 = = Pgl->tag) {return 0;    } PGL = pgl->val.sublist;            if (!PGL) {return 1;             } while (PGL) {if (pgl->tag) {depth = glist_depth (PGL);                     if (Depth > max) {max = depth;    }} PGL = pgl->next; } return max+1;}        /* Create a generalized table */pglist creat_glist (char **ch) {pglist PGL;    if (NULL = = ch) {return;     } Char tch = * (*CH); *ch = *ch + 1;        if (' tch '! = ') {PGL = (GList *) malloc (sizeof (GList));            if (' (' = = = tch) {Pgl->tag = LIST;        pgl->val.sublist = creat_glist (CH);        } else if (') ' = = tch) {PGL = NULL;            } else {pgl->tag = ATOM;            Pgl->val.data = tch;             }} else {PGL = NULL;     } tch = * (*CH);    *ch = *ch + 1;                if (PGL) {if (', ' = = tch) {pgl->next = creat_glist (CH);            } else {pgl->next = NULL; }} return PGL; }/* Traversal print Generalized table */void Printf_glist (PGList pgl) {if (PGL) {if (1 = = Pgl->tag) {printf (")            ;             if (pgl->val.sublist) {printf_glist (pgl->val.sublist);                } else {printf (""); }} else        {printf ("%c", pgl->val.data);         } if (1 = = Pgl->tag) {printf (")");            } if (Pgl->next) {printf (",");                       Printf_glist (Pgl->next);  }    }}


The test file test.c is as follows:

/*************************************** Time: 2014.12.15xiao_ping_ping****************************************/# Include <conio.h> #include <stdlib.h> #include <string.h> #include "glist.h" int main () {    int length,depth;    Char *s= "(A, (B,c,d), (d,l), d,p))";     PGList GL;         GL = creat_glist (&s);    Length = Glist_length (GL);    Depth = Glist_depth (GL);    printf ("length =%d,depth =%d\n", length,depth);    Printf_glist (GL);        Getch ();    return 0;         }


The results of the operation are as follows:

Representation and implementation of recursive data structures for generalized tables--writing data structures by themselves

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.