您的位置:首页 > 其它

第三周项目2-建设“顺序表”算法库 补1

2017-10-14 23:14 393 查看
这次的代码是补充上一次“建立顺序表”。

代码:

#include <iostream>
#include "kudetouwenjian.h"
using namespace std;
int main()
{
linknode *L;
A a[5]={1,2,3,4,5};
A e=0;
A f=4;
int b=3;
int n=5;
toucha(L,a,n);
shuchu(L);
cout<<"-----------------"<<endl;
cout<<"长度为:"<<changdu(L)<<endl;
cout<<"-----------------"<<endl;
chushihua(L);
shuchu(L);
cout<<endl;
cout<<"-----------------"<<endl;
cout<<panduanshifouweikong(L)<<endl;
cout<<"-----------------"<<endl;
weicha(L,a,n);
shuchu(L);
cout<<"-----------------"<<endl;
cout<<"顺序查找,如果没有,输出0;如果有,先输出1,在输出查找到的单位。"<<endl;
shunxuchazhao(L,b,e);
cout<<e<<endl;
cout<<"-----------------"<<endl;
cout<<"查找指定的单个,如果没有,输出0;如果有,输出第一个。"<<chazhaodange(L,f)<<endl;
cout<<"-----------------"<<endl;
cuihui(L);
shuchu(L);
cout<<endl;
cout<<"-----------------"<<endl;
return 0;
}
#include <iostream>
#include <malloc.h>
#include "kudetouwenjian.h"
using namespace std;
void toucha(linknode *&L,A a[],int n)
{
linknode *s;
L=(linknode *)malloc(sizeof(linknode));
L->next=NULL;
for(int i=0;i<n;i++)
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=a[i];
s->next=L->next;
L->next=s;
}
}
void shuchu(linknode *L)
{
linknode *p=L->next;
while(p!=NULL)
{
cout<<p->shuju<<endl;
p=p->next;
}
}
void weicha(linknode *&L,A a[],int n)
{
linknode *s,*r;
L=(linknode *)malloc(sizeof(linknode));
r=L;
for(int i=0;i<n;i++)
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void chushihua(linknode *&L)
{
L=(linknode *)malloc(sizeof(linknode));
L->next=NULL;
}
void cuihui(linknode *&L)
{
linknode * pre=L,* p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
L->next=NULL;
}
bool panduanshifouweikong(linknode *L)
{
return(L->next==NULL);
}
int changdu(linknode *L)
{
int e=0;
linknode *p=L->next;
while(p!=NULL)
{
e++;
p=p->next;
}
return(e);
}
bool shunxuchazhao(linknode *L,int i,A &e)
{
int j=1;
linknode *p=L->next;
if(i<=0)return(false);
while(j<i)
{
j++;
p=p->next;
}
if(p==NULL)
return(false);
else
{
e=p->shuju;
return(true);
}
}
int chazhaodange(linknode *L,A e)
{
int j=1;
linknode *p=L->next;
if(p==NULL)return(false);
while(p!=NULL&&e!=p->shuju)
{
p=p->next;
j++;
}
if(p==NULL)
return(0);
else
return(j);
}
#ifndef KUDETOUWENJIAN_H_INCLUDED
#define KUDETOUWENJIAN_H_INCLUDED
#include <iostream>
typedef int A;
typedef struct lianbiao
{
A shuju;
struct lianbiao *next;
}linknode;
void toucha(linknode *&L,A a[],int n);
void shuchu(linknode *L);
void weicha(linknode *&L,A a[],int n);
void chushihua(linknode *&L);
void cuihui(linknode *&L);
bool panduanshifouweikong(linknode *L);
int changdu(linknode *L);
bool shunxuchazhao(linknode *L,int i,A &e);
int chazhaodange(linknode *L,A e);
#endif // KUDETOUWENJIAN_H_INCLUDED

运行图:




总结:

这次只是补充上次的算法库,以后还会继续补充。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: