mirror of
https://github.com/Ruvikm/Wangdao-Data-Structures.git
synced 2026-11-04 22:33:58 +08:00
第七章更新完毕
好像就两道需要写代码的题...
This commit is contained in:
74
查找/顺序折半查找/P278.6.cpp
Normal file
74
查找/顺序折半查找/P278.6.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#pragma region 建立顺序存储的线性表
|
||||
|
||||
#define MAX 30
|
||||
#define _for(i,a,b) for( int i=(a); i<(b); ++i)
|
||||
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
|
||||
|
||||
typedef struct {
|
||||
int data[MAX];
|
||||
int length;
|
||||
}List;
|
||||
|
||||
void swap(int* a, int* b) {
|
||||
int t;
|
||||
t = *a;
|
||||
*a = *b;
|
||||
*b = t;
|
||||
}
|
||||
|
||||
//给定一个List,传入List的大小,要逆转的起始位置
|
||||
void Reserve(List* list, int start, int end, int size) {
|
||||
if (end <= start || end >= size) {
|
||||
return;
|
||||
}
|
||||
int mid = (start + end) / 2;
|
||||
_rep(i, 0, mid - start) {
|
||||
swap(&list->data[start + i], &list->data[end - i]);
|
||||
}
|
||||
}
|
||||
|
||||
void PrintList(List list) {
|
||||
_rep(i, 1, list.length) {
|
||||
cout << list.data[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
//P278.6
|
||||
//写出折半查找的递归算法。初始调用时,1ow为1,high为ST.length。
|
||||
|
||||
int BinSearch(List list, int key, int low, int high) {
|
||||
|
||||
// 返回key在表中的序号
|
||||
if (low > high) {
|
||||
return 0;
|
||||
}
|
||||
int mid = (low + high) / 2;
|
||||
if (key > list.data[mid])
|
||||
return BinSearch(list, key, mid + 1, high);
|
||||
else if (key < list.data[mid])
|
||||
return BinSearch(list, key, low, mid - 1);
|
||||
else
|
||||
return mid;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
List list;
|
||||
list.length = 9;
|
||||
int Data[] = { -99999,2,3,5,7,9,11,13,15,41 };
|
||||
//顺序表的第0个位置不存数据
|
||||
for (int i = 1; i <= list.length; i++)
|
||||
list.data[i] = Data[i];
|
||||
PrintList(list);
|
||||
cout << "位置为:" << BinSearch(list, 15, 1, list.length) << endl;
|
||||
return 0;
|
||||
}
|
||||
79
查找/顺序折半查找/P278.7(1).cpp
Normal file
79
查找/顺序折半查找/P278.7(1).cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#pragma region 建立顺序存储的线性表
|
||||
|
||||
#define MAX 30
|
||||
#define _for(i,a,b) for( int i=(a); i<(b); ++i)
|
||||
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
|
||||
|
||||
typedef struct {
|
||||
int data[MAX];
|
||||
int length;
|
||||
}List;
|
||||
|
||||
void swap(int* a, int* b) {
|
||||
int t;
|
||||
t = *a;
|
||||
*a = *b;
|
||||
*b = t;
|
||||
}
|
||||
|
||||
//给定一个List,传入List的大小,要逆转的起始位置
|
||||
void Reserve(List* list, int start, int end, int size) {
|
||||
if (end <= start || end >= size) {
|
||||
return;
|
||||
}
|
||||
int mid = (start + end) / 2;
|
||||
_rep(i, 0, mid - start) {
|
||||
swap(&list->data[start + i], &list->data[end - i]);
|
||||
}
|
||||
}
|
||||
|
||||
void PrintList(List list) {
|
||||
_for(i, 0, list.length) {
|
||||
cout << list.data[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
//P278.7(1)
|
||||
//线性表中各结点的检索概率不等时,可用如下策略提高顺序检索的效率:若找到指定的
|
||||
//结点,则将该结点和其前驱结点(若存在)交换, 使得经常被检索的结点尽量位于表的
|
||||
//前端。试设计在顺序结构和链式结构的线性表上实现上述策略的顺序裣索算法。
|
||||
|
||||
//顺序实现
|
||||
int Search(List &list, int key) {
|
||||
|
||||
int i = 0;
|
||||
for (i; i < list.length && list.data[i] != key; i++);
|
||||
if (i < list.length && i > 0) {
|
||||
swap(list.data[i], list.data[i - 1]);
|
||||
return --i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
List list;
|
||||
list.length = 9;
|
||||
int Data[] = {2,3,5,7,9,11,13,15,41 };
|
||||
//顺序表的第0个位置不存数据
|
||||
for (int i = 0; i < list.length; i++)
|
||||
list.data[i] = Data[i];
|
||||
PrintList(list);
|
||||
cout << "位置为:" << Search(list, 9) << endl;
|
||||
cout << "位置为:" << Search(list, 9) << endl;
|
||||
cout << "位置为:" << Search(list, 9) << endl;
|
||||
cout << "位置为:" << Search(list, 9) << endl;
|
||||
cout << "位置为:" << Search(list, 13) << endl;
|
||||
cout << "位置为:" << Search(list, 13) << endl;
|
||||
PrintList(list);
|
||||
return 0;
|
||||
}
|
||||
102
查找/顺序折半查找/P278.7(2).cpp
Normal file
102
查找/顺序折半查找/P278.7(2).cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
#pragma region 建立带头结点的链表
|
||||
|
||||
#define ElemType int
|
||||
#define _for(i,a,b) for(int i=(a);i<(b);i++)
|
||||
|
||||
typedef struct LNode {
|
||||
ElemType data;
|
||||
struct LNode* next;
|
||||
}LNode, * LinkList;
|
||||
|
||||
//初始化链表
|
||||
bool InitList(LinkList& L) {
|
||||
//分配一个头结点
|
||||
L = (LNode*)malloc(sizeof(LNode));
|
||||
//内存不足,分配失败
|
||||
if (L == NULL) {
|
||||
return false;
|
||||
}
|
||||
//头结点之后暂时还没有节点
|
||||
L->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//判断是否为空
|
||||
bool IsEmpty(LinkList L) {
|
||||
return ((L->next) == NULL);
|
||||
}
|
||||
|
||||
//使用尾插法建表
|
||||
LinkList CreatList(vector<int> data) {
|
||||
if (data.size() < 1) {
|
||||
return NULL;
|
||||
}
|
||||
//头结点
|
||||
LNode* head = (LinkList)malloc(sizeof(LNode));
|
||||
head->data = NULL;
|
||||
head->next = NULL;
|
||||
LinkList p = head;
|
||||
_for(i, 0, data.size()) {
|
||||
LNode* s = (LinkList)malloc(sizeof(LNode));
|
||||
s->data = data[i];
|
||||
s->next = NULL;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
//输出链表
|
||||
void PrintList(LinkList list) {
|
||||
list = list->next;
|
||||
while (list != NULL) {
|
||||
printf("%d ", list->data);
|
||||
list = list->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
//P278.7(2)
|
||||
//线性表中各结点的检索概率不等时,可用如下策略提高顺序检索的效率:若找到指定的
|
||||
//结点,则将该结点和其前驱结点(若存在)交换, 使得经常被检索的结点尽量位于表的
|
||||
//前端。试设计在顺序结构和链式结构的线性表上实现上述策略的顺序裣索算法。
|
||||
|
||||
//链式实现
|
||||
|
||||
int Search(LinkList &head,ElemType key) {
|
||||
|
||||
int i = 0;
|
||||
LinkList p = head->next,pre=head;
|
||||
while (p && p->data != key) {
|
||||
i++;
|
||||
pre = p;
|
||||
p = p->next;
|
||||
}
|
||||
if (p) {
|
||||
swap(p->data, pre->data);
|
||||
return --i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> data{ 2,4,6,8,13,23,36,41,63 };
|
||||
LinkList head;
|
||||
InitList(head);
|
||||
head = CreatList(data);
|
||||
PrintList(head);
|
||||
cout << "位置为:" << Search(head, 8) << endl;
|
||||
cout << "位置为:" << Search(head, 8) << endl;
|
||||
cout << "位置为:" << Search(head, 8) << endl;
|
||||
cout << "位置为:" << Search(head, 41) << endl;
|
||||
cout << "位置为:" << Search(head, 41) << endl;
|
||||
PrintList(head);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user