注册 登录
编程论坛 数据结构与算法

本学期我学习数据结构的一些练习,现在发上来,希望能给初学者带来帮助

樱桃小新 发布于 2005-12-07 14:14, 15823 次点击
怎么说呢,这些练习写的不是很全面,写的可能也不是很好,结构也不够严谨,就是我平时看书时为了了解一下算法,而自己做的练习,做的时候就把主要的东西做出来了,比如说栈,我只写了入栈和出栈的函数,其他的都没写。程序也因此都不太长,不过我想正是因为他不全面,因为他们短,才可以为初学者带来一些帮助,那些全面的,初学者可能很难看懂,由于我是c语言和数据结构同时学的,所以开始写的水平可能差些,越往后就写的越好些,和和,不好意思,为了方便大伙能够复制下来就直接运行,里面的函数我和主程序也没有分开写,也简单的在里面加了些注释,现在写到了霍夫曼编码,后面的还正在写,礼拜五我就回学校了,也不知道能不能把整本书的主要内容都写完,所有程序在Dev C++下运行通过!最好真能够给大伙带来些帮助,我也算没白发,还有大家不要笑话我
63 回复
#2
樱桃小新2005-12-07 14:17

顺序表的

#include <malloc.h>
#include <stdio.h>
#define List_Max_Size 100
#define Add_Size 10


struct Human_Data {
char Name [20];
int Age;
};//定义顺序表数据结构体
typedef Human_Data Thread_Save_Struct;

struct Thread_1 {
int Length;
int List_Size;
Thread_Save_Struct *Point;
};//定义顺序表指针结构体
typedef Thread_1 Thread;

int Create_List( Thread &List )
{
List.Point = (Thread_Save_Struct * )malloc ( sizeof(Thread_Save_Struct) * List_Max_Size);
if ( !List.Point ) return (0);
List.List_Size = List_Max_Size;
List.Length = 0;
return (1);
}//建立一个顺序表

int main()
{
int Create_List( Thread &List );
int OnOff = 1;
int Add = 0;
Thread_Save_Struct *Point_1;
Thread User;


if (Create_List(User)==1)
{
printf("\n","存储空间开辟成功");

for (Point_1 = User.Point;OnOff==1 && User.Length<User.List_Size;Point_1++)
{ printf("当前指针域:%d\n",Point_1);
printf("在此输入名字:");
scanf("%s", Point_1->Name);
printf("在此输入年龄:");
scanf("%d", &(Point_1->Age));
printf("是否继续输入?是(1),否(0):");
scanf("%d",&OnOff);
User.Length++;
}//for
printf("当前指针域:%d\n",Point_1);
for (Point_1 = User.Point;Add != User.Length ;Point_1++)
{
printf("当前指针域%d\n",Point_1);
printf("姓名:%s\n",Point_1->Name);
printf("年龄:%d",Point_1->Age);
scanf("%d", &OnOff);
Add++;
}//for
}//if
else printf("%s\n","存储空间开辟失败");
return 0;}//main

开辟了段空间,然后在里面存数,然后打印

#3
樱桃小新2005-12-07 14:19

双向链式表的
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define List_Max_Size 100
#define Add_Size 10
#define Null 0


struct Member_Data {
char Name [20];
int Age,Length;
Member_Data *Next;
Member_Data *Pre;
};//定义链式表数据结构体
typedef Member_Data Chain_Save_Struct;
typedef Member_Data Head_Chain;



int Create_Chain( Chain_Save_Struct &List )
{
List.Length = 0;
List.Next = Null;
List.Pre = Null;
return (1);
}//建立一个链式表

int Add_Chain ( Head_Chain &List,Chain_Save_Struct **Position)
{
Chain_Save_Struct *Position_1,*PreP;
if (List.Next==NULL)
{
List.Next = (Chain_Save_Struct * )malloc ( sizeof(Chain_Save_Struct));
if (!List.Next) return(0);
List.Length=1;
(List.Next -> Next) = Null;
(List.Next ->Pre) = &List;
*Position = List.Next;
printf("List.Next=%d\n",List.Next);
return(1);
}
else
{
Position_1 = *Position;
for (*Position = (List.Next);
*Position !=NULL; *Position=(*Position)->Next)PreP = *Position;
PreP->Next = (Chain_Save_Struct * )malloc ( sizeof(Chain_Save_Struct));
if (!List.Next) return(0);
List.Length++;
(PreP->Next->Next) = Null;
(PreP->Next->Pre)= PreP;
*Position = Position_1;
return(1);
}
}//为链表添加一个元素;

int Del_Chain (Chain_Save_Struct &List,Chain_Save_Struct **Position)
{ int a,b,c;
Chain_Save_Struct *PreP,*NPosition;
PreP = Null;
if (List.Next==Null||(*Position)->Pre==Null)
{
return 0;
}//if 链表为空表时
for (NPosition=&List;
NPosition != *Position; NPosition = NPosition->Next)PreP = NPosition;//找到当前删除节点的前驱
NPosition = (*Position)->Next;//当前节点的后继
free(*Position);
--List.Length;
//分为当前节点无后继,当前节点有后继两种情况
if (NPosition == Null)
{ *Position =PreP;
printf("无后继");

scanf("%d",&a);
(*Position)->Next = Null ;
return 1;// 当前节点无后继节点
}
else if (NPosition != Null)
{ printf("有后继");

scanf("%d",&a);
*Position = PreP;
PreP->Next = NPosition;
NPosition->Pre = PreP;
return 1;

}//当前节点有后继节点

}

int Chain_Next (Chain_Save_Struct **Position)
{ if (*Position==Null)return 0;
if ((*Position)->Next == Null) return 0;
*Position=((*Position)->Next);
return 1;
}//下移一个位置


int Chain_Pre (Chain_Save_Struct **Position)
{
if (*Position==Null)return 0;
if ((*Position)->Pre == Null) return 0 ;
*Position=((*Position)->Pre);
return 1;
}//上移一个位置

int main()
{
int Create_Chain( Chain_Save_Struct &List );
int Add_Chain ( Chain_Save_Struct &List,Chain_Save_Struct *Position);
int Del_Chain (Chain_Save_Struct &List,Chain_Save_Struct *Position);
int Chain_Next (Chain_Save_Struct *Position) ;
int Chain_Pre (Chain_Save_Struct *Position);
int OnOff = 1;
int Long = 0,Now =0,First=1;
Chain_Save_Struct *Point_1,*Point_2;
Chain_Save_Struct User,User_1;
char Comm[20];

if (Create_Chain(User)==1)
{
printf("\n","存储空间开辟成功");
Point_1 = User.Next;
while (OnOff==1)

{
printf(">:");
scanf("%s", Comm);
if (strcmp("add",strlwr(Comm))== 0)Add_Chain(User,&Point_1);
if (strcmp("del",strlwr(Comm))== 0) if (Del_Chain(User,&Point_1)==0)printf("表已空或当前节点为头节点\n");
if (strcmp("next",strlwr(Comm))== 0)if (Chain_Next(&Point_1)==0)printf ("已到表尾\n");
if (strcmp("back",strlwr(Comm))== 0) if (Chain_Pre(&Point_1)==0)printf ("已到表头\n");
if (strcmp("exit",strlwr(Comm))==0) OnOff =0;

for (Point_2 = User.Next;Point_2!=Null;Point_2 = Point_2->Next)
{
Long++;
if (Point_2==Point_1) Now = Long;
}//for
printf("表长=%d\n",Long);
printf("当前位置=%d\n",Now);
printf("@");
while(Long>=First)
{
printf("#");
First++;
}//while
printf(">\n");
First = 1;
while(Now>=(First))
{
printf(" ");
First++;
}//while
printf("|\n");
printf("Point_1=%d\n",Point_1);
if (Point_1!=Null)printf("Point_1->Pre=%d\n",Point_1->Pre);
if (Point_1!=Null)printf("Point_1->Next=%d\n",Point_1->Next);
printf("当前位置%d\n",Point_1);
printf("User.Length=%d\n",User.Length);
Long=0,Now=0,First=1;
}//while
}//if


else printf("%s\n","存储空间开辟失败");

return 0;
}//main
就在里面做了4个函数,运行后输入add可以添加,del可以删除,next指针下移,back,上移

#4
樱桃小新2005-12-07 14:22

顺序栈的
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define List_Max_Size 100
#define Add_Size 10


struct Human_Data {
char Name[20] ;
int Age;
};//定义顺序栈数据结构体
typedef Human_Data Stack_Save_Struct;

struct Stack {
int Length;
int List_Size;
Stack_Save_Struct *Base,*Top;
};//定义顺序栈指针结构体

int Create_Stack( Stack &Stack_List )
{
Stack_List.Base = (Stack_Save_Struct * )malloc ( sizeof(Stack_Save_Struct) * List_Max_Size);
if ( !Stack_List.Base ) return (0);
Stack_List.List_Size = List_Max_Size;
Stack_List.Top = Stack_List.Base;
Stack_List.Length = 0;
return (1);
}//建立一个栈表

int Push_Stack ( Stack &List,Stack_Save_Struct &Data)
{ if (List.Length == List.List_Size)return 0;
*List.Top = Data;
// *Top++ = &Data;
List.Top++;
List.Length++;
return 1;
}//入栈

int Get_Stack ( Stack &List,Stack_Save_Struct &Data)

{ int a;
if ( List.Length == 0)return 0;
Data = *(--List.Top);
List.Length--;
return 1;
}//出栈

int main()
{ char Comm[20];
int a,b,c;
int OnOff = 1;
struct Stack User,User_1;
Stack_Save_Struct Save,Save_1;
int Create_Stack( Stack &Stack_List );
int Push_Stack ( Stack &List,Stack_Save_Struct &Data);
int Get_Stack ( Stack &List,Stack_Save_Struct &Data);

Create_Stack(User);
printf("存储空间开辟成功");
while (OnOff==1)

{ printf(">:");
scanf("%s", Comm);
if (strcmp("push",strlwr(Comm))== 0)
{

printf("在此输入姓名:");
scanf("%s", Save.Name);
printf("在此输入年龄:");
scanf("%d", &Save.Age);
if (Push_Stack(User,Save)== 0)printf("栈满");
}
else if (strcmp("get",strlwr(Comm))== 0)
{
if (Get_Stack(User,Save)== 0)printf("栈空");
else {
printf("%s\n", Save.Name);
printf("%d\n", Save.Age);}
}
}//while


// }//if
return 1 ;
}

运行后输入push和get可以分别入栈和出栈

#5
樱桃小新2005-12-07 14:24

#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define List_Max_Size 4
#define Add_Size 10


struct Human_Data {
char Name[20] ;
int Age;
};//定义顺序栈数据结构体
typedef Human_Data Stack_Save_Struct;

struct Queue {
int Tail_Position,Head_Position;
int Length;
int List_Size;
Stack_Save_Struct *Base;
};//定义顺序栈指针结构体





int Create_Stack( Queue &Queue_List )
{
Queue_List.Base = (Stack_Save_Struct * )malloc ( sizeof(Stack_Save_Struct) * List_Max_Size);
if ( !Queue_List.Base ) return (0);
Queue_List.List_Size = List_Max_Size;
Queue_List.Head_Position=0;
Queue_List.Tail_Position=0;
Queue_List.Length = 0;
return (1);
}//建立一个队列

int In_Stack ( Queue &List,Stack_Save_Struct &Data)

{ //printf(" tail=%d,Head_Position=%d,base=%d\n", List.Base+((List.Tail_Position%List_Max_Size)+1),List.Head_Position,List.Base);

if (List.Base+((List.Tail_Position+1)%List_Max_Size) == (List.Base+(List.Head_Position%List_Max_Size)))return 0;
List.Tail_Position = List.Tail_Position%List_Max_Size;
*(List.Base+List.Tail_Position) = Data;
List.Tail_Position++;
List.Length++;
return 1;
}//入队

int Out_Stack ( Queue &List,Stack_Save_Struct &Data)

{ if ( List.Base+(List.Head_Position%List_Max_Size) == List.Base+(List.Tail_Position%List_Max_Size))return 0;
Data = *(List.Base+(List.Head_Position%List_Max_Size));
List.Head_Position++;
List.Length--;
return 1;
}//出队

int main()
{ char Comm[20];
int a,b,c;
int OnOff = 1;
struct Queue User,User_1;
Stack_Save_Struct Save,Save_1;
int Create_Stack( Queue &Queue_List );
int In_Stack ( Queue &List,Stack_Save_Struct &Data);
int Out_Stack ( Queue &List,Stack_Save_Struct &Data);

if (Create_Stack(User)==1)
{
printf("存储空间开辟成功\n");
while (OnOff==1)

{ printf(">:");
scanf("%s", Comm);
if (strcmp("in",strlwr(Comm))== 0)
{

printf("在此输入姓名:");
scanf("%s", Save.Name);
printf("在此输入年龄:");
scanf("%d", &Save.Age);
if (In_Stack(User,Save)== 0)printf("队列满,数据没有添加\n");
}
else if (strcmp("out",strlwr(Comm))== 0)
{
if (Out_Stack(User,Save)== 0)printf("队列空\n");
else {
printf("%s\n", Save.Name);
printf("%d\n", Save.Age);
}
}
/* printf("Position=%d,",User.Tail_Position);
printf("Base=%d,",User.Base);
printf("Base+P=%d,",User.Tail_Position%List_Max_Size);
printf("NowAdd=%d,",User.Base+(User.Tail_Position%List_Max_Size));
printf("Head_Position==%d,",User.Head_Position);
printf("Size=%d\n",sizeof(Stack_Save_Struct));*/
}//while


}//if
return 1 ;
}
顺序队列的,输入in和out可以入队和出队,

#6
樱桃小新2005-12-07 14:28
暂时先发这几个,也不知道大伙用得上用不上,如果用的话,就说一声,我就再把剩下的发上来,没人用我就不占用空间了,还有对称矩阵,稀疏矩阵,Huffman编码的,对了,还有,这些程序在我的c里面是有缩进的,但是发到这里,可能是格式不一样吧,缩进有些乱,大家想看的话就自己下来调一下,就这样,我睡觉去了
#7
一直在迷茫2005-12-07 15:23
有用,当然有用了,好东西啊!!~!~!~!~!
#8
直撼的跑2005-12-11 18:07

你会用二叉树编写一个计算器的程序吗 c语言的

#9
yumeng2005-12-15 08:43
对于初学者太有用了,谢谢!
#10
jianandegeji2005-12-16 14:09
真的是很有用啊 小弟还有一题相求 望大哥帮帮我啊 十万火急啊
“设存储器中有M个物理块(块号为1。。。。M)。一个进程分为N个大小相等的页面(页面号为1。。。。N),执行过程时要对访问各个页面,每个物理块存储一个页面,当N大于M时,有时当前要访问的页面不在物理块中,则若有空闲物理块时,当前要访问的页面放入空闲物理块中,否则要淘汰物理块中的某一个页面进行置换。赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来所经历的时间T,给定页面号引用序列,若没有空闲物理块时,选择现有物理块中的页面中其T值最大的页面予以淘汰进行置换。”
大哥要是会的话一定帮小弟接解决一下啊 谢谢了先
#11
gevin2005-12-17 20:52

对于我很有用,谢谢!请问有没有Huffman编码与译码的C程序吗?不胜感激!

#12
forgetful2005-12-23 18:15

(1) 建立一棵二叉排序树,并在所建立的二叉叉排序树中可插入某一结点及删除任一结点,要求插入和删除后不破坏原二叉排序树的结构。

(2) 画出你所建的这棵二叉排序树,并能动态反映你所插入结点、删除结点及删除时结点继承的过程(具有可视化、彩色、美观的效果)。

(3) 能查找任一结点的左、右孩子

(4) 能查找任一结点的所有兄弟

(5) 能查找任一结点的父亲

提示:一定设计好界面,界面要求设计漂亮,界面占相当比例。
用C语言
兄弟,会的话,就帮帮忙,急用.谢谢!!

#13
doctor12005-12-24 14:56

兄弟, 好东东呀

#14
langzi19852005-12-25 13:10
[讨论]

有就发上来啊,供大家学习参考嘛

#15
wuduguer2005-12-29 08:47

我想要关于2-3树的程序
帮忙呀

#16
muyilion2005-12-29 11:38
收下了,3x
#17
樱桃小新2006-01-03 15:01

不好意思,各位,上次发完后来就一直没上网,就回学校了,这新年了才回来,我把剩下的发给大家伙

#18
樱桃小新2006-01-03 15:05

霍夫曼编码的
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define List_Max_Size 100
#define Add_Size 10
#define Null 0


struct Huffman_Data {
char Word[2];
int Weight,LChild,RChild,Parent;
};//定义顺序表数据结构体
typedef Huffman_Data Thread_Save_Struct;

struct Thread_1 {
int Length;
int List_Size;
Thread_Save_Struct *Point;
};//定义顺序表指针结构体
typedef Thread_1 Thread;

typedef struct {
int Min_Weight,Addres;
}Min;

int Create_List( Thread &List )
{
List.Point = (Thread_Save_Struct * )malloc ( sizeof(Thread_Save_Struct) * List_Max_Size);
if ( !List.Point ) return (0);
List.List_Size = List_Max_Size;
List.Length = 0;
return (1);
}//建立一个顺序表


int main ()
{ int Code_Amount,Weight,Value,OnOff=1;
int Create_List( Thread &List );
int Add = 0,Add_1,e=0;
Min j,i;
char Code[100],Code_Swap[100];
Thread_Save_Struct *Point_1;
Thread User;


if (Create_List(User)==1)
{
printf("\n","存储空间开辟成功");

for (Point_1 = (User.Point)+1;OnOff==1 && User.Length<User.List_Size;Point_1++)
{ printf("在此输入字符:");
scanf("%s", Point_1->Word);
printf("在此输入权值:");
scanf("%d", &(Point_1->Weight));
Point_1->Parent = Null;
Point_1->LChild = Null;
Point_1->RChild = Null;
printf("是否继续输入?是(1),否(0):");
scanf("%d",&OnOff);
User.Length++;
}//for

}//if
else printf("%s\n","存储空间开辟失败");
i.Min_Weight=0;
j.Min_Weight=0;
while (i.Min_Weight!=-1 && j.Min_Weight!=-1)
{ i.Min_Weight =-1,j.Min_Weight =-1;
Point_1 = User.Point;
for(Add=1;Add<=User.Length;Add++)
{
if ((Point_1+Add)->Parent == 0)
{
if (i.Min_Weight ==-1||j.Min_Weight ==-1)
{
if (i.Min_Weight ==-1)
{
i.Min_Weight =(Point_1+Add)->Weight;
i.Addres = Add;
}
else if (j.Min_Weight ==-1)
{
j.Min_Weight =(Point_1+Add)->Weight;
j.Addres = Add;
}
}else if((Point_1+Add)->Weight<=i.Min_Weight||(Point_1+Add)->Weight<=j.Min_Weight)
{
if (i.Min_Weight >=j.Min_Weight )
{
i.Min_Weight =(Point_1+Add)->Weight;
i.Addres = Add;
}
if (j.Min_Weight >i.Min_Weight )
{
j.Min_Weight =(Point_1+Add)->Weight;
j.Addres = Add;
}
}//else if
}//if

}//for
if (i.Min_Weight!=-1 && j.Min_Weight!=-1)
{
(Point_1+Add)->Weight = (i.Min_Weight)+(j.Min_Weight);
if ((i.Min_Weight)<=(j.Min_Weight))
{
(Point_1+Add)->LChild =i.Addres;
(Point_1+Add)->RChild =j.Addres;
}
if ((i.Min_Weight)> (j.Min_Weight))
{
(Point_1+Add)->RChild =i.Addres;
(Point_1+Add)->LChild =j.Addres;
}
(Point_1+i.Addres)->Parent = Add;
(Point_1+j.Addres)->Parent = Add;
User.Length++;
}//if

}//while
Add=1;
for (Point_1 = (User.Point+1);Add != User.Length+1 ;Point_1++)
{
printf("parent%d,",Point_1->Parent);
printf("LChild:%d,",Point_1->LChild);
printf("RChild:%d,",Point_1->RChild);
printf("字符:%s,",Point_1->Word);
printf("权值:%d\n",Point_1->Weight);
Add++;
}//for

Point_1 = User.Point;e=0;
for (Add=1;Add<=(User.Length+1+1)/2;Add++)
{
Code[e++]=(Point_1+Add)->Word[0];
for (Add_1=Add;(Point_1+Add_1)->Parent!=0;)
{
if ((Point_1+((Point_1+Add_1)->Parent))->LChild==Add_1) Code[e++]='0';
if ((Point_1+((Point_1+Add_1)->Parent))->RChild==Add_1) Code[e++]='1';
Add_1=(Point_1+Add_1)->Parent;
}
}
printf("%s,",Code);
scanf("%d", &OnOff);
return 1;
}
这个编码还缺最后一个反转编码的小函数,也就是说,正常找码需要从叶子往根找然后再把码调过来,我懒了点,后面的反转函数没写,不好意思,呵呵

#19
樱桃小新2006-01-03 15:06

这个是稀疏矩阵的三元表存储,和转置


#define ROW 4
#define COL 4
#define Max_Size 9 //非零元最大个数
#include <stdio.h>
typedef int Data;

typedef struct
{
int Row,Col;
Data e;
}Sparse;

typedef struct
{
Sparse Grid[Max_Size+1];
int Count_Row,Count_Col,None_O;
}Tg_Array;


int main()
{

Sparse Sp_Grid;
Tg_Array Grid_1,Grid_T;
int Reg[ROW][COL] = {{1,0,3,0},{2,1,5,0},{0,8,1,0},{9,0,0,1}},Out_Reg[ROW][COL],Out_Reg_T[ROW][COL];
int Count_Col,Count_Row,Count=1,Count_1=1,Count_2=1;
Grid_1.Count_Row = ROW,Grid_1.Count_Col = COL;



//打印此距阵
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<COL;Count_Col++)
{

if (Reg[Count_Row][Count_Col]!=0)
{
if (Count > Max_Size )
{ printf("非零元个数超出限定最大值\n");
break;
}
Grid_1.Grid[Count].Row=Count_Row;
Grid_1.Grid[Count].Col=Count_Col;
Grid_1.Grid[Count].e=Reg[Count_Row][Count_Col];
Grid_1.None_O++;
Count++;
}
}

}

//矩阵的转置(行主序)
Count_Row = 0;
Count_Col=0;
for(Count = 1;Count<=Max_Size;Count++)
{
for(Count_1=1;Count_1<=Max_Size;Count_1++)
{
if (Grid_1.Grid[Count_1].Col==Count-1)
{
Grid_T.Grid[Count_2].Row = Grid_1.Grid[Count_1].Col;
Grid_T.Grid[Count_2].Col = Grid_1.Grid[Count_1].Row;
Grid_T.Grid[Count_2].e = Grid_1.Grid[Count_1].e;
Count_2++;
}
}
}

//根据三元组输出距阵 (行主序)
Count_Row = 0;
Count_Col=0;
Count = 1;

for (Count_Row=0;Count_Row<ROW;Count_Row++)
{
for (Count_Col=0;Count_Col<COL;Count_Col++)
{ if (Count_Row == Grid_1.Grid[Count].Row && Count_Col == Grid_1.Grid[Count].Col)
Out_Reg[Count_Row][Count_Col]= Grid_1.Grid[Count++].e;
else Out_Reg[Count_Row][Count_Col]= 0;
}
}

//根据转置三元组输出距阵 (行主序)
Count_Row = 0;
Count_Col=0;
Count = 1;

for (Count_Row=0;Count_Row<ROW;Count_Row++)
{
for (Count_Col=0;Count_Col<COL;Count_Col++)
{ if (Count_Row == Grid_T.Grid[Count].Row && Count_Col == Grid_T.Grid[Count].Col)
Out_Reg_T[Count_Row][Count_Col]= Grid_T.Grid[Count++].e;
else Out_Reg_T[Count_Row][Count_Col]= 0;
}
}


//打印输出数组
printf("输出稀疏矩阵为:\n");
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");
}


//打印输出转置数组
printf("输出转置稀疏矩阵为:\n");
for (Count_Row=0;Count_Row<ROW;Count_Row++)
{ for (Count_Col=0;Count_Col<COL;Count_Col++)
{
printf("%d,",Out_Reg_T[Count_Row][Count_Col]);
}
printf("\n");
}


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

#20
樱桃小新2006-01-03 15:08

这个试图的邻接表存储和遍历

#include <malloc.h>
#include <string.h>
#define List_Max_Size 100
#define Add_Size 10
#define Null 0
#define Row 4
#define True 1
#define False 0
#include <stdio.h>

struct Element{
int Data,Addr,Length;
struct Element *Next,*Pre;
};//定义链式表数据结构体
typedef Element Chain_Save_Struct;
typedef Element Head_Chain;

int Create_Chain( Chain_Save_Struct &List )
{
List.Length = 0;
List.Next = Null;
List.Pre = Null;
return (1);
}//建立一个链式表

int Add_Chain ( Head_Chain &List,Chain_Save_Struct **Position)
{
Chain_Save_Struct *Position_1,*PreP;
if (List.Next==Null)
{
List.Next = (Chain_Save_Struct * )malloc ( sizeof(Chain_Save_Struct));
if (!List.Next) return(0);
List.Length=1;
(List.Next -> Next) = Null;
(List.Next ->Pre) = &List;
*Position = List.Next;
return(1);
}
else
{
Position_1 = *Position;
for (*Position = (List.Next);
*Position !=NULL; *Position=(*Position)->Next)PreP = *Position;
PreP->Next = (Chain_Save_Struct * )malloc ( sizeof(Chain_Save_Struct));
if (!List.Next) return(0);
List.Length++;
(PreP->Next->Next) = Null;
(PreP->Next->Pre)= PreP;
*Position = Position_1;
return(1);
}
}//为链表添加一个元素;

int Del_Chain (Chain_Save_Struct &List,Chain_Save_Struct **Position)
{ int a,b,c;
Chain_Save_Struct *PreP,*NPosition;
PreP = Null;
if (List.Next==Null||(*Position)->Pre==Null)
{
return 0;
}//if 链表为空表时
for (NPosition=&List;
NPosition != *Position; NPosition = NPosition->Next)PreP = NPosition;//找到当前删除节点的前驱
NPosition = (*Position)->Next;//当前节点的后继
free(*Position);
--List.Length;
//分为当前节点无后继,当前节点有后继两种情况
if (NPosition == Null)
{ *Position =PreP;
printf("无后继");

scanf("%d",&a);
(*Position)->Next = Null ;
return 1;// 当前节点无后继节点
}
else if (NPosition != Null)
{ printf("有后继");

scanf("%d",&a);
*Position = PreP;
PreP->Next = NPosition;
NPosition->Pre = PreP;
return 1;

}//当前节点有后继节点
}

int Chain_Next (Chain_Save_Struct **Position)
{ if (*Position==Null)return 0;
if ((*Position)->Next == Null) return 0;
*Position=((*Position)->Next);
return 1;
}//下移一个位置

int Chain_Pre (Chain_Save_Struct **Position)
{
if (*Position==Null)return 0;
if ((*Position)->Pre == Null) return 0 ;
*Position=((*Position)->Pre);
return 1;
}//上移一个位置

int Opera(Chain_Save_Struct Adj[Row],int Addr)
{
Adj[Addr].Data = False;
printf("Add=%d,",Addr);

return 1;
}//操作节点


int Deep_Order(Chain_Save_Struct Adj[Row],int Addr)
{
// Opera(Chain_Save_Struct Adj[Row],int Addr);
Element *Point;
Opera(Adj,Addr);
printf(",%d",Adj[Addr].Next);

for(Point = Adj[Addr].Next;Point.Next!=Null && Adj[Point->Addr].Data==False;Point=Point->Next);
Point =Point->Next
if (Point==Null)
{

for(Addr = 0;Addr<=Row-1;Addr++)
{
if (Adj[Addr].Data==True)break;
else if (Addr == Row-1)return 1;
}
}
Deep_Order(Adj,Point->Addr);






return 1;
}//图的邻接表的深度遍历



int main()
{ int Tragle[Row][Row] = {{0,0,0,0},{1,0,1,1},{0,0,0,1},{1,0,0,0}};
Chain_Save_Struct Adj_List[Row],*Point;
int Deep_Order(Chain_Save_Struct Adj[Row],int Addr);
int Addr;
int i,j,Col;
for (i=0;i<=(Row-1);i++)
{
Create_Chain(Adj_List[i]);
Adj_List[i].Data = True;
Point=&(Adj_List[i]);
for (j=0;j<=(Row-1);j++)
{
if (Tragle[i][j]==1)
{
Add_Chain(Adj_List[i],&Point);
if (Point->Next!=Null)Point=Point->Next;
Point->Addr =j;
}
}
}//根据给定的矩阵求图的逆邻接表

for (i=0;i<=(Row-1);i++)
{
printf("是否被访问过:%d,",Adj_List[i].Data);
printf("节点名=%d",i);
for (Point=Adj_List[i].Next;Point!=Null;Point=Point->Next)
{
printf("->%d",Point->Addr);
}
printf("\n");
}

//逆邻接表在此位置
//图的深度优先遍历
Addr =0;
Deep_Order(Adj_List, Addr);
//图的广度优先遍历

scanf("%d",Col);
return 1;
}

#21
樱桃小新2006-01-03 15:09

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

#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;
}

#22
樱桃小新2006-01-03 15:10

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

#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 ;
}

#23
樱桃小新2006-01-03 15:14
恩还有很多朋友想让我帮忙写几道题,可是我明天就要去海南了,等我回来的我要是有时间的话一定帮忙,才看到,不好意思不好意思
#24
cecoo2006-01-08 03:50

把人家当苦力啊
#25
spp5092006-01-09 21:24
真的很不错,我顶你
#26
ly6221662006-01-10 20:03
请问有没有 邻接表 存贮 稀疏矩阵的啊!!!!!!!!!!!!急用!!!!!!!!1
#27
hy993032006-03-11 15:08
谢谢楼主了,很有用的,对初学者
#28
zshgwy2006-03-11 21:44
好帖!!辛苦了
#29
cocogin2006-03-12 15:25

谢谢啦,收了看看

#30
戴待2006-05-22 23:45

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

#31
大头儿子2006-05-23 12:39

能不能介绍一本比较全面的编程手册,对初学者很有用的,有就帮忙介绍一下!
谢谢了

#32
lixiaole2006-05-28 22:37

有没有拓朴排序的?

#33
yllmjw2006-11-19 00:57
thank you!!!!!!!!!!!1
#34
woren2006-12-05 21:44

菜鸟先谢。Very helpful!

#35
ferry0012006-12-07 11:50
好东东
#36
phoye2006-12-07 18:59
有用啊,就是我还看不太懂哦。
#37
xufeng9172006-12-10 14:41

很有用的
谢谢
多多发点啊
#38
旷野乡人2006-12-20 10:50

我想要这个程序
商品货架管理
1、问题描述
一超市货架以栈的方式摆放商品,生产日期越靠近栈底;出货时从栈顶取货,一天营业结束,如果货架不满,则需上货。如果直接将商品摆放到货架上,则会使生产日期越近的商品越靠近栈顶。这样就需要倒货架,仍使生产日期越近的越靠近栈底。
假设该超市由专人根据电脑销售数据随时进行上货,某种商品每件次“取货”平均时间为TX1,每件次“上货” 平均时间为TX2,该商品每天销售件数为NX(每天销售总件数据为N,K为商品种类数,N=N1+~+NK),该员工该商品上货工作时间为TX(每天工作总时间为T,T=T1+~+TK),
2、要求
设计一个算法,每一次上货后始终保持生产日期越近的商品越靠近栈底。求货架上剩余货物M、每天销售件数N、员工每天上货工作时间T,三者之间有何关系及T的最小值。


帮忙呀

#39
zh_hn2006-12-25 12:30
好东东啊!多发一点吧.
#40
wasai1262006-12-28 10:41
#41
kangloom2007-01-03 09:19
谢了哈哥们
#42
wangdong10272007-01-20 11:55
大家好,偶最近新建了一个数据结构的群(C语言版的 ),愿有兴趣的人多多捧场!凡高手者立马聘为管理员
群号:16373977
#43
wangdong10272007-01-20 12:10
大家好,偶最近建了一个数据结构的群(C语言版的),原有兴趣的多多捧场!高手者立即聘为管理员!
16373977
#44
huangjh6302007-01-21 16:35
好东西``
#45
遥远的米亚2007-04-11 12:21
以下是引用phoye在2006-12-7 18:59:44的发言:
有用啊,就是我还看不太懂哦。

#46
I喜欢c2007-04-11 13:01

好...
LZ辛苦了~``
#47
夜猫子2007-04-12 09:48
谢谢拉..非常的有用..
#48
zfc212007-04-17 22:14
太好了  
#49
KobeKing2007-04-19 17:56
晕死,有些程序很简单的,不要老是向别人要,自己多动手写才会有提高!
#50
xxfeng_05122007-04-27 19:26
我也是初学数据结构,我们的老师很牛,但是我的c语言学的很烂,所以很多简单的问题我都弄不明白,如何定义一个主函数我都不知道,学的相当吃力,好想哪位哥们帮我指点迷津,小弟不胜感激!!!!
#51
fly_9022007-05-08 15:17
看来,ME还得继续学习呀
12