C + + Generalized table

Source: Internet
Author: User
Tags assert

Generalized tables

List with list, for example (1, (2, (3,4)), 5)

Linked list can be used to achieve

Results

Guangyibiao.h

#ifndef __GUANGYIBIAO__#define __GUANGYIBIAO__#include <stdio.h>#include <string.h>#include <memory.h>#include <malloc.h>#include <assert.h>#include <stdbool.h>#include <stdlib.h>#define AtomType inttypedef enum{ATOM, LIST}ElemTag;typedef struct GLNode{  ElemTag tag;  union{    AtomType atom;    struct GLNode* head;  };  struct GLNode* tail;}GLNode;typedef GLNode* GenList;void init(GenList* gl);void createGenList(GenList* gl, char* s);void show(GenList gl);#endif

Guangyibiao.c

#include "guangyibiao.h" void init (genlist* gl) {*GL = NULL;}  Select an element before the comma, the element can be an atom or it can be a list of bool Server1 (char* Sub, char* hsub) {int n = strlen (sub);  int i = 0;  char ch = sub[0];  int k = 0;  The function of k is to identify whether the comma is in parentheses, or outside the brackets, if the comma outside the parentheses jumps out while, the comma inside the parentheses does not jump out of the loop, continue to look down.    while (I < n && (ch! = ', ' | | k! = 0)) {if (ch = = ' (') {k++;    } else if (ch = = ') ') {k--;    } i++;  ch = sub[i];    }//comma within the range of sub if (I < n) {Sub[i] = ' + ';    strcpy (hsub, sub);  strcpy (Sub, sub+i+1);  }//brackets do not match else if (k! = 0) return false;    Sub itself is a table, for example, when I will be equal to N, so the sub to Hsub,sub there is no later part of the else{strcpy (hsub, sub);  Sub[0] = ' + '; } return true;  Idea: Recursively creates a node, if the sub is a list, recursively calls itself void Creategenlist (genlist* gl, char* s) {int n = strlen (s);  Remove the outer brackets of s, such as S (1, (2, 3)), handle the sub as 1, (2, 3), and at the end of the sub, add ' char* sub = (char*) malloc (sizeof (char) * (n-2));  char* hsub = (char*) malloc (sizeof (char) * (n-2));  ASSERT (NULL! = Sub && NULL! = hsub);  strncpy (Sub, s+1, n-2);  Sub[n-2] = ' + '; Glnode* p = *gl;      while (strlen (sub) = 0) {if (NULL = = p) {*GL = P = (glnode*) malloc (sizeof (Glnode));    P->head = P->tail = NULL;      }else{p = P->tail = (glnode*) malloc (sizeof (Glnode));    P->head = P->tail = NULL;    } assert (NULL! = p);    if (Server1 (sub, hsub)) {if (hsub[0] = = ' (') {p->tag = LIST;      Creategenlist (& (P->head), hsub);    } else{P->tag = ATOM;      P->atom = Atoi (hsub);    }}}}//idea: recursive void Show (Genlist gl) {if (gl = = NULL) return;    if (Gl->tag = = ATOM) {printf ("%d", gl->atom);    if (gl->tail! = NULL) {printf (",");  }//If GL is atom, GL will not have head, so just call Show (Gl->tail) show (Gl->tail);      }//If GL is a list, GL will have head and tail, so call Show (Gl->head), show (Gl->tail) Else if (Gl->tag = = LIST) {printf (");    Show (Gl->head);    printf (")");    if (gl->tail! = NULL) {printf (",");  } show (Gl->tail); }}

Guangyibiaomai.c

#include "guangyibiao.h"int main(){  GenList gl;  init(&gl);    char* a = "(1,2,3)";  char* b = "(1,(2,3))";  char* c = "(1,(2),3)";  char* d = "((1,2),3)";  char* e = "((1,2,3))";  char* f = "((),1)";  char* g = "(1,(2,(3,4)),5)";  char* h = "((),1,(2,(3,(),4)),5)";  char* i = "(())";    createGenList(&gl, i);  if(gl != NULL){    printf("(");    show(gl);    printf(")\n");  }    return 0;}

C + + Generalized 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.