// stdafx.cpp : 只包括标准包含文件的源文件
// 期末复习草稿纸.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
#include "stdlib.h"
/*查找*/
//顺序查找
int SearchInOrder(Sqlist ST, char key)//有则返回位置,没有则返回0
{
ST.elem[0] = key;
for (int i = ST.length; i >= 0; i--)
{
if (ST.elem[i] == key)
return i;
}
}
//折半查找
int SearchMid(Sqlist &ST, char key)
{
int low = 1;
int high = ST.length;
while (low<high)
{
int mid = (low + high) / 2;
if (ST.elem[mid] == key)
return mid;
else if (ST.elem[mid] > key)
low = mid + 1;
else
high = mid - 1;
}
return 0;
}
//二叉排序树查找
BiTree SearchBitree(BiTree T, char key,BiTree p)
{
if (T == NULL)
return 0;
else if (T->data == key)
{
p = T;
return p;
}
else if (T->data > key)
{
return SearchBitree(T->rchild,key,p);
}
else if (T->data < key)
{
return SearchBitree(T->lchild,key,p);
}
}
/*排序*/
//起泡排序
void SortBubble(Sqlist &L)
{
int temp;
for (int i = 1; i <= L.length; i++)
{
for (int j = 1; j < L.length - i; j++)
{
if (L.elem[j] > L.elem[j + 1])
{
temp = L.elem[j];
L.elem[j] = L.elem[j + 1];
L.elem[j + 1] = temp;
}
}
}
}
//快速排序
int partition(Sqlist &L, int low, int high)
{
char pivotkey = L.elem[low];
int temp;
while (low < high)
{
while (L.elem[high] > pivotkey)
high--;
temp = L.elem[low];
L.elem[low] = L.elem[high];
L.elem[high] = temp;
while (L.elem[low] < pivotkey)
low++;
temp = L.elem[low];
L.elem[low] = L.elem[high];
L.elem[high] = temp;
}
return low;
}
void QuickSort(Sqlist &L, int low, int high)
{
if (low < high)
{
int piv = partition(L, low, high);
QuickSort(L, low, piv - 1);
QuickSort(L, piv - 1, high);
}
}
//简单选择排序
//直接插入排序
void InsertSort(Sqlist &L)
{
for (int i = 2; i < L.length; i++)
{
if (L.elem[i] < L.elem[i - 1])
{
L.elem[0] = L.elem[i];
L.elem[i - 1] = L.elem[i];
int j;
for (j = i - 1; L.elem[j] > L.elem[0]; j--)
{
L.elem[j + 1] = L.elem[j];
}
L.elem[j + 1] = L.elem[0];
}
}
}
/*树*/
//插入元素构造树(有空元素符号的先序序列)
int CreateTree(BiTree &B)
{
char a;
scanf("%c", &a);
if (a ==' ')//注意单引号才是字符,双引号是字符串
B = NULL;
else
{
TreeNode* T = (TreeNode*)malloc(sizeof(TreeNode));
if (!T)
return OVERFLOW;
else
{
T->data = a;
CreateTree(T->lchild);
CreateTree(T->rchild);
}
}
return OK;
}
//先序遍历
void PreOrder(BiTree B)
{
if (B != NULL)
{
printf("%c ", B->data);
PreOrder(B->lchild);
PreOrder(B->rchild);
}
}
//中序遍历
void InOrder(BiTree B)
{
if (B != NULL)
{
PreOrder(B->lchild);
printf("%c ", B->data);
PreOrder(B->rchild);
}
}
//后序遍历
void PostOrder(BiTree B)
{
if (B != NULL)
{
PreOrder(B->lchild);
PreOrder(B->rchild);
printf("%c ", B->data);
}
}
//求高度
int HighTree(BiTree B)
{
if (B = NULL)
return 0;
else
{
return (HighTree(B->lchild) + HighTree(B->rchild) + 1);
}
}
//找到指定根节点的子树
BiTree FindCh(BiTree B,char e)
{
if (B->data == e)
return B;
else
{
BiTree T1 = FindCh(B->lchild,e);
if (!T1)
{
return T1;
}
BiTree T2 = FindCh(B->rchild, e);
if (!T2)
{
return T2;
}
}
}
//求树中叶子节点总个数
int CountLeaves(BiTree B)
{
if (B->lchild == NULL&&B->rchild == NULL)
return 1;
else
{
return (CountLeaves(B->lchild) + CountLeaves(B->rchild));
}
}
//求节点总个数
int CountAllNode(BiTree B)
{
if (B == NULL)
{
return 0;
}
else
{
return (1 + CountAllNode(B->lchild) + CountAllNode(B->rchild));
}
}
/*单链表*/
//初始化
int Initlist(Linklist &L)
{
//前方结构体定义的时候使用别名typdef别名才可生效
L = (LNode*)malloc(sizeof(LNode));
L->data = 0;
L->next = NULL;
return OK;
}
int GETLEN(Linklist L)
{
int i = 1;
while (L->next != NULL)
i++;
return i;
}
//插入
int Insertlist(Linklist &L, int i, char e)
{
if (i<0 || i>GETLEN(L)) return OVERFLOW;
else
{
Linklist p = L; int j = 1;
while (p&&j <i-1)
{p = p->next;j++;}
Linklist s = (Linklist)malloc(sizeof(LNode));
s->data = e; s->next = p->next;
p->next = s; return OK;
}
}
//删除
int Deletelist(Linklist &L, int i, char e)
{
if (i<0 || i>GETLEN(L)) return OVERFLOW;
else
{
Linklist p = L; int j = 1;
while (p&&j <i - 1)
{p = p->next; j++;}
//因为后面要free掉一个节点,所以前面要NEW一个来代替
Linklist q = p->next; p->next = q->next;
q->data = e; free(q); return OK;
}
}
//输入建立
int CreateList(Linklist &L, int n)
{
L = (Linklist)malloc(sizeof(LNode));
L->next = NULL;
for (int i = 0; i < n; i++)
{
int elem; scanf("%d\n", &elem);
Linklist s = (Linklist)malloc(sizeof(LNode));
if (!s || !elem) return ERROR;
s->next = L->next; L->next = s; s->data = elem;
}
}
//清空
int Clearlist(Linklist &L)
{
Linklist p = L;L->next = NULL;
while (p)
{Linklist q = p;p =p->next;free(q);
//删除整个链表free(L);}
}