书目为 数据结构 C语言版 严蔚敏
在学习数据结构这门课的过程中,其实在最初上这个课的时候,觉得都听不懂,都是天书。但再过了一年之后,再回过头去翻翻那本旧书。以前那些遥不可及的算法,其实并不难,都是因为懒,不去自己写写,一看就告诉自己,这个东西我肯定写不出来,那你绝对是写不出来的。
还有一个很重要的东西,那就是记忆。如果你什么都记不住的话,那你肯定是学不好的。要么你相当聪明,都能自己想到那些算法。反复的练习肯定是可以加深记忆。
当熟悉了算法,接下来的就是应用了。如何选择一种合适的算法应用于实际的代码中,除了要善于分析,即其中算法空间和时间复杂度。
下面列出部分我写的数据结构代码,全部代码都在VS 6.0中运行通过,
二叉树的例子
/**/
/*
* test for Binary Tree
* Vencient Yu
*/


/**/
//
//
//
filename: binaryTree.cpp
//
#ifndef STDIO_H
#define
STDIO_H

#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
malloc.h
>

#include
<
string
.h
>
//
for memset

//
#include "BiTree.h"
//
#include "queue.h"
#include
"
sqQueue.h
"

#define
TRUE 1
#define
FALSE 0

typedef
int
status;


/**/
//
//
Create binary tree
//
return T root node
BiTree CreateBiTree(BiTree T,
int
e)

...
{
if( T == NULL)

...{
T = (BiTree) malloc(sizeof(BiTNode));
if( NULL == T)

...{
printf("memory allocate error ");
return FALSE;
}
T->data = e;
T->lChild = NULL;
T->rChild = NULL;
}
else if( e > T->data)

...{
T->rChild = CreateBiTree(T->rChild, e);
}
else

...{
T->lChild =CreateBiTree(T->lChild, e);
}

return T;
}


void
TreePrint_InOrder(BiTree T)

...
{

if(T != NULL)

...{
TreePrint_InOrder(T->lChild);
printf("root val = %d ", T->data);
TreePrint_InOrder(T->rChild);
}

}



int
PrintTreeData(
int
e)

...
{
printf(" value = %d ", e );

return TRUE;
}


/**/
/////
//
use *visit to traverse binary tree
//
preOrder
status PreOrderTraverse(BiTree T, status(
*
visit)(
int
e))

...
{
if(T)

...{
(*visit)(T->data);
if(PreOrderTraverse(T->lChild, visit))
if(PreOrderTraverse(T->rChild, visit))
return TRUE;

}

printf("NO ELEMENTS!!");
return TRUE;
}



/**/
///
//
use *visit to traverse binary tree
//
LevelOrder
//
status LevelOrderTraverse(BiTree T, status(
*
visit)(
int
e))

...
{
BiTree p;
SqQueue Q ;
InitSQueue(Q);

p = T;

EnSQueue(Q, p);

(*visit)(p->data);

while(!IsEmptySQueue(Q)) //依次进入队列的就是层次遍历顺序

...{
DeSQueue(Q, p); // 主要是访问左右子树 并把他们加如队列

if(p->lChild)

...{
EnSQueue(Q,p->lChild);
(*visit)(p->lChild->data);
}
if(p->rChild)

...{
EnSQueue(Q, p->rChild);
(*visit)(p->rChild->data);
}
}
DestroySQueue(Q);

return TRUE;
}



int
GetData()

...
{
int val;

printf("input int value : 0 for escap ");

scanf("%d", &val);
return val;
}


void
main()

...
{
BiTree T = NULL;
int val;

status (*pVisit)(int) ;

pVisit = PrintTreeData ;
val = GetData();
while(val != 0) // 0 for escap

...{
T = CreateBiTree(T, val);
val = GetData();
}


printf("inorder traverse binary tree ");
TreePrint_InOrder(T);
printf("Level Order traverse binary tree ");
LevelOrderTraverse(T, pVisit);

return ;
}

#endif

/**/
//
//
循环队列
//
//
功函数
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
malloc.h
>

#include
"
sqQueue.h
"






/**/
////
//
initialize
int
InitSQueue(SqQueue
&
Q)

...
{
Q.base = (SQElemType*) malloc(sizeof(SqQueue)*MAXQSIZE);

if(Q.base == NULL)
exit(1);

Q.front = Q.rear = 0;

return true;
}



/**/
/
//
length of queue
int
SQueueLength(SqQueue Q)

...
{
return ((Q.rear - Q.front + MAXQSIZE) % MAXQSIZE) ;
}



/**/
//
put element in queue
int
EnSQueue(SqQueue
&
Q, SQElemType e)

...
{
if((Q.rear +1) % MAXQSIZE == Q.front) //queue is full

...{
printf("queue is full ");
return false;
}

Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;

return true;
}



/**/
///
//
delete element from queue
int
DeSQueue(SqQueue
&
Q, SQElemType
&
e)

...
{
if(Q.front == Q.rear)

...{
printf("queue is empty");
return false;
}

e = Q.base[Q.front] ;
Q.front = (Q.front + 1) % MAXQSIZE ;

return true;
}



/**/
/
//
int
DestroySQueue(SqQueue
&
Q)

...
{
free(Q.base) ;

Q.base = NULL;

return true;

}


int
IsEmptySQueue(SqQueue Q)

...
{
if(Q.front == Q.rear)
return true;
else
return false;
}






/**/
//
binary tree
//
//
filename: BiTree.h
//



/**/
///
//
二叉树的存储结构


typedef
struct
BiTNode
...
{
int data;
struct BiTNode *lChild, *rChild;
//struct BiTNode *parent;
}
BiTNode,
*
BiTree;






/**/
////
//
循环队列
//
//
filename: sqQueue.h

/**/
/
//
存储类型


#include
"
BiTree.h
"

#define
MAXQSIZE 100

typedef BiTree SQElemType ;


typedef
struct
...
{

SQElemType *base;
int front;
int rear;

}
SqQueue;



/**/
////
//
功能函数申明
//
//

/**/
////
//
initialize
int
InitSQueue(SqQueue
&
Q) ;



/**/
/
//
length of queue
int
SQueueLength(SqQueue Q);



/**/
//
put element in queue
int
EnSQueue(SqQueue
&
Q, SQElemType e);



/**/
///
//
delete element from queue
int
DeSQueue(SqQueue
&
Q, SQElemType
&
e);



/**/
///
//
destroy queue
int
DestroySQueue(SqQueue
&
Q) ;


/**/
///
//
empty
int
IsEmptySQueue(SqQueue Q);