| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 15815 人关注过本帖, 6 人收藏
标题:本学期我学习数据结构的一些练习,现在发上来,希望能给初学者带来帮助
只看楼主 加入收藏
樱桃小新
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2005-10-26
收藏
得分:0 

这个是用链式队列辅助建立的二叉树和中序递归遍历

#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define Max_Size 25
#define Null 0
#define Link 0
#define Thread 1

typedef int Tree_Data;

struct Tree {
Tree *LChild,*RChild;
int LTag ,RTag;
Tree_Data Value;
};//二叉树结点

typedef Tree Data;
struct Q_Data
{
Data *e;
Q_Data *Next;
};//定义链队列节点数据

typedef struct
{
struct Q_Data *Head,*Tail;
int Lengh;
}Chain_Q;//定义链队列指针

int Create_Chain_Q(Chain_Q &List)
{
List.Head = (Q_Data *)malloc(sizeof(Q_Data));
List.Tail = List.Head;
List.Head->Next = Null;
List.Lengh = 0;
return 1;
}//建立队列

int In_Chain_Q(Chain_Q &List, Data **Q_Value)
{
struct Q_Data *Data_1;
for (Data_1=List.Head;Data_1->Next!=Null;Data_1=Data_1->Next);

Data_1->Next = (Q_Data *)malloc(sizeof(Q_Data));
Data_1->Next->Next= Null;
List.Tail = Data_1->Next;
List.Tail->e = *Q_Value;
return 1;
}//入队

int Out_Chain_Q(Chain_Q &List, Data **Q_Value)
{
struct Q_Data *Data_1;
if (List.Head->Next == Null)return 0;
Data_1=List.Head->Next;
List.Head->Next=List.Head->Next->Next;

*Q_Value = Data_1->e;
free(Data_1);
return 1;
}//出队



int Opera(Tree &Tree_Data)
{
printf("Data=%d,",Tree_Data.Value);
printf("O=%d,",&Tree_Data);
printf("LChild=%d,",Tree_Data.LChild);
if (Tree_Data.LTag== 1)printf("LTag=%d,",Tree_Data.LTag);
printf("RChild=%d,",Tree_Data.RChild);
if (Tree_Data.RTag== 1)printf("RTag=%d,",Tree_Data.RTag);
printf("\n");
return 1;}//操作节点

int Pre_Order(Tree *List , int T)
{
if (List!=Null && T!=Thread)
{
Opera(*List);
if (Pre_Order(List->LChild,List->LTag)==1)if (Pre_Order(List->RChild,List->RTag)==1)return 1;
}
else return 1;
}//先序遍历二叉树


int Mid_Order(Tree *List,int T)
{
if (List!=Null && T!=Thread)
{ if (Mid_Order(List->LChild,List->LTag)==1)
{
Opera(*List);
Mid_Order(List->RChild,List->RTag);
return 1;
}
}
else return 1;
}//中序遍历二叉树

int Back_Order(Tree *List,int T)
{
if (List!=Null && T!=Thread)
{
{ if (Back_Order(List->LChild,List->LTag)==1)
if (Back_Order(List->RChild,List->RTag)==1)
{
Opera(*List);
return 1;
}
}
}else return 1;
} //后序遍历二叉树

int Mid_Thread( Tree *List,Tree **Top)
{ int In_Mid_Thread(Tree *T,Tree **Pre_1);
Tree *Pre;
Pre = *Top;
(*Top)->LTag = Thread;(*Top)->LChild = List;
In_Mid_Thread(List,&Pre);
Pre->RChild=*Top;
((*Top)->RTag) = Thread;(*Top)->RChild = Pre;


return 1;}//中序线索遍历二叉树

int In_Mid_Thread(Tree *T,Tree **Pre_1)
{
if (T!=Null && T->LTag!=Link && T->RTag !=Link)
{if (In_Mid_Thread(T->LChild, &(*Pre_1))==1)
{int a;

if ((T->LChild == Null) || (T->LTag == Thread))
{
T->LChild = *Pre_1;
T->LTag = Thread;
}else T->LTag = Link;//如果当前点的LChild为空或标志域为Thread,将上一遍历节点值赴给当前点LChild,
//并将当前点赴值给Pre,备用作下一叶子节点的前驱


if ((*Pre_1)->RChild == Null || (*Pre_1)->RTag == Thread)
{
(*Pre_1)->RChild = T;
(*Pre_1)->RTag = Thread;
}else (*Pre_1)->RTag = Link;
(*Pre_1) = T;
In_Mid_Thread(T->RChild, &(*Pre_1));
} return 1;
}else return 1;
}//为中序加线索


/*根据数组转化成二叉树,
设立辅助队列,建立结点并入队,出队一结点,建左右节结并入队*/
int main ()
{
int Create_Chain_Q(Chain_Q &List);
int In_Chain_Q(Chain_Q &List, Data &Q_Value);
int Out_Chain_Q(Chain_Q &List, Data &Q_Value);
int Pre_Order(Tree *List,int T),Mid_Order(Tree *List,int T),Back_Order(Tree *List,int T);
int Mid_Thread( Tree *List,Tree **Top);
Tree_Data Array[Max_Size]={32,54,20,89,92,14,24,85,24,58,52,27,9,21,4,55,28,19,7,25,19,23,65,2,14};
int Count=0;
char Add[12],Add_1[12];
struct Tree O,O_1,*Now_O,*Order,*Head_O;
Chain_Q User;
int a=1;
//函数声明与变量定义

O.Value = Array[Count];

Order = &O;
Now_O = &O;
Create_Chain_Q(User);
In_Chain_Q(User,&Now_O);

//按照数组利用辅助链接点建立二叉树
while (1)
{
if (++Count==Max_Size)break;
Out_Chain_Q(User,&Now_O);
Now_O->LChild = (Data *)malloc(sizeof(Data));
Now_O->LChild->Value = Array[Count];
In_Chain_Q(User,&(Now_O->LChild));

if (++Count==Max_Size)break;
Now_O->RChild = (Data *)malloc(sizeof(Data));
Now_O->RChild->Value = Array[Count];
In_Chain_Q(User,&(Now_O->RChild));
}
Head_O = (Data *)malloc(sizeof(Data));

//将无孩子节点的左右Child指针设为空
while (Out_Chain_Q(User,&Now_O)!= 0)
{
Now_O->LChild = Null;
Now_O->RChild = Null;
}
printf("\n");printf("先序遍历:");
Pre_Order(Order,Link);

Mid_Thread(Order,&Head_O);
printf("\n");printf("中序遍历:");
Mid_Order(Order,Order->LTag);

printf("\n");
printf("后序遍历:");
Back_Order(Order,Order->LTag);
scanf("%s",Add);

return 1;
}

2006-01-03 15:09
樱桃小新
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2005-10-26
收藏
得分:0 

这个是对称距镇的压缩存储

#define Row 4
#define Col 4
#include <stdio.h>

int main()
{ int Reg[Row][Col] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}},Out_Reg[Row][Col],Save_Reg[Row*(Row+1)/2];
int Count_Col,Count_Row,Count;


//打印此距阵
for (Count_Row=0;Count_Row<Row;Count_Row++)
{ for (Count_Col=0;Count_Col<Col;Count_Col++)
{
printf("%d,",Reg[Count_Row][Count_Col]);
}
printf("\n");
}



//将对称距阵赴值到数组
for (Count_Row = 0;Count_Row<Row;Count_Row++ )
{
for (Count_Col = 0 ; Count_Col <= Count_Row; Count_Col++)
{
Save_Reg[((Count_Row+1)*(Count_Row))/2+Count_Col]=Reg[Count_Row][Count_Col];
}
}

printf("数组内值为:");
for (Count = 0 ; Count <= (Row*(Row+1)/2)-1 ; Count ++ )printf("%d,",Save_Reg[Count]);
printf("\n");


//将压缩存储在一维数组中的矩阵输出
for (Count_Row=0;Count_Row<Row;Count_Row++)
{
for (Count_Col=0;Count_Col<Col;Count_Col++)
{
if (Count_Row>=Count_Col)Out_Reg[Count_Row][Count_Col] = Save_Reg[((Count_Row+1)*(Count_Row))/2+Count_Col];
if (Count_Row <Count_Col)Out_Reg[Count_Row][Count_Col] = Save_Reg[((Count_Col+1)*(Count_Col))/2+Count_Row];
}
}
//打印输出数组
for (Count_Row=0;Count_Row<Row;Count_Row++)
{ for (Count_Col=0;Count_Col<Col;Count_Col++)
{
printf("%d,",Out_Reg[Count_Row][Count_Col]);
}
printf("\n");
}

scanf("%d",&Count_Col);
return 1 ;
}

2006-01-03 15:10
樱桃小新
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2005-10-26
收藏
得分:0 
恩还有很多朋友想让我帮忙写几道题,可是我明天就要去海南了,等我回来的我要是有时间的话一定帮忙,才看到,不好意思不好意思
2006-01-03 15:14
cecoo
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-12-30
收藏
得分:0 

把人家当苦力啊
2006-01-08 03:50
spp509
Rank: 1
等 级:新手上路
威 望:1
帖 子:98
专家分:0
注 册:2005-11-23
收藏
得分:0 
真的很不错,我顶你

一听就懂,一看就会,一做就错……
2006-01-09 21:24
ly622166
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-1-10
收藏
得分:0 
请问有没有 邻接表 存贮 稀疏矩阵的啊!!!!!!!!!!!!急用!!!!!!!!1
2006-01-10 20:03
hy99303
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-3-11
收藏
得分:0 
谢谢楼主了,很有用的,对初学者
2006-03-11 15:08
zshgwy
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-3-11
收藏
得分:0 
好帖!!辛苦了
2006-03-11 21:44
cocogin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-3-12
收藏
得分:0 

谢谢啦,收了看看


为了某人而存在的这一瞬间 就算是将其当成我生命的全部也无怨无悔
2006-03-12 15:25
戴待
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-5-21
收藏
得分:0 

我也需要huffman编码译码的程序 谢谢啦

2006-05-22 23:45
快速回复:本学期我学习数据结构的一些练习,现在发上来,希望能给初学者带来帮助 ...
数据加载中...
 
   



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

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