zoukankan      html  css  js  c++  java
  • 动态顺序表

    动态为顺序表分配内存,实现基本的增、删、查、改、排序。

     List.h
     3 #include<stdio.h>
     4 #include<malloc.h>
     5 #include<assert.h>
     6 #define MAXSIZE 3
     7 typedef int Datatype;
     8 typedef struct List{
     9    Datatype *_arr;
    10    size_t size; 
    11    size_t capicity;
    12 }SeqList,*pSeqList;
    13 
    14 
    15 void SeqInit(pSeqList L );
    16 void SeqPrint(pSeqList L);
    17 
    18 void SeqPushBack(pSeqList L, Datatype data) ;
    19 void SeqPopBack(pSeqList L);
    20 void SeqPushFront(pSeqList L, Datatype data);
    21 void SeqPopFront(pSeqList L);
    22 
    23 void SeqInsert(pSeqList L, int pos, Datatype data);
    24 void SeqErase(pSeqList L, size_t pos); 
    25 int SeqSearch(pSeqList L, Datatype data );  
    26 void SeqAt(pSeqList L, size_t pos, Datatype data);    //替换 
      1 void SeqInit(pSeqList L ){  //定义
      2     L->size = 0;
      3     L->capicity = MAXSIZE;
      4     L->_arr = (Datatype *)malloc(sizeof(Datatype) *L->capicity );
      5     if(L->_arr == NULL){
      6         printf("初始化失败!
    ");
      7         return;
      8     }
      9 } 
     10 SeqList* ExpandCapicity(pSeqList L){   //扩容
     11     assert(L);
     12     Datatype* temp; 
     13     if(L->size == L->capicity){
     14         temp = (Datatype *)realloc(L->_arr, sizeof(Datatype)*L->capicity*2);
     15        if( NULL == temp){
     16          printf("扩容失败!
    ");
     17          return ; 
     18        }
     19        L->capicity *=2; 
     20        L->_arr = temp;
     21     }    
     22     return L;
     23 } 
     24 void SeqPrint(pSeqList L){
     25     assert(L);
     26     int i= 0;
     27     for(i; i<L->size ; ++i){
     28         printf("%d ",L->_arr[i]);
     29     }
     30 }
     31 void SeqPushFront(pSeqList L, Datatype data){  //头插
     32     assert(L);
     33     size_t end = L->size - 1;
     34     if(L->size >= L->capicity){
     35         L= ExpandCapicity(L); 
     36     }    
     37     while((int)end >= 0){
     38         L->_arr[end+1] = L->_arr[end];
     39         end--;
     40     }
     41     L->_arr[0] = data;
     42     L->size++;
     43 } 
     44 void SeqPopFront(pSeqList L){    //头出
     45     assert(L);
     46     size_t begin = 1;
     47     if(L->size == 0){
     48         printf("NULL!");
     49         return;
     50     }
     51     while(begin < L->size){
     52         L->_arr[begin-1] = L->_arr[begin];
     53         begin++;
     54     }
     55     L->size--;
     56 }
     57 void SeqPushBack(pSeqList L, Datatype data){   //尾插
     58     assert(L);
     59     
     60     if(L->size > L->capicity){
     61         printf("SeqList is Full!");
     62         return ;
     63     }
     64     L->_arr[L->size++] = data;
     65     
     66 }
     67 void SeqPopBack(pSeqList L){
     68     assert(L);
     69     if(L->size <=0){
     70         printf("NULL");
     71         return;
     72     }
     73     L->size--;
     74     
     75 }
     76 void SeqInsert(pSeqList L, int pos, Datatype data){  //普通插入
     77     assert(L);
     78     assert(pos >= 0);
     79     if(pos > L->size){
     80         printf("Position Error!");
     81         return;
     82     }
     83     int end =L->size - 1;
     84     while(end >= pos){
     85         L->_arr[end+1] = L->_arr[end];
     86         end--;
     87     }
     88     L->_arr[pos] = data;
     89     L->size++;
     90 }
     91 void SeqErase(pSeqList L, size_t pos){   //删除
     92     assert(L);
     93     if(pos< 0 || pos > L->size){
     94         printf("No Exist!");
     95         return ;
     96     }
     97     size_t begin = pos +1;
     98     while(begin < L->size){
     99         L->_arr[begin -1] = L->_arr[begin];
    100         begin++;
    101     }
    102     L->size--;
    103     
    104 }
    105 int SeqSearch(pSeqList L, Datatype data){   //二分查找
    106     assert(L);
    107     int left = 0;
    108     int right = L->size - 1;
    109     while(left <= right){
    110         int mid = left + (right - left>> 1);
    111         if(L->_arr[mid] < data){
    112             left = mid +1;
    113         }
    114         else if(L->_arr[mid] > data)
    115             right = mid - 1;
    116         else return mid;
    117     }
    118     return -1;
    119 }  
    120 void SeqAt(pSeqList L, size_t pos, Datatype data){   //替换 
    121        assert(L);
    122     if(pos < 0 || pos >= L->size){
    123         printf("替换错误!
    ");
    124         return; 
    125     }
    126     L->_arr[pos] = data; 
    127 }
    128 void swap(int *x,int *y){
    129     int temp;
    130     temp = *x;
    131     *x = *y;
    132     *y =temp;
    133 }
    134 void BubbleSort(pSeqList L){  //冒泡排序
    135     assert(L);
    136     int i,j;
    137     for( i=0;i< L->size-1;++i){
    138         int flag= 0;
    139         for( j=0; j< L->size -1; ++j){
    140             if(L->_arr[j] > L->_arr[j+1]){
    141                 swap(& L->_arr[j],&L->_arr[j+1]);
    142                 flag =1;
    143             }
    144         }
    145         if(0 == flag){
    146             break;
    147         }
    148     }    
    149 }
    150 
    151 void SelectSort(pSeqList L){  //选择排序 
    152     size_t i,left = 0;
    153     size_t right = L->size - 1;
    154     size_t min = left;//存储最小值的下标
    155     size_t max = left;//存储最大值的下标
    156 
    157     while (left <= right)
    158     {
    159         min = left;
    160         max = left;
    161         for (i = left; i <= right; ++i)
    162         {
    163             if (L->_arr[i] < L->_arr[min])
    164             {
    165                 min = i;
    166             }
    167             if (L->_arr[i] > L->_arr[max])
    168             {
    169                 max = i;
    170             }
    171         }
    172         swap(&(L->_arr[left]), &(L->_arr[min]));
    173         if (left == max)
    174             max = min;
    175         swap(&(L->_arr[right]), &(L->_arr[max]));
    176 
    177         ++left;
    178         --right;
    179     }
    180 }
     1 #include <stdio.h>
     2 
     3 int main(int argc, char *argv[]) {
     4 //    test01();
     5 //    test02();
     6 //    test03();
     7 //    test04();
     8 //    test05();
     9     test06();
    10     return 0;
    11 }
  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/tp-16b/p/8037196.html
Copyright © 2011-2022 走看看