查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
2008-04-12 19:32
741 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// search.cpp : Defines the entry point for the console application.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "LinkTable.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAX_KEY 500
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//------------------------------数组实现部分----------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
无序数组顺序查找算法函数nsq_Order_Search<用数组实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int array[] :被查找数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n :被查找数组元素个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key :被查找的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
返回值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
如果没有找到: nsq_Order_Search = -1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
否则: nsq_Order_Search = key数组下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int nsq_Order_Search(int array[],int n,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
array
= key;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*for循环后面的分号必不可少*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=0;key!=array[i];i++);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(i
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
有序数组顺序查找算法函数sq_Order_Search<用数组实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int array[] :被查找数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n :被查找数组元素个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key :被查找的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
返回值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
如果没有找到: sq_Order_Search = -1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
否则: sq_Order_Search = key数组下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int sq_Order_Search(int array[],int n,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
array
= MAX_KEY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*for循环后面的分号必不可少*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=0;key>array[i];i++);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
有序数组二分查找算法函数sq_Dichotomy_Search0<用数组实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int array[] :被查找数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n :被查找数组元素个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key :被查找的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
返回值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
如果没有找到: sq_Dichotomy_Search0 = -1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
否则: sq_Dichotomy_Search0 = key数组下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int sq_Dichotomy_Search0(int array[],int n,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int low,high,mid;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
low = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
high = n - 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(low<=high)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mid = (high+low)/2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(array[mid] == key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(mid);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*key>array[mid] 表明要求查找的值在[mid+1,high]*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*否则,在[low,mid-1]*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(key > array[mid])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
low = mid + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
high = mid - 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
有序数组插值查找算法函数sq_Dichotomy_Search1<用数组实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(插值查找算法是二分查找算法的改进)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int array[] :被查找数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n :被查找数组元素个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key :被查找的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
返回值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
如果没有找到: sq_Dichotomy_Search1 = -1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
否则: sq_Dichotomy_Search1 = key数组下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int sq_Dichotomy_Search1(int array[],int n,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int low,high, //二分数组的上,下标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pos; //查找码的大致(估算)位置
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
low = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
high = n-1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(low <= high)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pos = (key-array[low])/(array[high]-array[low])*(high-low)+low;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*找到关键值,中途退出*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(key == array[pos])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(pos);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(key > array[pos])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
low = pos + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
high = pos - 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*没有找到,返回-1*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//------------------------------数组实现部分----------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//------------------------------链表实现部分----------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
无序链表顺序查找算法函数nlk_Order_Serach<用链表实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[查找思想:遍历链表的所有节点]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node *head :被查找链表的首指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key :被查找的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
返回值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
如果没有找到: nlk_Order_Serach = NULL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
否则: nlk_Order_Serach = 键值为key的节点的指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Node *nlk_Order_Serach(Node *head,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(;head!=NULL && head->data != key;head = head->link);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(head);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
有序链表顺序查找算法函数lk_Order_Serach<用链表实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[查找思想:依次遍历链表的节点,发现节点不在key的范围时提前结束查找]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node *head :被查找链表的首指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key :被查找的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
返回值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
如果没有找到: lk_Order_Serach = NULL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
否则: lk_Order_Serach = 键值为key的节点的指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Node *lk_Order_Search(Node *head,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(;head!=NULL && head->data < key;head=head->link);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*当遍历指针为NULL或没有找到键值为key的节点,返回NULL(表明没有找到)*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*否则,返回head(表明已经找到)*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(head==NULL || head->data != key ? NULL : head);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
有序链表动态查找算法函数lk_Dynamic_Search<用链表实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[查找思想:依次遍历链表的节点,发现节点不在key的范围时提前结束查找]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node *head: 被查找链表的首指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node **p; 键值为key的节点的前驱指针(回传参数)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node **q: 键值为key的节点指针(回传参数)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key : 被查找的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
注意:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
当 *p == NULL 且 *q != NULL,链表的首节点键值为key
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
当 *p != NULL 且 *q != NULL,链表的中间(非首,尾)节点键值为key
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
当 *p != NULL 且 *q == NULL,链表的尾节点键值为key
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
当 *p == NULL 且 *q == NULL,链表是空链表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void lk_Dynamic_Search(Node *head,Node **p,Node **q,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node *pre,*cur;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pre = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cur = head;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(;cur != NULL && cur->data < key;pre = cur,cur = cur->link)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*p = pre;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*q = cur;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
有序链表动态插入算法函数lk_Dynamic_Insert<用链表实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node *head: 被插入链表的首指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key : 被插入的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
返回值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lk_Dynamic_Search = 插入键值为key的节点后的链表首指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Node *lk_Dynamic_Insert(Node *head,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*x, //插入节点的前驱节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*y, //插入节点的后续节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*p; //插入的节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = (Node *)malloc(sizeof(Node));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->data = key;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->link = NULL;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lk_Dynamic_Search(head,&x,&y,key);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(x==NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->link = x;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
head = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->link = x->link;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x->link = p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ListLinkTable(head,"插入节点");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(head);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
有序链表动态删除算法函数lk_Dynamic_Delete<用链表实现>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
参数描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node *head: 被删除链表的首指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int key : 被删除的关键值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
返回值:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lk_Dynamic_Delete = 删除键值为key的节点后的链表首指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Node *lk_Dynamic_Delete(Node *head,int key)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node *x, //删除节点的前驱节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*y; //删除的节点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lk_Dynamic_Search(head,&x,&y,key);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(x==NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*因为x=NLLL时,y指向首指针*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
head = y->link;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
x->link = y->link;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
free(y);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ListLinkTable(head,"删除节点");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return(head);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//------------------------------链表实现部分----------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Node *head;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Node *p,*x,*y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int KEY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int count,i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//int result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
KEY = 11;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//PrintArrayValue(TestArray2,DEFAULT_ARRAY_SIZE,"原始");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//result = sq_Dichotomy_Search1(TestArray2,DEFAULT_ARRAY_SIZE,KEY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//printf("查找结果是:数组[%d] = %d ",result,TestArray2[result]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
head = CreateLinkTable(DEFAULT_ARRAY_SIZE,TestArray2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ListLinkTable(head,"原始");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = lk_Order_Search(head,KEY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(p==NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 查找结果是:指定链表中没有[数据域 = %d]的节点! ",KEY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 查找结果是:节点.Data = %d 节点.Link = %d 节点.Addr = %d ",p->data,p->link,p);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("输入插入节点的个数: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d",&count);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=0;i
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("输入插入节点的数据域: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d",&KEY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lk_Dynamic_Insert(head,KEY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
do
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("输入删除节点的数据域: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d",&KEY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lk_Dynamic_Delete(head,KEY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}while(head!=NULL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 应用程序正在运行...... ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现,附代码)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现) .
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现) (转载)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)