最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這個我用wintc運行總是提示出現(xiàn)錯誤,請問該怎么解決?

這個我用wintc運行總是提示出現(xiàn)錯誤,請問該怎么解決?

大家?guī)蛶兔?,我想用c實現(xiàn)廣義表的創(chuàng)建和遍歷,但再最開始的定義廣義表就出現(xiàn)一堆錯誤。 以下是偽碼算法typedef enum { ATOM, LIST } ElemTag; typedef struct GLNode { ElemTag tag; union { AtomType atom; //元素有2個域 struct { struct GLNode *hp,*tp; }ptr; //表有3個域 } } *GList; 我編寫的 #include<stdio.h> #define ATOM 0 #define LIST 1 typedef enum { ATOM, LIST}ElemTag ; typedef struct GLNode { ElemTag tag; union { int atom; struct { struct GLNode *hp,*tp; }ptr; } }*GList; GList L; main(){} 沒學(xué)過enum 和union的用法所以我覺得是不是這兩個函數(shù)用錯了。請大家?guī)臀腋南鲁绦蝽槺愀嬖V我那兩個函數(shù)該怎么用,謝謝。
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經(jīng)驗 獲得超3個贊

囧~~
#include <stdio.h>
#include <stdlib.h>
#define NULL 0

typedef enum{ATOM, LIST}ElemTag; /*ATOM==0:原子,LIST==1:子表*/

typedef struct GLNode 
{
int tag; /*公共部分,區(qū)分原子和表結(jié)點*/
union /*原子結(jié)點和表結(jié)點的聯(lián)合部分*/ 

char atom; /*原子結(jié)點的值域*/  
struct GLNode *sublist; /*表結(jié)點表頭指針*/
};
struct GLNode *next; /*下一個元素結(jié)點*/
}GList;

void CreateGList(GList **gl);
void PrintGList(GList *gl);
int GListDepth(GList *gl);

int main(void)
{
GList *gl;

printf("建立一個廣義表,以右括號結(jié)束\n");
CreateGList(&gl);

printf("輸出廣義表:");
PrintGList(gl);

printf("\n");

printf("廣義表的深度:");
printf("%d\n", GListDepth(gl->sublist));

return 0;
}

/*************************************************
函數(shù)名稱:CreateGList

函數(shù)功能:創(chuàng)建一個廣義表(遞歸構(gòu)件子表)
輸入的時候是換一行輸入,最后要多輸右括號,是遞歸的原因

被本函數(shù)調(diào)用的函數(shù)清單:無

調(diào)用本函數(shù)的函數(shù)清單:無

輸入?yún)?shù):gl,取用指針的指針的形式,可以對其直接修改

輸出參數(shù):無

函數(shù)返回值:(void)
**************************************************/
void CreateGList(GList **gl)
{
char ch;
scanf("%c", &ch);
getchar(); /*吞食scanf留下的換行*/

if(ch == '#') /*如果輸入的是#表示為空*/
{
*gl = NULL;
}
else if(ch == '(') /*如果是左括號就遞歸構(gòu)件子表*/
{
*gl = (GList *)malloc(sizeof(GList));
(*gl)->tag = LIST;
CreateGList(&((*gl)->sublist));
}
else /*就是只有原子的情況下*/
{
*gl = (GList *)malloc(sizeof(GList));
(*gl)->tag = ATOM;
(*gl)->atom = ch;
}

scanf("%c", &ch); /*此處輸入的必為逗號或者右括號*/
getchar();

if((*gl) == NULL)
{
;
}
else if(ch == ',') /*如果是逗號就遞歸構(gòu)件下一個子表*/
{
CreateGList(&((*gl)->next));
}
else if(ch == ')') /*如果是右括號就結(jié)束*/
{
(*gl)->next = NULL;
}
}

/*************************************************
函數(shù)名稱:GListDepth

函數(shù)功能:求廣義表的深度(遞歸子表->到子表..(最長+1))

被本函數(shù)調(diào)用的函數(shù)清單:無

調(diào)用本函數(shù)的函數(shù)清單:無

輸入?yún)?shù):gl

輸出參數(shù):無

函數(shù)返回值:(void)
**************************************************/
int GListDepth(GList *gl)
{
int max, dep;

if(!gl)
return 1;

for(max = 0; gl; gl = gl->next)
{
if(gl->tag == LIST)
{
dep = GListDepth(gl->sublist); /*求以gl->sunlist的子表深度*/

if(dep > max)
{
max = dep;
}//if
}//if
}//for

return max + 1; /*各元素的深度的最大值加一*/
}

/*************************************************
函數(shù)名稱:PrintGList

函數(shù)功能:打印廣義表(遞歸打印子表)

被本函數(shù)調(diào)用的函數(shù)清單:無

調(diào)用本函數(shù)的函數(shù)清單:無

輸入?yún)?shù):gl

輸出參數(shù):無

函數(shù)返回值:(void)
**************************************************/
void PrintGList(GList *gl)
{
if(gl->tag == LIST)  
{
printf("("); /*先輸出左括號*/

if(gl->sublist == NULL)
{
printf("#");
}
else
{
PrintGList(gl->sublist); /*遞歸打印子表*/
}
printf(")"); /*結(jié)束打印右括號*/
}
else
{
printf("%c", gl->atom);

}

if(gl->next != NULL) /*如果沒結(jié)束就繼續(xù)遞歸打印子表*/
{
printf(", ");
PrintGList(gl->next);
}


查看完整回答
反對 回復(fù) 2023-04-05
  • 1 回答
  • 0 關(guān)注
  • 289 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號