zoukankan      html  css  js  c++  java
  • C语言版本:顺序表的实现

    seqlist.h

     1 #ifndef __SEQLIST_H__
     2 #define __SEQLIST_H__
     3 
     4 #include<cstdio>
     5 #include<malloc.h>
     6 #include<assert.h>
     7 #define SEQLIST_INIT_SIZE 8
     8 #define INC_SIZE 3 //空间增量的大小
     9 typedef int ElemType;
    10 typedef struct Seqlist {
    11     ElemType *base;
    12     int capacity; //顺序表容量
    13     int size; //表的大小
    14 }Seqlist;
    15 
    16 bool Inc(Seqlist *list);//增加顺序表的容量
    17 void InitSeqlist(Seqlist *list); //初始化顺序表
    18 void push_back(Seqlist *list, ElemType x); //在顺序表的末尾插入元素
    19 void push_front(Seqlist *list, ElemType x); //在顺序表的头部插入元素
    20 void show_list(Seqlist *list); //显示顺序表中的元素
    21 void pop_back(Seqlist *list); //删除顺序表最后一个元素
    22 void pop_front(Seqlist *list); //删除顺序表第一个元素
    23 void insert_pos(Seqlist *list, int pos, ElemType x);//在顺序表的选定位置上插入数据
    24 int find(Seqlist *list, ElemType key); //在顺序表中查找元素key的下标
    25 int length(Seqlist *list);//求顺序表的长度
    26 void delete_pos(Seqlist *list, int pos); //删除顺序表中特定位置的数据元素
    27 void delete_val(Seqlist *list, int key);//删除顺序表中值为key的数据元素
    28 void sort(Seqlist *list);//冒泡排序
    29 void reverse(Seqlist *list);//逆置顺序列表
    30 void clear(Seqlist *list);//清除顺序表中的所有元素
    31 void destroy(Seqlist *list);//摧毁顺序表
    32 void merge(Seqlist *lt, Seqlist *la, Seqlist *lb);//合并两个顺序列表
    33 
    34 #endif //__SEQLIST_H__

    seqlist.cpp

      1 #include"seqlist.h"
      2 
      3 bool Inc(Seqlist *list) {
      4     ElemType *newbase = (ElemType*)realloc(list, sizeof(ElemType)*(list->capacity + INC_SIZE));  //重新分配内存空间
      5     if (newbase == NULL) {
      6         printf("内存空间已满,无法再分配内存空间!
    ");
      7         return false;
      8     }
      9     list->base = newbase;
     10     list->capacity += INC_SIZE;
     11     return true;
     12 }
     13 
     14 void InitSeqlist(Seqlist *list) {
     15     list->base = (ElemType*)malloc(sizeof(ElemType)*SEQLIST_INIT_SIZE);
     16     assert(list->base != NULL);
     17     list->capacity = SEQLIST_INIT_SIZE;
     18     list->size = 0;
     19 }
     20 
     21 void push_back(Seqlist *list, ElemType x) {
     22     if (list->size >= list->capacity && !Inc(list)) { //Inc(list)用来判断增加顺序表容量是否成功,只有在失败的情况下才会进入if语句中
     23         printf("顺序表容量已满,无法再在表尾继续插入新元素!
    ");
     24         return;
     25     }
     26     list->base[list->size] = x;
     27     list->size++;
     28 }
     29 
     30 void push_front(Seqlist *list, ElemType x) {
     31     if (list->size >= list->capacity && !Inc(list)) {
     32         printf("顺序表容量已满,无法再在表头插入新元素!
    ");
     33         return;
     34     }
     35     for (int i = list->size;i > 0;i--) {
     36         list->base[i] = list->base[i - 1];
     37     }
     38     list->base[0] = x;
     39     list->size++;
     40 }
     41 
     42 void show_list(Seqlist *list) {
     43     for (int i = 0;i < list->size;i++) {
     44         printf("%d ", list->base[i]);
     45     }
     46     printf("
    ");
     47 }
     48 
     49 void pop_back(Seqlist *list) {
     50     if (list->size == 0) {
     51         printf("顺序表已空,无法再在表尾删除元素!
    ");
     52         return;
     53     }
     54     list->size--;
     55 }
     56 
     57 void pop_front(Seqlist *list) {
     58     if (list->size == 0) {
     59         printf("顺序表已空,无法再在表头删除元素!
    ");
     60         return;
     61     }
     62     for (int i = 0;i < list->size - 1;i++) {
     63         list->base[i] = list->base[i + 1];
     64     }
     65     list->size--;
     66 }
     67 
     68 void insert_pos(Seqlist *list, int pos, ElemType x) {
     69     if (pos<0 || pos>list->size) {
     70         printf("插入位置不合法,无法插入元素!
    ");
     71         return;
     72     }
     73     if (list->size >= list->capacity && !Inc(list)) {
     74         printf("顺序表容量已满,无法在插入新的元素!
    ");
     75         return;
     76     }
     77     for (int i = list->size;i > pos;i--) {
     78         list->base[i] = list->base[i - 1];
     79     }
     80     list->base[pos] = x;
     81     list->size++;
     82 }
     83 
     84 int find(Seqlist *list, ElemType key) {
     85     for (int i = 0;i < list->size;i++) {
     86         if (list->base[i] == key)
     87             return i;
     88     }
     89     return -1;
     90 }
     91 
     92 int length(Seqlist *list) {
     93     return list->size;
     94 }
     95 
     96 void delete_pos(Seqlist *list, int pos) {
     97     if (pos < 0 || pos >= list->size) {
     98         printf("删除位置不合法,无法删除元素!
    ");
     99         return;
    100     }
    101     for (int i = pos;i < list->size - 1;i++) {
    102         list->base[i] = list->base[i + 1];
    103     }
    104     list->size--;
    105 }
    106 
    107 void delete_val(Seqlist *list, int key) {
    108     int pos = find(list, key);
    109     if (pos == -1) {
    110         printf("顺序表中没有这个元素!
    ");
    111         return;
    112     }
    113     delete_pos(list, pos);
    114 }
    115 
    116 void sort(Seqlist *list) {
    117     for (int i = 0;i < list->size - 1;i++) {//排序的趟数(例如5个数据需要比较4趟)
    118         for (int j = 0;j < list->size - 1 - i;j++) {//每一趟比较中的比较次数(例如5个数据在第0趟需要比较4次)
    119             if (list->base[j] > list->base[j + 1]) {
    120                 ElemType temp = list->base[j];
    121                 list->base[j] = list->base[j + 1];
    122                 list->base[j + 1] = temp;
    123             }
    124         }
    125     }
    126 }
    127 
    128 void reverse(Seqlist *list) {
    129     if (list->size == 0 || list->size == 1) return;
    130     int low = 0, high = list->size - 1;
    131     while (low < high) {
    132         ElemType temp = list->base[low];
    133         list->base[low] = list->base[high];
    134         list->base[high] = temp;
    135         low++;
    136         high--;
    137     }
    138 }
    139 
    140 void clear(Seqlist *list) {
    141     list->size = 0;
    142 }
    143 
    144 void destroy(Seqlist *list) {
    145     free(list->base);
    146     list->base = NULL;
    147     list->capacity = 0;
    148     list->size = 0;
    149 }
    150 
    151 void merge(Seqlist *lt, Seqlist *la, Seqlist *lb) {
    152     lt->capacity = la->size + lb->size;
    153     lt->base = (ElemType*)malloc(sizeof(ElemType)*lt->capacity);
    154     assert(lt->base != NULL);
    155 
    156     int ia = 0, ib = 0, ic = 0;
    157     while (ia < la->size&&ib < lb->size) {
    158         if (la->base[ia] < lb->base[ib]) {
    159             lt->base[ic++] = la->base[ia++];
    160         }
    161         else {
    162             lt->base[ic++] = lb->base[ib++];
    163         }
    164     }
    165     while (ia < la->size) {
    166         lt->base[ic++] = la->base[ia++];
    167     }
    168     while (ib < lb->size) {
    169         lt->base[ic++] = lb->base[ib++];
    170     }
    171     lt->size = la->size + lb->size;
    172     show_list(lt);
    173 }

    main.cpp

      1 #include"seqlist.h"
      2 
      3 void main() {
      4     Seqlist list;
      5     InitSeqlist(&list);
      6 
      7     ElemType item;
      8     int pos;
      9     int select = 1;
     10     while (select) {
     11         printf("*******************************************
    ");
     12         printf("*[1]  push_back        [2]  push_front    *
    ");
     13         printf("*[3]  show_list        [4]  pop_back      *
    ");
     14         printf("*[5]  pop_front        [6]  insert_pos    *
    ");
     15         printf("*[7]  find             [8]  length        *
    ");
     16         printf("*[9]  delete_pos       [10] delete_value  *
    ");
     17         printf("*[11] sort             [12] reverse       *
    ");
     18         printf("*[13] clear            [14] merge         *
    ");
     19         printf("*[0]  quit_system                         *
    ");
     20         printf("*******************************************
    ");
     21         printf("请选择:>>");
     22         scanf("%d", &select);
     23         if (select == 0) break;
     24         switch (select) {
     25         case 1:
     26             printf("请输入要插入的数据(-1结束):>");
     27             while (scanf("%d", &item), item != -1) {//先输入item的值,只要item不等于-1就接着循环
     28                 push_back(&list, item);
     29             }
     30             break;
     31         case 2:
     32             printf("请输入要插入的数据(-1结束):>");
     33             while (scanf("%d", &item), item != -1) {
     34                 push_front(&list, item);
     35             }
     36             break;
     37         case 3:
     38             show_list(&list);
     39             break;
     40         case 4:
     41             pop_back(&list);
     42             break;
     43         case 5:
     44             pop_front(&list);
     45             break;
     46         case 6:
     47             printf("请输入要插入的数据:>");
     48             scanf("%d", &item);
     49             printf("请输入要插入的位置:>");
     50             scanf("%d", &pos);
     51             insert_pos(&list, pos, item);
     52             break;
     53         case 7:
     54             printf("请输入要查找的数据:>");
     55             scanf("%d", &item);
     56             pos = find(&list, item);
     57             if (pos == -1)
     58                 printf("查找的数据元素不在顺序表中!
    ");
     59             else
     60                 printf("查找的数据元素在顺序表中的下标位置为%d
    ", pos);
     61             break;
     62         case 8:
     63             printf("顺序表的长度为%d
    ", length(&list));
     64             break;
     65         case 9:
     66             printf("请输入要删除数据在顺序表中的下标位置:>");
     67             scanf("%d", &pos);
     68             delete_pos(&list, pos);
     69             break;
     70         case 10:
     71             printf("请输入要删除数据的值:>");
     72             scanf("%d", &item);
     73             delete_val(&list, item);
     74             break;
     75         case 11:
     76             sort(&list);
     77             break;
     78         case 12:
     79             reverse(&list);
     80             break;
     81         case 13:
     82             clear(&list);
     83             break;
     84         case 14:
     85             Seqlist mylist, yourlist;
     86             ElemType item1, item2;
     87             InitSeqlist(&mylist);
     88             InitSeqlist(&yourlist);
     89             printf("请输入顺序表1中的元素值(-1结束):>");
     90             while (scanf("%d", &item1), item1 != -1) {
     91                 push_back(&mylist, item1);
     92             }
     93             printf("请输入顺序表2中的元素值(-1结束):>");
     94             while (scanf("%d", &item2), item2 != -1) {
     95                 push_back(&yourlist, item2);
     96             }
     97             merge(&list, &mylist, &yourlist);
     98             destroy(&mylist);
     99             destroy(&yourlist);
    100             break;
    101         default:
    102             printf("输入的选择错误!请重新输入!
    ");
    103             break;
    104         }
    105     }
    106     destroy(&list);
    107 }
  • 相关阅读:
    c语言指针讲解第一节初识指针
    linux的的一些入门常识
    sql手注的思路
    mysql主从备份配置
    CentOS 6.5 nginx+tomcat+ssl配置
    mysql 5.7.18安装教程
    minIO分布式集群搭建+nginx负载均衡
    Linux常用命令
    使用python连接mysql数据库——pymysql模块的使用
    with与上下文管理器
  • 原文地址:https://www.cnblogs.com/duwenxing/p/7562588.html
Copyright © 2011-2022 走看看