数据结构实验2Word文档下载推荐.docx
- 文档编号:18994267
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:36
- 大小:22.71KB
数据结构实验2Word文档下载推荐.docx
《数据结构实验2Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《数据结构实验2Word文档下载推荐.docx(36页珍藏版)》请在冰豆网上搜索。
}//GetElem_L
StatusListInsert_L(LinkListL,inti,ElemTypee){
LinkListp,s;
intj;
p=L;
j=0;
j<
i-1){
}
if(!
p||j>
i-1)returnERROR;
s=(LinkList)malloc(sizeof(LNode));
s->
data=e;
next=p->
p->
next=s;
}//LinstInsert_L
StatusListDelete_L(LinkListL,inti,ElemType*e){
LinkListp,q;
while(p->
next&
if(!
(p->
next)||j>
q=p->
next=q->
*e=q->
free(q);
}//ListDelete_L
voidCreateList_L(LinkList*L,intn){
inti;
*L=(LinkList)malloc(sizeof(LNode));
(*L)->
next=NULL;
for(i=n;
i>
0;
--i){
p=(LinkList)malloc(sizeof(LNode));
data=i;
next=(*L)->
next=p;
}//CreateList_L
voidmain()
{
LinkListL;
ElemTypee;
CreateList_L(&
L,5);
print_L(L);
GetElem_L(L,3,&
e);
ListInsert_L(L,5,79);
ListDelete_L(L,5,&
2-SqStack
#defineOVERFLOW-2
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
typedefintSElemType;
typedefstruct{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
StatusInitStack(SqStack*S){
S->
base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!
S->
base)exit(OVERFLOW);
top=S->
base;
stacksize=STACK_INIT_SIZE;
}//InitStack
StatusDestroyStack(SqStack*S){
base)returnERROR;
free(S->
base);
}//DestroyStack
StatusClearStack(SqStack*S){
if(S->
top!
=S->
base){
S->
}//ClearStack
StatusGetTop(SqStackS,SElemType*e){
if(S.top==S.base)returnERROR;
*e=*(S.top-1);
}//GetTop
StatusStackEmpty(SqStackS){
if(S.top==S.base)
returnTRUE;
else
returnFALSE;
intStackLength(SqStackS){
returnS.top-S.base;
StatusPush(SqStack*S,SElemTypee){
top-S->
base>
stacksize){
S->
base=(SElemType*)realloc(S->
base,(S->
stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!
base+S->
stacksize;
stacksize+=STACKINCREMENT;
*(S->
top++)=e;
}//Push
StatusPop(SqStack*S,SElemType*e){
if(S->
top==S->
*e=*(--S->
top);
}//Pop
voidconvert(intnum,intdig){
SqStackS;
charc;
intd;
InitStack(&
S);
while(num!
=0){
d=num%dig;
Push(&
S,d);
num/=dig;
while(!
StackEmpty(S)){
Pop(&
S,&
d);
if(d<
10)
printf("
%d"
d);
else{
c='
A'
+(d-10);
%c"
c);
}
\n"
voidmain(){
convert(1388,16);
3-BiTree
typedefstructbitnode{
chardata;
structbitnode*lchild;
structbitnode*rchild;
}BiTNode,*Bitree;
intk=0;
voidOrder_leafnum(BitreeT){
if(T==NULL)return;
if(T->
lchild==NULL&
T->
rchild==NULL){
printf("
%3c"
T->
k++;
Order_leafnum(T->
lchild);
rchild);
voidpreOrder(BitreeT)
if(T){
printf("
preOrder(T->
voidinOrder(BitreeT)
inOrder(T->
voidproOrder(BitreeT)
proOrder(T->
voidCreate(Bitree*T)
charch;
ch=getchar();
if(ch=='
@'
)*T=NULL;
else
*T=(Bitree)malloc(sizeof(BiTNode));
(*T)->
data=ch;
Create(&
voidExchange_BiTree(Bitreebt){
Bitreep;
if(bt){
p=bt->
lchild;
bt->
lchild=bt->
rchild;
rchild=p;
Exchange_BiTree(bt->
BitreeT;
Create(&
T);
Order_leafnum(T);
\nleafnum=%d"
k);
preOrder(T);
inOrder(T);
proOrder(T);
Exchange_BiTree(T);
4-ALGraph
#include<
#definemax50
typedefintvextype;
typedefstructArcNode{
intadjvex;
structArcNode*nextarc;
}ArcNode,*ArcPtr;
typedefstruct{
vextypedata;
ArcNode*firstarc;
}VNode,AdjList[max];
VNodevertices[max];
intvexnum;
intarcnum;
}ALGraph;
voidCreat_AG(ALGraph*AG)
{//输入顶点和边的信息,建立图的邻接表
ArcPtrp;
inti,j,k;
\ninputvexnum:
"
scanf("
&
AG->
vexnum);
\ninputarcnum:
arcnum);
for(i=1;
i<
=AG->
vexnum;
++i){//初始化
\ninput%dthvex(char):
i);
scanf("
vertices[i].data);
getchar();
AG->
vertices[i].firstarc=NULL;
for(k=1;
k<
arcnum;
k++){//输入边信息,建立邻接表,无向无权图
\ninput%dtharci(int)j(int):
%d%d"
i,&
j);
/*在第i个链表上插入序号为j的表结点*/
p=(ArcPtr)malloc(sizeof(ArcNode));
p->
adjvex=j;
nextarc=AG->
vertices[i].firstarc;
vertices[i].firstarc=p;
/*在第j个链表上插入序号为i的表结点*/
adjvex=i;
vertices[j].firstarc;
vertices[j].firstarc=p;
intvisited[50];
voidBFS(ALGraphG,inti){
intQ[50];
intrear,front;
rear=0;
front=0;
Q[rear++]=1;
while(front!
=rear){
i=Q[front++];
G.vertices[i].data);
for(p=G.vertices[i].firstarc;
nextarc){
j=p->
adjvex;
if(visited[j]==0){
Q[rear++]=j;
visited[j]=1;
voidTraverse(ALGraphG)
=G.vexnum;
i++)visited[i]=0;
//访问标志数组初始化
i++)
if(visited[i]==0)BFS(G,i);
//对未访问的顶点调用BFS
//intvisited[50];
//voidDFS(ALGraphG,inti){
//intj;
//ArcPtrp;
//visited[i]=1;
//printf("
%3d"
//for(p=G.vertices[i].firstarc;
//p;
nextarc)
//{
//j=p->
//if(!
visited[j])DFS(G,j);
//}
//Indegree[k]--j;
//if(indegree[k]==0)s[top++]=k;
//}
//voidTraverse(ALGraphG)
//{
//inti;
//for(i=1;
//visited[i]=0;
visited[i])DFS(G,i);
intTopologicalSort(ALGraphG){//拓扑排序
ints[50];
inttop;
inti,j,k;
intcount;
intindegree[50];
indegree[i]=0;
p!
=NULL;
indegree[j]++;
top=0;
for(i=1;
if(indegree[i]==0){
s[top++]=i;
count=0;
while(top!
i=s[--top];
count++;
for(p=G.vertices[i].firstarc;
{
k=p->
indegree[k]--;
if(indegree[k]==0)s[top++]=k;
if(count==G.vexnum)
return1;
else
return0;
intvisited[50];
voidDFS(ALGraphG,inti)
{intj;
ArcPtrp;
visited[i]=1;
G.vertices[i].vexdata);
for(p=G.vertices[1].firstarc;
j=p->
if(!
visited[j])DFS(G,j)}
voidBFS(ALGraphG,intv)
intQ[30],i,j;
intrear=0,front=0;
%c\t"
G.vertices[v].vexdata);
visited[v]=1;
rear++;
Q[rear]=v;
while(rear!
=front){
front++;
i=Q[front];
visited[i])
%ct"
G.vertices[j].vexdata)}
visited[j]=1;
Q[rear]=j}}}
ALGraphG;
Creat_AG(&
G);
//DFS(G,1);
//Traverse(G);
BFS(G,1);
Traverse(G);
TopologicalSort(G);
5-Search_seq
#definemax20
ElemTypeelem[max];
intlength;
}SSTable;
intSearch_seq0(SSTableST,ElemTypekey)
i=ST.length;
while(ST.elem[i]!
=key&
i>
0)i--;
returni;
intSearch_seq1(SSTableST,ElemTypekey)
ST.elem[0]=key;
=key)i--;
intSearch_seq2(SSTableST,ElemTypekey)
ST.elem[ST.length+1]=key;
i=1;
=key)i++;
intSearch_Bin_d(SSTableST,ElemTypekey,intlow,inthigh){
intmid;
low=1;
high=ST.length;
if(low<
=high)
mid=(low+high)/2;
if(key==ST.elem[mid])returnmid;
elseif(key<
ST.elem[mid])
return(Search_Bin_d(ST,key,low,mid-1));
return(Search_Bin_d(ST,key,mid+1,high));
elsereturn0;
intSearch_Bin(SSTableST,ElemTypekey){
intlow;
inthigh;
intmid;
while(low<
ST.elem[mid])high=mid-1;
elselow=mid+1;
return0;
voidCreateList__Sq(SSTable*ST,intn){
\n请输入%d个整数!
n);
for(i=1;
i<
n;
i++){scanf("
ST->
elem[i]);
ST->
length=n;
}
SSTableST,STy;
CreateList__Sq(&
ST,7);
inti=Search_seq2(ST,65);
STy,7);
intj=Search_Bin(STy,76);
j);
j=Search_Bin_d(STy,76,1,7);
6-Insert
#definemaxsize20
typedefintKeyType;
KeyTypekey;
}RedType;
RedTyper[maxsize+1];
}Sqlist;
inti;
voidCreateList__Sq(Sqlist*
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验
