資源簡(jiǎn)介
consts.cpp
代碼片段和文件信息
#include“consts.h“
typedef?struct?GLNode
{
char?name[100];?/*?教師或?qū)W生的姓名?*/
char?prof[100];?/*?教師結(jié)點(diǎn)表示職稱,學(xué)生結(jié)點(diǎn)表示班級(jí)?*/
int?type;?/*?結(jié)點(diǎn)類型:0--教師,1--研究生,2--本科生?*/
struct?{struct?GLNode?*hp*tp;}ptr;/*?hp?指向同級(jí)的下一結(jié)點(diǎn),tp?指向下級(jí)的首結(jié)點(diǎn)?*/
}GList;
GList?*GListCreate(char?*str)/*建立廣義表*/
{
GList?*head*p*q*m*a;/*?簡(jiǎn)要介紹:head?指向頭結(jié)點(diǎn)不變;p?指向?qū)熃Y(jié)點(diǎn);q?指向研究生結(jié)點(diǎn);a?指向本科生節(jié)點(diǎn);?m?指向新建立的節(jié)點(diǎn)*/
int?i?=?0j?=?0flag?=?0flag1?=?0flag2=0len?=?strlen(str);
head=p=q=m=a=NULL;
while(i? {
if(str[i]?==?‘)‘?||?str[i]?==?‘(‘?||?str[i]?==?‘‘||?str[i]?==?‘)‘||?str[i]?==?‘(‘||?str[i]?==?‘,‘)
{i++;continue;}
else
{
if(!(m?=(GList?*)malloc(sizeof(GList))))?exit(1);
for(j=0;str[i]?!=?‘-‘;)?/*?將字符串中的學(xué)生信息轉(zhuǎn)化成學(xué)生結(jié)點(diǎn)?*/
m->name[j++]?=?str[i++];
m->name[j]?=?‘\0‘;
for(j=0++i;str[i]?!=?‘-‘;)
m->prof[j++]?=?str[i++];
m->prof[j]?=?‘\0‘;
m->type?=?str[++i]?-?48;
m->ptr.hp=m->ptr.tp=NULL;
i++;
if(m->type?==?0)?/*?導(dǎo)師結(jié)點(diǎn)的處理?*/
{
if(flag)
{
p->ptr.hp=m;?/*?非首結(jié)點(diǎn)?*/
p=m;
}
else
{
head=p=m;?/*?首結(jié)點(diǎn)的處理?*/
flag=1;
}
flag1=0;
a=q=m;?/*?a?在此等于?m主要是處理本科生直屬于導(dǎo)師的情
況?*/
}
else?if(m->type==1)?/*?研究生結(jié)點(diǎn)?*/
{
if(flag1)
{
q->ptr.hp=m;?/*?非首結(jié)點(diǎn)的處理?*/
q=m;
}
else
{
q->ptr.tp=m;?/*?首結(jié)點(diǎn)的處理?*/
q=m;
flag1=1;
}
flag2=0;
a=m;
}
else?/*?本科生結(jié)點(diǎn)?*/
{
if(flag2)
{
a->ptr.hp=m;?/*?非首結(jié)點(diǎn)的處理?*/
a=m;
}
else
{
a->ptr.tp=m;?/*?首結(jié)點(diǎn)的處理?*/
a=m;
flag2=1;
}
}
}
}
return?head;
}
void?GListPrint(GList?*head)/*輸出廣義表*/
{
GList?*p*q*a;?/*?與?CreatGList?函數(shù)中的指向一樣?*/
int?flag=0flag1=0flag2=0;
p?=?head;
printf(“(“);
while(TRUE)?/*?導(dǎo)師范疇?*/
{
if(p?==?NULL)?break;
if(flag)
printf(“(%s-%s-%d“p->namep->profp->type);
else
{
printf(“(%s-%s-%d“p->namep->profp->type);
????flag=1;
}
q?=?p->ptr.tp;
flag2=flag1=0;
while(TRUE)?/*?研究生或本科生范疇?*/
{
if(q?==?NULL)?break;
if(flag1)
if(q->type==1)
printf(“(%s-%s-%d“q->nameq->profq->type);
else
printf(“%s-%s-%d“q->nameq->profq->type);
else
{
printf(“(%s-%s-%d“q->nameq->profq->type);
flag1=1;
}
a?=?q->ptr.tp;
flag2?=?0;
while(TRUE)?/*?本科生范疇?*/
{
if(a?==?NULL)?break;
if(flag2)
printf(“%s-%s-%d“a->namea->profa->type);
else
{
printf(“(%s-%s-%d“a->namea->profa->type);
flag2?=?1;
}
a?=?a->ptr.hp;
}
if(flag2)?printf(“)“);
if(q->type==1?||(?q->ptr.hp==NULL))
printf(“)“);
q?=?q->ptr.hp;
}
printf(“)“);
p?=?p->ptr.hp;
}
printf(“)\n“);
}
GList?*StudentInsert(GList?*head)/*?插入學(xué)生?*/
{
char?slen[100]teacher[100]graduate[100];
GList?*Slen*p*q;
int?ij;
p?=?head;
printf(“請(qǐng)輸入待插入學(xué)生信息,如:李剛-二班-1\n“);
scanf(“%s“slen);
if(!(Slen?=(GList?*)malloc(sizeof(GList))
評(píng)論
共有 條評(píng)論