您的位置:首页 > 理论基础 > 数据结构算法

迎战下周自考数据结构实践科目

2015-01-07 01:03 162 查看
编写完成重点数据结构和算法: 0.链表 1.栈 2.队列 3.二叉树数据结构和构建 4.前序中序后序遍历二叉树 5.构建哈夫曼树(最优二叉树) 6.图数据结构,图的深度优先遍历和广度优先遍历 7.拓扑排序 8.直接插入排序 9.希尔排序 10.希尔排序 11.冒泡排序 12.快速排序 13.直接选择排序 14.堆排序 15.归并排序 16.箱排序和基数排序 17.顺序查找,二分查找,索引顺序查找

// ExamTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdlib.h"
#include <string>
/************************************************************************/
/*                栈数据结构                                              */
/************************************************************************/
#define StackSize 1024    //栈大小
typedef char DataType;
typedef struct
{
DataType data[StackSize];    //栈数组
int top;    //栈顶索引
}Stack;

//清空栈
void InitStack(Stack * s)
{
if(s == NULL)
return;
s->top = -1;
}
//判断是否满栈
bool StatckEmpty(Stack* s)
{
if(s == NULL)
return false;
return s->top == StackSize-1;
}
//进栈
void PushStack(Stack* s,DataType x)
{
if(s == NULL)
return;
if(StatckEmpty(s))
return;
else
{
//规定当前入栈完成后指向当前的数据
s->top = s->top +1;
s->data[s->top] = x;
}
}
//出栈
DataType PopStack(Stack* s)
{
if(s == NULL)
{
exit(0);
}
if(StatckEmpty(s))
{
printf("Stack is empty .\n");
exit(0);
}
else
{
return s->data[s->top--]; //先用后减
}
}

//获取栈顶元素
DataType GetTop(Stack* s)
{
if(s == NULL)
{
exit(0);
}
if(StatckEmpty(s))
{
printf("Stack empty . \n");
exit(0);
}
else
{
return s->data[s->top];
}
}

/************************************************************************/
/*                队列数据结构(循环队列)                              */
/************************************************************************/
#define QUEUESIZE 1024
typedef struct
{
DataType data[QUEUESIZE];    //队列数组
int front;    //队列头
int rear;    //队列尾部
}Queue;

//初始化队列
void InitQueue(Queue* q)
{
if(q == NULL)
return;
q->front = 0;
q->rear = 0;
}

//判断栈是否为空
bool QueueEmpty(Queue* q)
{
if(q == NULL)
return false;
else
return q->front == q->rear;
}

//判断队列是否满
bool QueueFull(Queue* q)
{
if(q == NULL)
return false;
return (q->rear + 1) % QUEUESIZE == q->front;
}

//入队列
void InsertQueue(Queue* q,DataType x)
{
if(q == NULL)
return;
if(QueueFull(q))
{
printf("Queue Full !\n");
}
else
{
//队尾添加,队尾指向后面一个为空的
q->data[q->rear] = x;
q->rear = (q->rear + 1) % QUEUESIZE;
}
}

//出队列
DataType DelQueue(Queue* q)
{
DataType x;
if(QueueEmpty(q))
{
printf("Queue is Empty \n");
exit(0);
}
else
{
x = q->data[q->front];
q->front = (q->front + 1) % QUEUESIZE;
return x;
}
}

//取队头元素
DataType GetFrontData(Queue* q)
{
if(QueueEmpty(q))
{
printf("queue is empty!\n");
exit(0);
}
else
{
return q->data[q->front];
}
}

//取队尾元素
DataType GetRearData(Queue* q)
{
if(QueueEmpty(q))
{
printf("queue is empty !");
exit(0);
}
else
{
return q->data[q->rear];
}
}
/* 字符串匹配(判断一个子串是否是另一个字符串的子串)*/
bool IsSubString()
{
char parentString[1024];
char subString[1024];
printf("please input parent string!\n");
scanf("%s",parentString);
printf("please input sub string!\n");
scanf("%s",subString);

int nParent = strlen(parentString);
int nSub = strlen(subString);
/*
1. 如果子串的字符数量大于父串的数量则匹配失败
2. 子串的数量大于父串的数量,匹配比较。
*/
if(nSub > nParent)
return false;
for(int i = 0; i <= nParent; i++)
{
int k = i;
if((nParent - k) < nSub)
return false;
for(int j = 0; j <= nSub; j++)
{
if(subString[j] == parentString[k])
{
if(j == nSub -1)
return true;
k++;
}
else
{
break;
}
}
}
return false;
}
typedef struct BinNode
{
DataType data;
BinNode* lChild;
BinNode* rChild;
}BinNTree;

typedef BinNode* BinTree;
char* str = "ABDH#K###E##CFI###G#J##";
//构造一个二叉树,使用前序创建,#表示对应的节点为NULL
int index = 0;
void creatBinTree(BinNTree** t)
{
if( (*t) = (BinNTree*) malloc(sizeof(BinNTree)))
{
char ch = str[index++];
if(ch == '#')
{
(*t) = NULL;
return;
}
(*t)->data = ch;
creatBinTree(&(*t)->lChild);
creatBinTree(&(*t)->rChild);
}
};

//清空二叉树
void clearBinTree(BinNTree* t)
{
if(t == NULL)
return;

if(t->lChild)
clearBinTree(t->lChild);
if(t->rChild)
clearBinTree(t->rChild);
free(t);
}
//求二叉树深度
int getBinTreeDeep(BinNTree* & t)
{
int i = 0;
int j = 0;
if(!t)
return 0;
if(t->lChild)
i = getBinTreeDeep(t->lChild);
if(t->rChild)
j = getBinTreeDeep(t->rChild);
return i > j? i+1:j+1;
}

//前序遍历
void preOrder(BinNTree* t)
{
if(t == NULL)
return;
printf("%c ",t->data);
preOrder(t->lChild);
preOrder(t->rChild);
}

//中序遍历
void inOrder(BinNTree* t)
{
if(t == NULL)
return;
inOrder(t->lChild);
printf("%c ",t->data);
inOrder(t->rChild);
}

//后序遍历
void postOrder(BinNTree* t)
{
if(t == NULL)
return;
postOrder(t->lChild);
postOrder(t->rChild);
printf("%c ",t->data);
}

//查找二叉树中是否存在某值
bool find(BinNTree* t,DataType data)
{
if(t == NULL)
return false;
if(t->data == data)
return true;
if(find(t->lChild,data))
return true;
return find(t->rChild,data);

}

int _tmain(int argc, _TCHAR* argv[])
{
/*    // 字符串匹配函数测试
while (1)
{
if(IsSubString())
{
printf("this is subString for parentString!\n");
}
else
{
printf("sorry,this is not subString for parentString\n");
}
}
*/

/************************************************************************/
/*            二叉树测试                                                  */
/************************************************************************/
BinTree t = NULL;
creatBinTree(&t);
int nDept = getBinTreeDeep(t);
printf("this BinTree Depth is: %d\n",nDept);
printf("前序遍历结果:");
preOrder(t);
printf("\n后序遍历结果:");
postOrder(t);
printf("\n中序遍历结果:");
inOrder(t);
getchar();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: