| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1288 人关注过本帖
标题:广义表的基本操作
只看楼主 加入收藏
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
 问题点数:0 回复次数:1 
广义表的基本操作

程序代码:

//************************************************************
//Lists.cpp
//************************************************************
#include \"stdafx.h\"

//枚举类型,分别标识整数、字符和子表
typedef enum
{
INTGR,
CH,
LST
}ElemTag;

//广义表类
class Lists
{
private:
ElemTag utype;
Lists* first;
public:
Lists()
{
this->first=NULL;
}

//使用匿名联合体存储相应类型
union
{
int intinfo;
char charinfo;
Lists* hlink;
};

ElemTag GetType(){return this->utype;}
Lists* GetFirst(){return this->first;}
void SetFirst(Lists* f){this->first=f;}
void SetType(ElemTag t){this->utype=t;}

//S是广义表的书写形式串,由S创建广义表GL
Lists* CreateLists(char*& s);
//建立广义表时调用的过程
int Sever(char*& str1,char*& hstr1);
//求由ls指示的非递归表的深度
int Depth(Lists*& ls);
void PrtLists(Lists* h);
};


Lists* Lists::CreateLists(char*& s)
{
Lists *r=NULL,*q=NULL,*p=NULL;
p=q=new Lists;

//初始化两个工作字符串
char* sub=new char[30];
char* hsub=new char[30];
for(int i=0;i<30;i++)
hsub[i]=sub[i]='\0';

//将形式串拷贝到sub
strncpy(sub,s,strlen(s));
sub[strlen(s)]='\0';

cout<<\"欲创建的广义表 =\"<<s<<endl;
cout<<\"形式串的长度 =\"<<strlen(s)<<endl;

if(strlen(sub)==0 || !strcmp(sub,\"()\")) //如果形式串长度为0或者为空括号,则设为空表
p->SetFirst(NULL);
else
{
do
{
//调用函数处理字符串
Sever(sub,hsub);
if(strlen(hsub)==1)
{
//如果是数字
if(48<=hsub[0] && hsub[0]<=57)
{
//申请空间
r=new Lists;
//设置相应标识
p->SetType(ElemTag::INTGR);
//将元素存入联合体
p->intinfo=atoi(hsub);
//将新节点插入表尾
p->SetFirst(r);
p=r;
}
else
{
//如果是字符
if((65<=hsub[0]&&hsub[0]<91)||(hsub[0]>=97&&hsub[0]<123)||hsub[0]==')')
{
r=new Lists;
p->SetType(ElemTag::CH);
p->charinfo=hsub[0];
p->SetFirst(r);
p=r;
}
else
{
//如果是子表
if(hsub[0]=='(')
{
r=new Lists;
p->SetType(ElemTag::LST);
//将子表插入联合体的链域
p->hlink=r;
p->SetFirst(r);
p=r;
}
}
}
}
}while(*sub!='\0');
p->SetFirst(NULL);
}
//返回表头
return q;
}

//处理书写形式串的存储过程
int Lists::Sever(char *&str1, char *&hstr1)
{
char ch;
int i=0,k=0,j=0;
int n=strlen(str1);
static int m=1;
if(m==1)
//判断括号是否对称
while(j<n || k!=0)
{
ch=str1[j];
j++;
m++;
if(ch=='\0')
break;
if(ch=='(')
k++;
else if(ch==')')
k--;
}
if(k!=0)
return 0;

//清除逗号
for(i=0;i<n;++i)
{
ch=str1[i];
if(ch=='\0')
break;
if(ch==',')
{
for(int i0=i;i0<n;i0++)
str1[i0]=str1[i0+1];
break;
}
}

//将sub的字符逐个取出然后在sub删除已取出的字符
if(n>1)
{
strncpy(hstr1,str1,1);
strncpy(str1,str1+1,n-1);
str1[n-1]='\0';
return 1;
}
else if(n==1)
{
strncpy(hstr1,str1,1);
str1[0]='\0';
return 1;
}
else
return 0;
}

//返回(非递归)广义表的深度
int Lists::Depth(Lists*& ls)
{
Lists* temp=ls;
int m=0;
if(temp->GetFirst()==NULL)
return 0;
do
{
//如果有子表就计数
if(temp->utype==LST)
m++;
temp=temp->GetFirst();
}while(temp!=NULL);

return m;
}

//输出广义表
void Lists::PrtLists(Lists* h)
{
Lists *q=NULL,*p=h;
//如果不为空表
if(h!=NULL)
do
{
//如果元素是整数
if(p->utype==INTGR)
{
q=p->GetFirst();
//如果下一个节点是右括号或者表尾直接输出
if(q->charinfo==')' || q->GetFirst()==NULL)
cout<<p->intinfo;
else
//否则附加逗号
cout<<p->intinfo<<',';
//p指向下一个节点
p=p->GetFirst();
}
else
{
//如果是字符
if(p->utype==CH)
{
q=p->GetFirst();
if(q->charinfo==')' || q->GetFirst()==NULL)
cout<<p->charinfo;
else
cout<<p->charinfo<<',';
p=p->GetFirst();
}
else
//如果是子表则输出左括号
if(p->utype==LST)
{
cout<<'(';
p=p->GetFirst();
}
}
}while(p->GetFirst()!=NULL);
}

int _tmain(int argc, _TCHAR* argv[])
{
char* GUCH1=\"(1,((2,A),B))\";
char* GUCH2=\"(A,(a,(1,2)),B)\";
Lists* lists=new Lists;
lists=lists->CreateLists(GUCH1);
lists->PrtLists(lists);
cout<<endl;
cout<<\"广义表的深度 =\"<<lists->Depth(lists)<<endl;

lists=lists->CreateLists(GUCH2);
lists->PrtLists(lists);
cout<<endl;
cout<<\"广义表的深度 =\"<<lists->Depth(lists)<<endl;
return 0;
}

[此贴子已经被作者于2007-6-26 19:11:58编辑过]

搜索更多相关主题的帖子: 广义表 Lists first ElemTag LST 
2007-06-24 10:04
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
得分:0 

stdafx.h里是


// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once


#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <stdio.h>
#include <tchar.h>
#include <math.h>


#include <iostream>
#include <string>
using namespace std;


Viva,espana!
2007-06-24 10:06
快速回复:广义表的基本操作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013506 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved