数据结构代码汇总范本模板.docx

上传人:b****6 文档编号:7898623 上传时间:2023-01-27 格式:DOCX 页数:23 大小:24.11KB
下载 相关 举报
数据结构代码汇总范本模板.docx_第1页
第1页 / 共23页
数据结构代码汇总范本模板.docx_第2页
第2页 / 共23页
数据结构代码汇总范本模板.docx_第3页
第3页 / 共23页
数据结构代码汇总范本模板.docx_第4页
第4页 / 共23页
数据结构代码汇总范本模板.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

数据结构代码汇总范本模板.docx

《数据结构代码汇总范本模板.docx》由会员分享,可在线阅读,更多相关《数据结构代码汇总范本模板.docx(23页珍藏版)》请在冰豆网上搜索。

数据结构代码汇总范本模板.docx

数据结构代码汇总范本模板

实验一/*******************线性表的应用*******************/

#include

h>

intn,j,k;//声明结构体类型,并确定其成员变量

typedefstructstudent{

charname[20];

charsex[5];

intage;

longlongtel;

longqq;

charemail[50];

}person;

persona[100];//输入链表的个人信息

voidcreat(intn){

if(n>100){

printf(”超出划定内存");//判断所存个人信息是否超出内存

}

else

inti=0;

for(inti=0;i

printf(”依次输入姓名性别年龄电话号码QQ号Email地址(回车键隔开)”);

inputname();//输入姓名

inputtel();//输入电话

scanf("%d”,a[i]。

age);

scanf(”%d”,a[i]。

tel);

scanf(”%d",a[i].qq);

inputemail();//输入email地址

}

//输入第i个成员数据

voidshuru(inti){

printf("依次输入姓名性别年龄电话号码QQ号Email地址(回车键隔开)”);

inputname();//输入姓名

inputtel();//输入电话

scanf(”%d”,a[i]。

age);

scanf("%d",a[i].tel);

scanf(”%d”,a[i]。

qq);

inputemail();//输入email地址

}

//创建一个姓名输入方法

voidinputname(){

charname[20];

for(inti=0;i<20;i++)

scanf("%c",&name[i]);

}

//创建电话输入方法

voidinputtel(){

chartel[15];

for(inti=0;i〈15;i++)

scanf(”%c",&tel[i]);

//创建email输入方法

voidinputemail(){

charemail[50];

for(inti=0;i〈50;i++)

scanf(”%c",&email[i]);

}

//插入方法插入第i个成员

voidinsert(intk){

if(k<0||k〉100)printf(”插入位置错”);

else{

inti;

for(i=n;i

a[i+1]=a[i];

shuru(k);

}

//删除第k个元素

voiddele(intk){

if(k〈0||k〉n)printf(”删除位置不存在");

else

for(intk;k

a[k]=a[k+1];

voidmain(){

printf("输入您将输入的通讯录人数");

scanf(”%d”,&n);

creat(n);

printf(”输入插入的位置");

scanf(”%d",&k);

insert(k);

printf("输入删除的位置");

scanf(”%d”,&j);

creat(j);

}

实验二/********************求哈夫曼编码********************/

#include〈stdio。

h〉

#include

h>

#include〈stdlib.h>

usingnamespacestd;

#definen5

#definem2*n—1

#defineinfinity32727

structHTNode{

unsignedintweight;

unsignedintplink,rlink,llink;

}*HuffmanTree;

structcodetype{

intstart;

charbits[n+1];

};

structelement{

charsymbol;

codetypecode;

};

structelementtable[n+1];

inlinevoidselect(structHTNodeht[2*n],ints,int&x1,int&x2)

inti;

floatv1,v2;

v1=v2=infinity;

x1=x2=0;

for(i=1;i〈=s;i++)

if(ht[i].plink==0)

if(ht[i]。

weight〈v1)

v2=v1;

x2=x1;

v1=ht[i]。

weight;

x1=i;

elseif(ht[i].weight

v2=ht[i].weight;

x2=i;

}

voidset_huffmantree(structHTNodeht[2*n])

inlinevoidselect(structHTNodeht[2*n],ints,int&x1,int&x2);

inti;

ints1,s2;

for(i=1;i<=m;++i)

ht[i]。

plink=ht[i]。

llink=ht[i]。

rlink=0;

for(i=n+1;i〈=m;++i)

{//建哈夫曼树

select(ht,i—1,s1,s2);

//在ht[k](1〈=k<=i-1)中选择两个双亲域为零的最小的

//结点:

s1和s2(s1和s2为最小值所在的下标)

ht[s1].plink=ht[s2].plink=i;

ht[i]。

llink=s1;

ht[i].rlink=s2;

ht[i]。

weight=ht[s1].weight+ht[s2]。

weight;

}

voidsethufcode(structHTNodeht[2*n])

structHTNode*p=ht;

voidset_huffmantree(structHTNodeht[2*n]);

inti,s,f;

codetypec;

for(i=1;i〈=n;i++)

printf("请输入字符:

");

scanf(”%s”,&table[i].symbol);

printf("请输入相应的权值:

");

scanf(”%d",&ht[i].weight);

}

set_huffmantree(p);

for(i=1;i<=n;i++)

c.start=n+1;

s=i;

f=ht[s].plink;

do{

c。

start——;

if(s==ht[f]。

llink)

c。

bits[c.start]='0’;

else

c.bits[c。

start]='1’;

s=f;

f=ht[s].plink;

}while(f);

table[i].code=c;

}

}

voidOutHuffmanTree(structHTNodeht[2*n])

{

inti,j;

codetypec;

for(i=1;i<=n;i++)

printf(”\n%c",table[i].symbol);

c=table[i]。

code;

for(j=c.start;j<=n;j++)

printf("%c",c。

bits[j]);

intmain()

{

structHTNodeHT[2*n];

voidOutHuffmanTree(structHTNodeht[2*n]);

voidsethufcode(structHTNodeht[2*n]);

sethufcode(HT);

OutHuffmanTree(HT);

printf("\n");

return0;

实验三/*********************最短路径*******************/

#include”stdio。

h"

#include"stdlib.h"

#include"io。

h"

#include"math.h”

#include"time.h"

#defineOK1

#defineERROR0

#defineTRUE1

#defineFALSE0

#defineMAXEDGE20

#defineMAXVEX20

#defineINFINITY65535

typedefintStatus;/*Status是函数的类型,其值是函数结果状态代码,如OK等*/

typedefstruct

{

intvexs[MAXVEX];

intarc[MAXVEX][MAXVEX];

intnumVertexes,numEdges;

}MGraph;

typedefintPatharc[MAXVEX][MAXVEX];

typedefintShortPathTable[MAXVEX][MAXVEX];

/*构件图*/

voidCreateMGraph(MGraph*G)

{

inti,j;

/*printf("请输入边数和顶点数:

");*/

G—>numEdges=8;

G->numVertexes=5;

for(i=0;i

{

G—>vexs[i]=i;

for(i=0;i〈G—>numVertexes;i++)/*初始化图*/

{

for(j=0;jnumVertexes;j++)

if(i==j)

G—〉arc[i][j]=0;

else

G—>arc[i][j]=G—>arc[j][i]=INFINITY;

}

G—>arc[0][1]=3;

G-〉arc[0][4]=30;

G—>arc[1][2]=25;

G-〉arc[1][3]=8;

G->arc[2][4]=10;

G-〉arc[3][4]=12;

G->arc[3][0]=20;

G—>arc[3][2]=4;

G—〉arc[4][0]=15;

for(i=0;i〈G—>numVertexes;i++)

{

for(j=i;j〈G->numVertexes;j++)

{

G-〉arc[j][i]=G—〉arc[i][j];

}

//Floyd算法,求网图G中各顶点v到其余顶点w的最短路径P[v][w]及带权长度D[v][w]。

voidShortestPath_Floyd(MGraphG,Patharc*P,ShortPathTable*D)

{

intv,w,k;

for(v=0;v

numVertexes;++v)/*初始化D与P*/

{

for(w=0;w〈G。

numVertexes;++w)

{

(*D)[v][w]=G。

arc[v][w];/*D[v][w]值即为对应点间的权值*/

(*P)[v][w]=w;/*初始化P*/

}

for(k=0;k

numVertexes;++k)

{

for(v=0;v〈G。

numVertexes;++v)

{

for(w=0;w

numVertexes;++w)

if((*D)[v][w]〉(*D)[v][k]+(*D)[k][w])

{/*如果经过下标为k顶点路径比原两点间路径更短*/

(*D)[v][w]=(*D)[v][k]+(*D)[k][w];/*将当前两点间权值设为更小的一个*/

(*P)[v][w]=(*P)[v][k];/*路径设置为经过下标为k的顶点*/

}

intmain(void)

intv,w,k;

MGraphG;

PatharcP;

ShortPathTableD;/*求某点到其余各点的最短路径*/

CreateMGraph(&G);

ShortestPath_Floyd(G,&P,&D);

printf(”各顶点间最短路径如下:

\n”);

for(v=0;v

for(w=v+1;w〈G.numVertexes;w++)

{

printf(”v%d-v%dweight:

%d",v,w,D[v][w]);

k=P[v][w];/*获得第一个路径顶点下标*/

printf(”path:

%d”,v);/*打印源点*/

while(k!

=w)/*如果路径顶点下标不是终点*/

printf("—>%d",k);/*打印路径顶点*/

k=P[k][w];/*获得下一个路径顶点下标*/

}

printf("-〉%d\n”,w);/*打印终点*/

}

printf(”\n”);

}

printf("最短路径D\n");

for(v=0;v〈G。

numVertexes;++v)

{

for(w=0;w〈G。

numVertexes;++w)

printf("%d\t”,D[v][w]);

}

printf("\n”);

printf(”最短路径P\n”);

for(v=0;v〈G.numVertexes;++v)

for(w=0;w〈G。

numVertexes;++w)

{

printf("%d",P[v][w]);

printf("\n");

return0;

}

/******************关键路径*******************/

#include"stdio.h"

#include"stdlib.h"

#include”io。

h”

#include"math。

h"

#include”time.h”

#defineOK1

#defineERROR0

#defineTRUE1

#defineFALSE0

#defineMAXEDGE30

#defineMAXVEX30

#defineINFINITY65535

typedefintStatus;/*Status是函数的类型,其值是函数结果状态代码,如OK等*/

int*etv,*ltv;/*事件最早发生时间和最迟发生时间数组,全局变量*/

int*stack2;/*用于存储拓扑序列的栈*/

inttop2;/*用于stack2的指针*/

/*邻接矩阵结构*/

typedefstruct

{

intvexs[MAXVEX];

intarc[MAXVEX][MAXVEX];

intnumVertexes,numEdges;

}MGraph;

/*邻接表结构*******************/

typedefstructEdgeNode/*边表结点*/

intadjvex;/*邻接点域,存储该顶点对应的下标*/

intweight;/*用于存储权值,对于非网图可以不需要*/

structEdgeNode*next;/*链域,指向下一个邻接点*/

}EdgeNode;

typedefstructVertexNode/*顶点表结点*/

{

intin;/*顶点入度*/

intdata;/*顶点域,存储顶点信息*/

EdgeNode*firstedge;/*边表头指针*/

}VertexNode,AdjList[MAXVEX];

typedefstruct

AdjListadjList;

intnumVertexes,numEdges;/*图中当前顶点数和边数*/

}graphAdjList,*GraphAdjList;

/******************************/

voidCreateMGraph(MGraph*G)/*构建图*/

{

inti,j;

/*printf(”请输入边数和顶点数:

");*/

G-〉numEdges=11;

G-〉numVertexes=8;

for(i=0;i〈G->numVertexes;i++)/*初始化图*/

G->vexs[i]=i;

for(i=0;i〈G-〉numVertexes;i++)/*初始化图*/

{

for(j=0;jnumVertexes;j++)

{

if(i==j)

G-〉arc[i][j]=0;

else

G-〉arc[i][j]=INFINITY;

}

}

G—>arc[0][1]=6;G—〉arc[0][2]=4;G—〉arc[0][3]=5;

G—〉arc[1][4]=1;G—>arc[2][4]=1;G—>arc[3][5]=2;

G-〉arc[4][6]=9;G—>arc[4][7]=7;G-〉arc[5][7]=4;

G—>arc[6][8]=2;G->arc[7][8]=4;

/*利用邻接矩阵构建邻接表*/

voidCreateALGraph(MGraphG,GraphAdjList*GL)

inti,j;

EdgeNode*e;

*GL=(GraphAdjList)malloc(sizeof(graphAdjList));

(*GL)->numVertexes=G.numVertexes;

(*GL)->numEdges=G。

numEdges;

for(i=0;i〈G。

numVertexes;i++)/*读入顶点信息,建立顶点表*/

(*GL)—〉adjList[i]。

in=0;

(*GL)—〉adjList[i]。

data=G.vexs[i];

(*GL)->adjList[i]。

firstedge=NULL;/*将边表置为空表*/

for(i=0;i

numVertexes;i++)/*建立边表*/

for(j=0;j〈G.numVertexes;j++)

{

if(G.arc[i][j]!

=0&&G.arc[i][j]〈INFINITY)

{

e=(EdgeNode*)malloc(sizeof(EdgeNode));

e—〉adjvex=j;/*邻接序号为j*/

e—〉weight=G。

arc[i][j];

e->next=(*GL)->adjList[i].firstedge;/*将当前顶点上的指向的结点指针赋值给e*/

(*GL)->adjList[i]。

firstedge=e;/*将当前顶点的指针指向e*/

(*GL)—〉adjList[j].in++;

}

}

}

/*拓扑排序*/

StatusTopologicalSort(GraphAdjListGL)

{/*若GL无回路,则输出拓扑排序序列并返回1,若有回路返回0。

*/

EdgeNode*e;

inti,k,gettop;

inttop=0;/*用于栈指针下标*/

intcount=0;/*用于统计输出顶点的个数*/

int*stack;/*建栈将入度为0的顶点入栈*/

stack=(int*)malloc(GL->numVertexes*sizeof(int));

for(i=0;inumVertexes;i++)

if(0==GL—>adjList[i].in)/*将入度为0的顶点入栈*/

stack[++top]=i;top2=0;

etv=(int*)malloc(GL-〉numVertexes*sizeof(int));/*事件最早发生时间数组*/

for(i=0;i〈GL->numVertexes;i++)

etv[i]=0;/*初始化*/

stack2=(int*)malloc(GL-〉numVertexes*sizeof(int));/*初始化拓扑序列栈*/

printf(”TopologicalSort:

\t");

while(top!

=0)

gettop=stack[top——];

printf("%d—〉”,GL—>adjList[gettop].data);

count++;/*输出i号顶点,并计数*/

stack2[++top2]=gettop;/*将弹出的顶点序号压入拓扑序列的栈*/

for(e=GL—〉adjList[gettop]。

firstedge;e;e=e—〉next)

{

k=e—>adjvex;

if(!

(——GL-〉adjList[k].in))/*将i号顶点的邻接点的入度减1,如果减1后为0,则入栈*/

stack[++top]=k;

if((etv[gettop]+e->weight)>etv[k])/*求各顶点事件的最早发生时间etv值*/

etv[k]=etv[gettop]+

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1