问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

抽象数据类型定义的具体函数实现(C++实现)52

发布网友 发布时间:2023-10-11 17:03

我来回答

5个回答

热心网友 时间:2024-11-02 04:21

#include<iostream>
#define ok 1
#define error 0
using namespace std;
typedef int status;
typedef struct Lnode
{
float coef;
int expn;
struct Lnode*next;
}*link;
typedef struct
{
link head,tail;
int len;
}polynomail;
extern status makenode(link&p,int e,float f)
{
p=(link)malloc(sizeof(Lnode));
if(!p)return error;
p->coef=f;
p->expn=e;
return ok;
}//生成一个节点
void freenode(link &p){free(p);}//释放一个节点
status initlist(polynomail &p)
{
p.head=p.tail=NULL;
p.len=0;
return ok;
}//初始化一个线性表
extern status insfirst(polynomail &p,link h,link s)
{
s=(link)malloc(sizeof(Lnode));if(!s)return error;
h=(link)malloc(sizeof(Lnode));if(!h)return error;
h=p.head;
s->next=h->next;
h->next=s;
return ok;
}//已知h指向线性表的头结点,将s所指节点插入在第一个节点之前
extern status delfirst(polynomail &p,link h,link &q)
{
q=(link)malloc(sizeof(Lnode));if(!q)return error;
h=(link)malloc(sizeof(Lnode));if(!h)return error;
h=p.head;
q=h->next;
h->next=q->next;
free(q);
return ok;
}//删除线性表第一个节点
extern status append(polynomail &p,link s)
{
p.tail->next=s;
while(s->next){s=s->next;}
p.tail=s;
return ok;
}//将指针s所指的一串节点链接在线性表的末尾,并改变表的尾指针
extern status remove(polynomail &p,link &q)
{
link s;
s=(link)malloc(sizeof(Lnode));
if(!s)return error;
s=p.head;
while(s->next!=p.tail){s=s->next;}
q=p.tail;
free(q);
p.tail=s;
return ok;
}//删除尾节点并以q返回,然后改变链表尾指针的指向
extern status insbefore(polynomail &p,link &q,link s)
{
link h;
s=(link)malloc(sizeof(Lnode));if(!s)return error;
h=(link)malloc(sizeof(Lnode));if(!h)return error;
h=p.head;
while(h->next!=q){h=h->next;}
s->next=q;
h->next=s;
q=s;
return ok;
}//在q所指节点之前插入一个节点
extern status insafter(polynomail &l,link &p,link s)
{
s->next=p->next;
p->next=s;
p=s;
return ok;
}//在P所指节点之后插入一个节点
extern status SetCurElem(link &p,int e,float f)
{
p->coef=f;
p->expn=e;
return ok;
}//设置节点的值
extern link GetCurElem(link p)
{
return p;
}//返回一个指向节点的指针
extern status listempty(polynomail p)
{
if(p.head==p.tail)
return ok;
else return error;
}//判断线性表是否为空
extern int listlength(polynomail p)
{
return(p.tail-p.head+1);
}//求线性表的长度
extern link GetHead(polynomail p)
{
return p.head;
}
extern link GetTail(polynomail p)
{
return p.tail;
}
extern link priorpos(polynomail l,link p)
{
link q;
q=(link)malloc(sizeof(Lnode));if(!q)return error;
q=l.head;
while(q->next!=p){q=q->next;}
if(q->next==p)return q;
else return NULL;
}//返回p节点前驱的位置
extern link nextpos(polynomail l,link p)
{
link q;
q=(link)malloc(sizeof(Lnode));if(!q)return error;
if(!p->next)return NULL;
return p->next;
}//返回p节点后继的位置
extern status locatepos(polynomail l,int i,link &p)
{
link q;
q=(link)malloc(sizeof(Lnode));if(!q)return error;
q=l.head->next;
int j=1;
while(q&&j<i)
{
q=q->next;
j++;
}
p=q;
if(!q||j>i)return error;
else return ok;
}//返回p所指的第i项的地址
extern int compare(link p,link q)
{
if(p->expn>q->expn)return 1;
if(p->expn<q->expn)return -1;
if(p->expn==q->expn)return 0;
}
extern link locateelem(polynomail p,link m,link &q,link &h,int(*cmp)(link a,link b))
{
q=(link)malloc(sizeof(Lnode));if(!q)return error;
h=(link)malloc(sizeof(Lnode));if(!h)return error;
cmp=compare;//函数指针的赋值
for(q=p.head->next;q->next!=NULL;q=q->next)
{
int n; n=(*cmp)(m,q);
if(n==0)
{
return q;
}
}
for(h=p.head->next;h->next!=NULL;h=h->next)
{
int n; n=(*cmp)(m,h);
if(n>0)
{
return priorpos(p,h);
}
}
}
兄弟我可是花了好长的时间写的啊,通过调试了!
我应该跟你一样都是学计算机专业的!
现在真的是好需要积分啊!麻烦你还是把你那诱人的分数给我吧!
我不胜感激啊!

热心网友 时间:2024-11-02 04:22

我将你所说的抽象结构全部面向对象(类模板)实现,并将全部实现写入头文件(当然正常只会写入类接口,不会写实现):
基元类:ElemSet<T>
链表类:List<T>
栈类:Stack<T>
直接在你的工程中包括这些头文件就能用了

#ifndef ELEMSET
#define ELEMSET

#include <iostream>
using namespace std;

template <typename T>
class ElemSet //数据对象
{
T data;
public:
ElemSet<T>* next;

ElemSet():next(NULL){}
ElemSet(T d):data(d),next(NULL){}
ElemSet(const ElemSet<T>& e):data(e.data),next(NULL){}
void operator=(const ElemSet<T>& e){data=e.data;next=NULL;}

const T& read(){return data;}
bool compare(ElemSet<T> t)
{
return data==t.data;
}
void visit()
{
if(!(cout<<data)) throw "错误";
}
};

#endif

#ifndef LIST
#define LIST

#include <iostream>
#include "ElemSet.h"
using namespace std;

template <typename T>
class List
{
ElemSet<T>* first; //头指针
public:
List() //就是你说的InitList()
{
first=new ElemSet<T>;
first->next=NULL;
}
~List() //就是说的DestroyList()
{
while(first)
{
ElemSet<T>* p=first;
first=first->next;
delete p;
}
}
void Clearlist()
{
while(first->next)
{
ElemSet<T>* p=first;
first=first->next;
delete p;
}
}
bool ListEmpty()
{
return first->next==NULL;
}
int ListLength()
{
int count=0;
for(ElemSet<T>* p=first->next;p;p=p->next)
count++;
return count;
}
void GetElem(int num,ElemSet<T>& e)
{
ElemSet<T>* p=first;
for(;num;num--)
{
if(!(p=p->next)) throw "位置错误";
}
e=*p;
}
int LocateElem(const ElemSet<T>& t)
{
int loc=1;
for(ElemSet<T>* p=first->next;p;p=p->next)
{
if(p->compare(t)) return loc;
loc++;
}
return 0;
}
void PriorElem(const ElemSet<T>& cur_e,ElemSet<T>& pre_e)
{
if(first->next->compare(cur_e)) throw "第一个元素无前驱";
for(ElemSet<T>* p=first->next;p->next;p=p->next)
{
if(p->next->compare(cur_e))
{
pre_e=*p;
return;
}
}
throw "位置超限";
}
void NextElem(const ElemSet<T>& cur_e,ElemSet<T>& next_e)
{
for(ElemSet<T>* p=first->next;p->next;p=p->next)
{
if(p->compare(cur_e))
{
next_e=*(p->next);
return;
}
}
throw "链表没有该元素,或该元素没有后继";
}
void ListInsert(int i,const ElemSet<T>& e)
{
if(i<1) throw "位置错误";
ElemSet<T>* p=first; //找到待插入位置的前一个元素
for(;i>1;i--)
if(!(p=p->next)) throw "位置错误";
ElemSet<T>* t=new ElemSet<T>(e);
t->next=p->next;
p->next=t;
}
void ListDelete(int i,ElemSet<T>& e)
{
if(i<1) throw "位置错误";
ElemSet<T>* p=first; //找到待删除位置的前一个元素
for(;i>1;i--)
if(!(p=p->next)) throw "位置错误";
ElemSet<T>* t=p->next;
if(!t) throw "位置错误";
p->next=t->next;
e=*t;
delete t;
}
void ListTraverse()
{
for(ElemSet<T>* p=first->next;p;p=p->next)
{
p->visit();
cout<<' ';
}
cout<<endl;
}
};

#endif

#ifndef STACK
#define STACK

#include <iostream>
#include "ElemSet.h"
using namespace std;

template <typename T>
class Stack
{
ElemSet<T>* top;
public:
Stack<T>():top(NULL){} //就是你说的InitStack()
~Stack<T>() //就是你说的DestroyStack()
{
while(top)
{
ElemSet<T>* p=top;
top=top->next;
delete p;
}
}
void Clearlist()
{
while(top)
{
ElemSet<T>* p=top;
top=top->next;
delete p;
}
}
bool StackEmpty()
{
return !top;
}
int StackLength()
{
int n=0;
for(ElemSet<T>* p=top;p;p=p->next)
n++;
return n;
}
void GetTop(ElemSet<T>& e)
{
if(!top) throw "当前栈为空";
e=*top;
}
void Push(const ElemSet<T>& e)
{
ElemSet<T>* p=new ElemSet<T>(e);
p->next=top;
top=p;
}
void Pop(ElemSet<T>& e)
{
if(!top) throw "当前栈为空";
e=*top;
ElemSet<T>* p=top;
top=top->next;
delete p;
}
void StackTraverse() //从栈底到栈顶依次对S的每个数据元素调用函数visit()
{
if(!top) throw "当前栈为空";
Stack<ElemSet<T>*> t;
for(ElemSet<T>* p=top;p;p=p->next)
t.Push(p);
ElemSet<ElemSet<T>*> te;
for(t.Pop(te);!t.StackEmpty();t.Pop(te))
{
te.read()->visit();
cout<<' ';
}
te.read()->visit();
cout<<endl;
}
};

#endif

热心网友 时间:2024-11-02 04:22

#include<iostream>
#define ok 1
#define error 0
using namespace std;
typedef int status;
typedef struct Lnode
{
float coef;
int expn;
struct Lnode*next;
}*link;
typedef struct
{
link head,tail;
int len;
}polynomail;
extern status makenode(link&p,int e,float f)
{
p=(link)malloc(sizeof(Lnode));
if(!p)return error;
p->coef=f;
p->expn=e;
return ok;
}//生成一个节点
void freenode(link &p){free(p);}//释放一个节点
status initlist(polynomail &p)
{
p.head=p.tail=NULL;
p.len=0;
return ok;
}//初始化一个线性表
extern status insfirst(polynomail &p,link h,link s)
{
s=(link)malloc(sizeof(Lnode));if(!s)return error;
h=(link)malloc(sizeof(Lnode));if(!h)return error;
h=p.head;
s->next=h->next;
h->next=s;
return ok;
}//已知h指向线性表的头结点,将s所指节点插入在第一个节点之前
extern status delfirst(polynomail &p,link h,link &q)
{
q=(link)malloc(sizeof(Lnode));if(!q)return error;
h=(link)malloc(sizeof(Lnode));if(!h)return error;
h=p.head;
q=h->next;
h->next=q->next;
free(q);
return ok;
}//删除线性表第一个节点
extern status append(polynomail &p,link s)
{
p.tail->next=s;
while(s->next){s=s->next;}
p.tail=s;
return ok;
}//将指针s所指的一串节点链接在线性表的末尾,并改变表的尾指针
extern status remove(polynomail &p,link &q)
{
link s;
s=(link)malloc(sizeof(Lnode));
if(!s)return error;
s=p.head;
while(s->next!=p.tail){s=s->next;}
q=p.tail;
free(q);
p.tail=s;
return ok;
}//删除尾节点并以q返回,然后改变链表尾指针的指向
extern status insbefore(polynomail &p,link &q,link s)
{
link h;
s=(link)malloc(sizeof(Lnode));if(!s)return error;
h=(link)malloc(sizeof(Lnode));if(!h)return error;
h=p.head;
while(h->next!=q){h=h->next;}
s->next=q;
h->next=s;
q=s;
return ok;
}//在q所指节点之前插入一个节点
extern status insafter(polynomail &l,link &p,link s)
{
s->next=p->next;
p->next=s;
p=s;
return ok;
}//在P所指节点之后插入一个节点
extern status SetCurElem(link &p,int e,float f)
{
p->coef=f;
p->expn=e;
return ok;
}//设置节点的值
extern link GetCurElem(link p)
{
return p;
}//返回一个指向节点的指针
extern status listempty(polynomail p)
{
if(p.head==p.tail)
return ok;
else return error;
}//判断线性表是否为空
extern int listlength(polynomail p)
{
return(p.tail-p.head+1);
}//求线性表的长度
extern link GetHead(polynomail p)
{
return p.head;
}
extern link GetTail(polynomail p)
{
return p.tail;
}
extern link priorpos(polynomail l,link p)
{
link q;
q=(link)malloc(sizeof(Lnode));if(!q)return error;
q=l.head;
while(q->next!=p){q=q->next;}
if(q->next==p)return q;
else return NULL;
}//返回p节点前驱的位置
extern link nextpos(polynomail l,link p)
{
link q;
q=(link)malloc(sizeof(Lnode));if(!q)return error;
if(!p->next)return NULL;
return p->next;
}//返回p节点后继的位置
extern status locatepos(polynomail l,int i,link &p)
{
link q;
q=(link)malloc(sizeof(Lnode));if(!q)return error;
q=l.head->next;
int j=1;
while(q&&j<i)
{
q=q->next;
j++;
}
p=q;
if(!q||j>i)return error;
else return ok;
}//返回p所指的第i项的地址
extern int compare(link p,link q)
{
if(p->expn>q->expn)return 1;
if(p->expn<q->expn)return -1;
if(p->expn==q->expn)return 0;
}
extern link locateelem(polynomail p,link m,link &q,link &h,int(*cmp)(link a,link b))
{
q=(link)malloc(sizeof(Lnode));if(!q)return error;
h=(link)malloc(sizeof(Lnode));if(!h)return error;
cmp=compare;//函数指针的赋值
for(q=p.head->next;q->next!=NULL;q=q->next)
{
int n; n=(*cmp)(m,q);
if(n==0)
{
return q;
}
}
for(h=p.head->next;h->next!=NULL;h=h->next)
{
int n; n=(*cmp)(m,h);
if(n>0)
{
return priorpos(p,h);
}
}
}

热心网友 时间:2024-11-02 04:23

#include<iostream>
#define ok 1
#define error 0
using namespace std;
typedef int status;
typedef struct Lnode
{
float coef;
int expn;
struct Lnode*next;
}*link;
typedef struct
{
link head,tail;
int len;
}polynomail;
extern status makenode(link&p,int e,float f)
{
p=(link)malloc(sizeof(Lnode));
if(!p)return error;
p->coef=f;
p->expn=e;
return ok;
}//生成一个节点
void freenode(link &p){free(p);}//释放一个节点
status initlist(polynomail &p)
{
p.head=p.tail=NULL;
p.len=0;
return ok;
}//初始化一个线性表
extern status insfirst(polynomail &p,link h,link s)
{

热心网友 时间:2024-11-02 04:23

抽象数据类型 函数实现在子类中
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
压轴是倒数第二的意思吗? DNF 感觉资料站的喷人的真是好多 大料是什么(大料是什么东西) 大哥户口迁出去二十多年现在还可可以享受到农田补贴吗? 不是本村人农田可以确权吗 农村户口转出老家农田可以种吗 丈夫去世后自动离婚吗? 光遇注销怎么申诉 旧的不去新的不来怎么造句 我想找一张恶魔的图片当纹身.天使类型的.3对翅膀都是张开的.手也是张开... 抽象数据类型有什么用?怎么用?175 结合线性表的学习,谈谈抽象数据类型定义、表示与实现之间的关系3 德国,德国人 ,德语用英语怎么说120 游戏王十星以上怪11 游戏王3000攻以上的怪兽有哪些?2 游戏王卡攻防2000 4星怪有什么? 游戏王攻击力1900(包括1900)以上四星通常(无效果)怪...21 游戏王4星以下攻击最高的通常怪兽3 游戏王攻击力2000的通常怪兽1 《重生之妖娆人生》最新txt全集下载4 乙肝大三阳有哪些常见并发症 信息化战争的基本特征35 最近电脑总是莫名其妙的和无线路由断开网络,然后网络连接那里就... 得了乙肝还有并发症吗? 乙肝会引起哪些并发症?9 军人需要什么样的精神48 求几付去体内湿气的中药方子32 乙肝有什么症状和并发症 第三次军事变革浪潮大约经历了400年吗?33 乙肝有什么并发征? 双十二母婴好物你知道吗? 在派出所监视居住到检察院批捕还会羁押吗 《重生之升级人生》最新txt全集下载1 看着乐观,实际上最容易抑郁三个星座是谁? 玉林马路圩科三考试 已被监视居住期满后还会再次拘捕吗 在玉林马路圩考科目四需要带什么? 广西玉林车马路圩考B2牌9选6就是那么难吗?有没有人帮出出主... 母亲的眼泪阅读题及答案308 信用卡一年没还了 能补办身份证吗1 《母亲的眼泪》 阅读题答案。139 去法院拿刑事案件起诉书会被关押吗?诈骗罪,监视居住阶段。 信用卡逾期了一年多了,身份证过期了能补办吗??3 母亲的眼泪阅读答案 张24 信用卡欠款二十几万。一年多没还了,身份证丢了,异地补办的话会...2 我欠信用卡两年还不上了。现在身份证掉了去补办身份证的花会被抓...2 下列哪种中药具有清热解毒的功效?2 《母亲的眼泪》文章充满了深情,很感动,那么贯穿文章的感情是什...14 我犯寻衅滋事罪 监视居住不到4个月 今天检察院叫我把身份证带去,把我... 无限宝直播课老师能看到学生吗?1