# 数据结构必看算法

1. 逆转顺序表中的所有元素

void Reverse(int A[], int n)
{
int i, t;
for (i=0; i < n/2; i++)
{
t = A[i];
A[i] = A[n-i-1];
A[n-i-1] = t;
}
}
2. 删除线性链表中数据域为 item 的所有结点

{
p = list->next;
while (p != NULL)
{
if (p->data == item)
{
q->next = p->next;
free(p);
p = q->next;
}
else
{
q = p;
p = p->next;
}
}
if (list->data == item)
{
q = list;
list = list->next;
free(q);
}

}

3. 逆转线性链表
{
p = list;
q = NULL;
while (p != NULL)
{
r = q;
q = p;
p = p->next;
q->next = r;
}
list = q;
}
4. 复制线性链表（递归）
{
if (lista == NULL)
return NULL;
else
{
listb->data = lista->data;
listb->next = Copy(lista->next);
return listb;
}
}
5. 将两个按值有序排列的非空线性链表合并为一个按值有序的线性链表
{
LinkList listc, p = lista, q = listb, r; // listc 指向 lista 和 listb 所指结点中较小者
if (lista->data <= listb->data)
{
listc = lista;
r = lista;
p = lista->next;
}
else
{
listc = listb;
r = listb;
q = listb->next;
}
while (p != NULL && q != NULL)
{
if (p->data <= q->data)
{
r->next = p;
r = p;
p = p->next;

else
{
r->next = q;
r = q;
q = q->next;
}
}
// 将剩余结点（即未参加比较的且已按升序排列的结点）链接到整个链表后面
r->next = (p != NULL) ? p : q;
return listc;
}
3

1. 二叉树的先序遍历（非递归算法）

#define MAX_STACK 50
void PreOrderTraverse(BTree T)
{
BTree STACK[MAX_STACK], p = T;
int top = -1;
while (p != NULL || top != -1)
{
while (p != NULL)
{
VISIT(p);
STACK[++top] = p;
p = p->lchild;
}
p = STACK[top--];
p = p->rchild;
}
}
2. 二叉树的中序遍历（非递归算法）

#define MAX_STACK 50
void InOrderTraverse(BTree T)
{
BTree STACK[MAX_STACK], p = T;
int top = -1;
while (p != NULL || top != -1);
{
while (p != NULL)
{
STACK[++top] = p;
p = p->lchild;
}
p = STACK[top--];
VISIT(p);
p = p->rchild;
}
}
4
3. 二叉树的后序遍历（非递归算法）

flag，当flag = 0 时表示该结点暂不访问，flag = 1 时表示该结点可以访问。flag 的值随同该结点的地

#define MAX_STACK 50
void PostOrderTraverse(BTree T)
{
BTree STACK1[MAX_STACK], p = T;
int STACK2[MAX_STACK], flag, top = -1;
while (p != NULL || top != -1)
{
while (p != NULL)
{
STACK1[++top] = p;
STACK2[top] = 0;
p = p->lchild;
}
p = STACK1[top];
flag = STACK2[top--];
if (flag == 0)
{
STACK1[++top] = p;
STACK2[top] = 1;
p = p->rchild;
}
else
{
VISIT(p);
p = NULL;
}
}
}
4. 二叉树的按层次遍历

#define MAX_QUEUE 50
void LayeredOrderTraverse(BTree T)
{
BTree QUEUE[MAX_QUEUE], p;
int front, rear;
if (T != NULL)
{
QUEUE[0] = T;
front = -1;
rear = 0;
while (front < rear)
{
p = QUEUE[++front];
VISIT(P);
if (p->lchild != NULL)
QUEUE[++rear] = p->lchild;
if (p->rchild != NULL)
QUEUE[++rear] = p->rchild;
}
}
}
5. 建立二叉树（从键盘输入数据，先序遍历递归算法）
BTree CreateBT()
{
char ch;
BTree T;
sacnf("%c", &ch);
if (ch == ' ')
return NULL;
else
{
T = (BTree)malloc(sizeof(BTNode));
T->data = ch;
T->lchild = CreateBT();
T->rchild = CreateBT();
return T;
}
}
6. 建立二叉树（从数组获取数据）
BTree CreateBT(int A[], int i, int n)
{
BTree p;
if (i > n)
return NULL;
else
{
p = (BTree)malloc(sizeof(BTNode));
p->data = A[i];
p->lchild = CreateBT(A, 2*i, n);
p->rchild = CreateBT(A, 2*i+1, n);
return p;
}
}
T = CreateBT(A, 1, n);
--------------------------------------------------------
BTree CreateBT(int A[], int n)
{
int i;
BTree *pT;
// 对应n 个结点申请可容纳n 个指针变量的内存空间
pT = (BTree *)malloc(sizeof(BTree)*n);
// 若数组中的某个元素不等于零，则申请相应的结点空间并进行赋值
for (i=1; i <= n; i++)
{
if (A[i] != 0)
{
pT[i] = (BTree)malloc(sizeof(BTNode));
pT[i]->data = A[i];

else
{
pT[i] = NULL;
}
}
// 修改结点的指针域的内容，使父结点指向左、右孩子结点
for (i=1; i <= n; i++)
{
if (pT[i] != NULL)
{
pT[i]->lchild = pT[2*i];
pT[i]->rchild = pT[2*i+1];
}
}
}
7. 求二叉树的深度（递归算法）
int Depth(BTree T)
{
int ldepth, rdepth;
if (T == NULL)
return 0;
else
{
ldepth = Depth(T->lchild);
rdepth = Depth(T->rchild);
if (ldepth > rdepth)
return ldepth+1;
else
return rdepth+1;
}
}
8. 求二叉树的深度（非递归算法）

#define MAX_STACK 50
int Depth(BTree T)
{
BTree STACK1[MAX_STACK], p = T;
int STACK2[MAX_STACK];
int curdepth, maxdepth = 0, top = -1;
if (T != NULL)
{
curdepth = 1;
while (p != NULL || top != -)
{
while (p != NULL)
{
STACK1[++top] = p;
STACK2[top] = curdepth;
p = p->lchild;
curdepth++;
}
p = STACK1[top];
curdepth = STACK2[top--];
if (p->lchild == NULL && p->rchild == NULL)
if (curdepth > maxdepth)
maxdepth = curdepth;
p = p->rchild;
curdepth++;
}
}
return maxdepth;
}
9. 求结点所在层次

#define MAX_STACK 50
int LayerNode(BTree T, int item)
{
BTree STACK1[MAX_STACK], p = T;
int STACK2[MAX_STACK], flag, top = -1;
while (p != NULL || top != -1)
{
while (p != NULL)
{
STACK1[++top] = p;
STACK2[top] = 0;
p = p->lchild;
}
p = STACK1[top];
flag = STACK2[top--];
if (flag == 0)
{
STACK1[++top] = p;
STACK2[top] = 1;
p = p->rchild;

else
{
if (p->data == item)
p = NULL;
}
}
}
10.交换二叉树中所有结点的左右子树的位置

#define MAX_QUEUE 50
void ExchangeBT(BTree T)
{
BTree QUEUE[MAX_QUEUE], temp, p = T;
int front, rear;
if (T != NULL)
{
QUEUE[0] = T;
front = -1;
rear = 0;
while (front < rear)
{
p = QUEUE[++front];
temp = p->lchild;
p->lchild = p->rchild;
p->rchild = temp;
if (p->lchild != NULL)
QUEUE[++rear] = p->lchild;
if (p->rchild != NULL)
QUEUE[++rear] = p->rchild;
}
}
}
11.删除二叉树中以某个结点为根结点的子树

#define MAX_STACK 50
BTree DeleteSubtree(BTree &T, int item)
{
BTree STACK[MAX_STACK], q, p = T;
int top = -1;
if (T->data == item)
{
DestroyBT(T);
T = NULL;
return NULL;
}
else
{
while (p != NULL || top != -1)
{
while (p != NULL)
{
if (p->data == item)
{
if (q->lchild == p)
q->lchild = NULL;
else
q->rchild = NULL;
DestroyBT(p);
return T;
}
STACK[++top]= p;
q = p;
p = p->lchild;
}
q = STACK[top--];
p = q->rchild;
}
}
}

1. 顺序查找的递归算法
int RecurSeqSearch(int A[], int n, int key, int i)
{
if (i >= n)
return -1;
if (A[i] == key)
return i;
else
return RecurSeqSearch(A, n, key, i+1);
}
pos = RecurSeqSearch(A, n, key, 0);
2. 折半查找
int BinSearch(int A[], int n, int key)
{
int low=0, high=n-1, mid;
while (low <= high)
{
mid = (low+high)/2;
if (key == A[mid])
return mid;
if (key > A[mid])
low = mid + 1;
else
high = mid – 1;
}
return -1;
}
3. 折半查找的递归算法
int RecurBinSearch(int A[], int low, int high, int key)
{
int mid;
if (low > high)
return -1;
else
{
mid = (low+high)/2;
if (key == A[mid])
return mid;
if (key > A[mid])
return RecurBinSearch(A, mid+1, high, key);
else
return RecurBinSearch(A, low, mid-1, key);
}
}
pos = RecurBinSearch(A, 0, n-1, key);
4. 在按值递增排列且长度为 n 的线性表中折半查找并插入一元素
void BinInsert(int A[], int &n, int key)
{
int j, low=0, high=n-1, mid;
while (low <= high)
{
mid = (low+high)/2;
if (key > A[mid])
low = mid + 1;
else
high = mid – 1;
}
for (j=n; j > low; j--)
A[j] = A[j-1];
A[low] = key;
n++;
}
5. 在按值递增排列且长度为 n 的线性表中折半查找值不小于key 的最小元素
void BinSearch(int A[], int n, int key)
{
int low=0, high=n-1, mid;
while (low <= high)
{
mid = (low+high)/2;
if (key == A[mid])
return mid;
if (key > A[mid])
low = mid + 1;
else
high = mid – 1;
}
if (low <= n-1)
return low;
else
return -1;
}

1. 插入排序

void InsertSort(int A[], int n)
{
int i, j, temp;
for (i=1; i <= n-1; i++)
{
if (A[i] < A[i-1])
{
j = i-1;
temp = A[i];
while (j >= 0 && temp < A[j])
{
A[j+1] = A[j];
j--;
}
A[j+1] = temp;
}
}
}
2. 折半插入排序

void BinInsertSort(int A[], int n)
{
int i, j, low, high, mid, temp;
for (i=1; i <= n-1; i++)
{
temp = A[i];
low = 0;
high = i – 1;
while (low <= high)
{
mid = (low+high)/2;
if (temp > A[mid])
low = mid + 1;
else
high = mid – 1;
}
for (j=i; j > low; j--)
A[j] = A[j-1];
A[low] = temp;
}
}
3. 冒泡排序

? i + 1 个位置上。显然，判断冒泡排序结束的条件是“在一趟排序中没有进行过交换元素的操作”，

void BubbleSort(int A[], int n)
{
int i, j, temp, flag = 1;
for (i=n-1; i >= 1 && flag == 1; i--)
{
flag = 0;
for (j=0; j < i; j++)
{
if (A[j] > A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
flag = 1;
}
}
}
}
4. 选择排序

void SelectSort(int A[], int n)
{
int i, j, min, temp;
for (i=0; i < n; i++)
{
min = i;
for (j=i+1; j < n; j++)
{
if (A[min] > A[j])
min = j;
}
if (min != i)
{
temp = A[min];
A[min] = A[i];
A[i] = temp;
}
}
}
5. 快速排序

void QuickSort(int A[], int n)
{
QSort(A, 0, n-1);
}
void QSort(int A[], int low, int high)
{
int pivotloc;
if (low < high)
{
pivot = Partition(A, low, high);
QSort(A, low, pivotloc-1);
QSort(A, pivotloc+1, high);
}
}
int Partition(int A[], int low, int high)
{
int pivot;
pivot = A[low];
// 从线性表的两端交替地向中间扫描
while (low < high)
{
while (low < high && A[high] >= pivot)
high--;
A[low] = A[high];
while (low < high && A[low] <= pivot)
low++;
A[high] = A[low];
}
A[low] = pivot;
return low;
}
6. 堆排序
void HeapSort(int A[], int n)
{
int i, temp;
// 建立大顶堆
for (i = n/2; i >= 1; i--)
for (i = n-1; i >= 1; i--)
{
temp = A[1];
A[1] = A[i+1];
A[i+1] = temp;
// 将 A[1..i] 重新调整为大顶堆
}
}
void HeapAdjust(int A[], int low, int high)
{
int i, temp;
temp = A[low];
for (i=2*low; i <= high; i=i*2)
{
// 令 i 为关键字较大的记录的下标
if (i < high && A[i] < A[i+1])
i++;
if (temp >= A[i])
break;
else
{
A[low] = A[i];
low = i;
}
}
A[low] = temp; // 插入
}
• 本文已收录于以下专栏：

举报原因： 您举报文章：数据结构必看算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)