C/C++知识点之广义表
小标 2018-08-10 来源 : 阅读 1018 评论 0

摘要:本文主要向大家介绍了C/C++知识点之广义表,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之广义表,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

列表里面有列表,比如(1,(2,(3,4)),5)

用链表可以实现

结果如图


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 int

typedef 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;
}

//挑出逗号前的一个元素,元素可以是原子也可以是列表
bool server1(char* sub, char* hsub){

  int n = strlen(sub);
  int i = 0;
  char ch = sub[0];
  int k = 0;
  //k的作用是,识别逗号是括号里的,还是括号外的,如果是括号外的逗号就跳出while了,括号内的逗号不跳出循环,继续往下找。
  while(i < n && (ch != ',' || k != 0)){
    if(ch == '('){
      k++;
    }
    else if(ch == ')'){
      k--;
    }
    i++;
    ch = sub[i];
  }

  //逗号在sub的范围内
  if(i < n){
    sub[i] = '\0';
    strcpy(hsub, sub);
    strcpy(sub, sub+i+1);
  }
  //括号不匹配
  else if(k != 0) return false;
  //sub本身就是表,比如为(1,2)时,i会等于n,所以把sub给hsub,sub就没有以后部分了
  else{
    strcpy(hsub, sub);
    sub[0] = '\0';
  }

  return true;
}

//思路:递归创建节点,如果sub是列表,就递归调用自己
void createGenList(GenList* gl, char* s){

  int n = strlen(s);

  //去掉s的外括号,比如s为(1,(2, 3)),处理后sub为1,(2, 3),并在sub的最后加上'\0'
  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] = '\0';

  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);
      }
    }    
  }
}
//思路:递归
void show(GenList gl){
  if(gl == NULL) return;
  
  if(gl->tag == ATOM){
    printf("%d", gl->atom);
    if(gl->tail != NULL){
      printf(",");
    }
    //如果gl为ATOM的话,gl就不会有head,所以只需调用show(gl->tail)
    show(gl->tail);
  }
  //如果gl为LIST的话,gl就会有head,也会有tail,所以调用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/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程