【无标题】数据结构---第6天

发布于:2022-07-26 ⋅ 阅读:(320) ⋅ 点赞:(0)

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
int main(int argc, const char *argv[])
{
    int arr[10] = {25, 51, 8, 22, 26, 67, 11, 16, 54, 41};
    Node *hash[N];
    hash_init(hash);
    for (int i = 0; i < N; i++)
    {
        hash_insert(hash, arr);
    }
    //循环调用
    for (int i = 0; i < 10; i++)
    {
        hash_insert(hash, arr[i]);
    }
    //显示
    hash_show(hash);
    //查找
    hash_search(hash, 26);
    hash_search(hash, 100);
    return 0;
}

hash.h

#ifndef __HASH_H__
#define __HASH_H__
    typedef  int datatype;
    #define N 13
    
    typedef struct Node{
        datatype data;
        struct Node *next;
    }Node;
  // 初始化 哈希表
  void hash_init(Node *hash[]);
//存储
  void hash_insert (Node *hash[], int key);
  // 显示
  void hash_show(Node *hash[]);
  //查找
  void hash_search(Node *hash[], int key);
  
#endif

hash.c

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include "hash.h"

void hash_init(Node *hash[]){
    if(NULL == hash){
        printf("输入参数为空");
    }
for(int i = 0; i < N; i++){
    hash[i] = NULL;
}
}
//数据表存入哈希表中
void hash_insert(Node *hash[], int key){
    int pos = key%N;
    Node *p =(Node *)malloc(sizeof(Node));
    if(NULL == p){
        printf("申请失败");
        return -1;
    }
    p->data = key;
    p->next = NULL;
    //头插法 插数据
    p->next = hash[pos];
    hash[pos] = p;
    printf("存入成功");
    return 0;
}
//输入 哈希表内容
void hash_show(Node *hash[]){
    for(int i = 0; i < N; i++){
        printf("%d: ", i);
        //定义便利指针
    Node *q  = hash[i];
    while(q != NULL){
        printf("%d-->",q->data);
        q = q->next;
    }
    printf("\n");
    }
    return 0;
}
//哈希表查找
void hash_search(Node *hash[], int key){
    int pos = key%N;
    Node *q = hash[pos];
    int i =0;
    while(q != NULL ){
        if(q->data == key){
            printf("数据在表里");
            i =1; 
        }

        q = q->next;

    }
        if(i != 1)
        {
            printf("数据不在里");
        }

    return 0;

}

/

main1.c

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include "sort.h"
int main(int argc, const char  *argv[] ){
    //输入数组
    int arr[8] ={198,289,98,357,85,170,232,110};
    pin(arr,8);
    //冒泡排序
    pop_sort(arr,8);
    pin(arr,8);
输入数组
int arr1[8] ={198,289,98,357,85,170,232,110};
     pin(arr1,8);
    select_sort(arr1,8);
    pin(arr1,8);
    int arr2[8] ={198,289,98,357,85,170,232,110};
    int p3 = part(arr2, 0, 7);
    printf("%d\n", p3);
    int p4 =part(arr2,0,p3);
    int p5 = part(arr2,p3+1,7);
     pin(arr1,8);
    
    int arr3[8] ={198,289,98,357,85,170,232,110};
     insert_sort(arr3,8);
     pin(arr3,8);
    return 0;
}

sort.h

#ifndef __SORT_H__
#define __SORT_H__
//冒泡排序
void pop_sort(int *arr, int n);
//选择排序
void select_sort(int *arr, int n);
//快速排序
int part(int *arr, int low , int high);
//直接插入 排序
void  insert_sort(int *arr,int n);

 //打印 排序
 void  pin( int *arr, int n);
#endif

sort.c

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include "sort.h"

// 冒泡排序
void pop_sort(int *arr, int n){

    int i ,j;
    int t;
    for(i =0; i < n;i++){
        int flag = 0;
        for(j = 0; j < n-1;j++){
            if(arr[j] > arr[j+1]){
                t = 0;
                t = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = t;
                flag = 1;
            }
        }
        if(flag == 0){
            break;
        }
}
}
//选择排序
void select_sort(int *arr, int n){
 
    int i = 0;
    int j = 0;
    int min;
    int t;
    for (i = 0; i < n; i++)
    {
        min = i;
        for(j = i + 1;j < n; j++){
        if(arr[min] > arr[j]){
            min = j;
        }
    }
    if(min != i){
        t = arr[min];
        arr[min] = arr[i];
        arr[i] = t;
    }
}
 printf("排序成功");
    

    return 0;
// 快速排序
}
int part(int *arr, int low, int high)
{
    int x = arr[low];
    while(low<high)
    {
    while(arr[high]>=x && low<high){
            high--; 
    }
    arr[low] = arr[high];
    while(arr[low]<=x && low<high)
    {
    low++;
    }
    arr[high] = arr[low];
    }
       arr[low] = x;
        return low;
    }
void quick_sort(int *arr, int low, int high){
    if(low < high){
        int mid = part(arr,low, high);
        quick_sort(arr, low,mid-1);
        quick_sort(arr, mid+1,high);

    }
}
//直接插入排序
void  insert_sort(int *arr,int n){
    int i,j;
    int temp;
    for(i = 0;i < n;i++){
        int temp = arr[i];
        for(j = i; j > 0 && temp < arr[j-1]; j--){
            arr[j] = arr[j-1];
     }
     arr[j] = temp;
     }
}
//打印排序
void  pin( int *arr, int n){
    int i = 0;
    for(i = 0; i < n; i++){
        printf("%d   ",arr[i]);

    }
    printf("\n");
    printf(",,,,,,,\n");

    return 0;
}