这个是在链表基础上根据实验内容然后添加了一个二分查找的功能:
二分查找在竞赛极其开发中极其有用,尤其是二分的思想!!!
[cpp]
view plain
copy
print
?
- using namespace std;
- const int Dafultsize = 10;
- enum Error_code{success, overflow, underflow, Range_error, faild};
- template<class List_entry>
- class List{
- public:
- List(int Size = Dafultsize):Max_list(Size), num(0){
- Entry = new List_entry[Max_list];
- }
- int Size_list()const;
- bool Full_list()const;
- bool Empty_list()const;
- void Clear();
- void Print_list()const;
- void Tra_list(void (*visit)(List_entry &));
- Error_code retrieve(int postion, List_entry &item)const;
- Error_code Replace(int postion, const List_entry &item);
- Error_code Remove(int postion, List_entry &item);
- Error_code Insert(int postion, const List_entry &item);
- ~List(){
- delete []Entry;
- }Error_code Binary_search(const List_entry &key, List_entry &position);
- protected:
- int num;
- List_entry *Entry;
- int Max_list;
- };
- template<class List_entry>
- int List<List_entry>::Size_list()const{
- return num ;
- }
- template<class List_entry>
- bool List<List_entry>::Full_list()const{
- return num == Max_list – 1;
- }
- template<class List_entry>
- bool List<List_entry>::Empty_list()const{
- return num == 0;
- }
- template<class List_entry>
- void List<List_entry>::Clear(){
- num = 0;
- }
- template<class List_entry>
- Error_code List<List_entry>::Insert(int position, const List_entry &item){
- if(Full_list()){
- return overflow;
- }
- if(position < 0 || position > num){
- return Range_error;
- }
- for(int i = num – 1; i >= position; i–){
- Entry[i + 1] = Entry[i];
- }
- Entry[position] = item;
- num++;
- return success;
- }
- template<class List_entry>
- void List<List_entry>::Tra_list(void(*visit)(List_entry &)){
- for(int i = 0; i < num; i++){
- (*visit)(Entry[i]);
- }
- }
- template<class List_entry>
- Error_code List<List_entry>::retrieve(int position, List_entry &item)const{
- if(Full_list()){
- return underflow;
- }
- if(position < 0 || position > num){
- return Range_error;
- }
- item = Entry[position];
- return success;
- }
- template<class List_entry>
- Error_code List<List_entry>::Replace(int position, const List_entry &item){
- if(position > num || position < 0){
- return Range_error;
- }
- Entry[position] = item;
- return success;
- }
- template<class List_entry>
- Error_code List<List_entry>::Remove(int position, List_entry &item){
- if(Empty_list()){
- return underflow;
- }
- if(position < 0 || position > num){
- return Range_error;
- }
- item = Entry[position];
- for(int i = position;i < num; i++){
- Entry[i] = Entry[i + 1];
- }
- num–;
- return success;
- }
- template<class List_entry>
- void List<List_entry>::Print_list()const{
- cout<<“| “;
- for(int i = 0; i < num; i++){
- cout<<Entry[i];
- if(i != num – 1){
- cout<<” — “;
- }
- }
- cout<<” |”<<endl;
- }
- template<class List_entry>
- Error_code List<List_entry>:: Binary_search(const List_entry & key, List_entry &position){
- List_entry low, high, mid;
- low =0; high = this -> Size_list() – 1;
- while(low <= high){
- mid = (low + high) / 2;
- if(key < Entry[mid]){
- high = mid – 1;
- }
- else if(key > Entry[mid]){
- low = mid + 1;
- }
- else{
- position = mid;
- return success;
- }
- }
- return faild;
- }
Text.cpp:
[cpp]
view plain
copy
print
?
- #include<iostream>
- #include”a.h”
- using namespace std;
- int main()
- {
- List<int>list_1(9);
- for(int i = 1; i <= 5; i++){
- list_1.Insert(i – 1, i);
- }
- int x;
- list_1.Print_list();
- cout<<list_1.Size_list()<<endl;
- list_1.retrieve(1, x);
- cout<<x<<endl;
- list_1.Replace(2, 100);
- list_1.Print_list();
- list_1.Remove(3, x);
- list_1.Print_list();
- list_1.Clear();
- list_1.Print_list();
- // 二分查找部分
- for(int i = 1; i <= 5; i++){
- list_1.Insert(i – 1, i);
- }
- cout<<“二分查找检测:\n原数据:\n”<<endl;
- list_1.Print_list();
- cout<<“input value what you want to find:\n”;
- cin>>x;
- int position;
- list_1.Binary_search(x, position);
- cout<<“positon : “<<x<<endl;
- return 0;
- }
二叉搜索树的实现:
这里使用了二级指针来操作,函数传参数的时候传的是二级指针,二级指针的值是一级指针的地址,指向的值是一级指针的值,通过传递二级指针来操作一级指针的值,进而可以改变一级指针的指向内容。如果我们在函数中传递的是一级指针,那么就会出线传递的是一级指针的拷贝,函数就不会改变外部指针的指向内容。尤其应用在寻找插入点的时候的定位操作!!!然后中序遍历打印。
[cpp]
view plain
copy
print
?
- #include<iostream>
- #include<stdio.h>
- #include<stdlib.h>
- using namespace std;
- typedef struct BiTNode{
- int data;
- struct BiTNode *lchild, *rchild;
- }BiTNode, *BiTree;
- // 二叉搜索树实现
- bool SearchBST(BiTree T, int key, BiTree f, BiTree *p){
- if(!T){
- *p = f;
- return false;
- }
- else if(key == T -> data){
- *p = T;
- return true;
- }
- else if((key < T -> data)){
- return SearchBST(T -> lchild, key, T, p);
- }
- else{
- return SearchBST(T -> rchild, key, T, p);
- }
- }
- //二叉排序树的插入操作
- bool InsertBST(BiTree *T, int key){
- BiTree p, s;
- if(!SearchBST(*T, key, NULL, &p)){
- s = (BiTree)malloc(sizeof(BiTNode));
- s -> data = key;
- s -> lchild = s -> rchild = NULL;
- if(!p){
- *T = s;
- }
- else if(key < p -> data){
- p -> lchild = s;
- }
- else{
- p -> rchild = s;
- }
- return true;
- }
- else
- return false;
- }
- bool Delete(BiTree *p){
- BiTree q, s;
- if((*p) -> rchild == NULL){
- q = *p;
- *p = (*p) -> lchild;
- delete q;
- }
- else if((*p) -> lchild == NULL){
- q = *p;
- *p = (*p) -> rchild;
- delete q;
- }
- else{
- q = *p;
- s = (*p) -> lchild;
- while(s -> rchild){
- q = s;
- s = s -> rchild;
- }
- (*p) -> data = s -> data;
- if(q != *p){
- q -> rchild = s -> lchild;
- }
- else{
- q -> lchild = s -> lchild;
- }
- delete s;
- }
- return true;
- }
- bool DeleteBST(BiTree *T, int key){
- if(! *T){
- return false;
- }
- else{
- if(key == (*T) -> data){
- return Delete(T);
- }
- else if(key < (*T) -> data){
- return DeleteBST(&(*T) -> lchild, key);
- }
- else{
- return DeleteBST(&(*T) -> rchild, key);
- }
- }
- }
- //中序遍历算法,打印二叉排序树
- void InOrderTraverse(BiTree T){
- if(T == NULL){
- return ;
- }
- InOrderTraverse(T -> lchild);
- printf(“–%d — “, T -> data);
- InOrderTraverse(T -> rchild);
- }
- int main()
- {
- BiTNode cnt;
- cnt.data = 89;
- cnt.lchild = cnt.rchild = NULL;
- BiTree T , t;
- t = T = &cnt;
- //插入:
- cout<<“input 8 numbers what you want to insert:\n”;
- for(int i = 0; i < 8; i++){
- int x;cin>>x;
- InsertBST(&T, x);
- }
- InOrderTraverse(t);
- //查找
- cout<<“\ninput value what you want to find”<<endl;
- int x;
- cin>>x;
- t = &cnt;
- BiTree p, s;
- p = NULL;
- SearchBST(t, x, p, &s);
- cout<<(s) -> data<<endl;
- // 删除搜索树结点
- cout<<“input node what you want to delete:\n”<<endl;
- t = &cnt;
- cin>>x;
- DeleteBST(&t, x);
- InOrderTraverse(t);
- return 0;
- }
Hash拉链实现查找:
测试程序为数字去重,输出部分包括数字不同的个数,并打印出去重后的序列。
[cpp]
view plain
copy
print
?
- #include<iostream>
- #include<sstream>
- #include<algorithm>
- #include<cstdio>
- #include<string.h>
- #include<cctype>
- #include<string>
- #include<cmath>
- #include<vector>
- #include<stack>
- #include<queue>
- #include<map>
- #include<set>
- using namespace std;
- const int M = 1007;
- struct Node{
- int d;
- Node * next ;
- };
- Node * pnd[M + 1];
- Node nd[M +1];
- int n_cnt;
- int a[1000 + 18];
- int a_cnt;
- int main(){
- int n, d, p;
- while(scanf(“%d”,&n) != EOF){
- memset(pnd, 0, sizeof(pnd));
- n_cnt = 0;
- a_cnt = 0;
- for(int i = 0; i < n; i++){
- scanf(“%d”,&d);
- p = d % M;
- bool found = false ;
- Node *pt = pnd[p];
- while(pt){
- if(pt -> d == d){
- found = true;
- break;
- }
- pt = pt -> next;
- }
- if(!found){
- nd[n_cnt].d = d;
- nd[n_cnt].next = pnd[p];
- pnd[p] = &nd[n_cnt];
- n_cnt ++;
- a[a_cnt++] = d;
- }
- }
- sort(a, a + a_cnt);
- printf(“%d\n%d”, a_cnt, a[0]);
- for(int i = 1; i < a_cnt; ++i){
- printf(” %d”,a[i]);
- }
- printf(“\n”);
- }
- return 0;
- }