• 顺序链表的实现


      1 #include<iostream>
      2 #define Max 50
      3 
      4 #define OK 1
      5 using namespace std;
      6 //顺序链表c语言版(a1,a2...an)
      7 typedef int ElemType;
      8 typedef struct //s顺序链表定义
      9 {
     10     ElemType* elem;
     11     int length;
     12 }ListLink;
     13 
     14 int ListInit(ListLink&List)//顺序链表初始化
     15 {
     16     List.elem = (ElemType*)malloc(sizeof(ElemType) * Max);
     17     if (!List.elem)//内存分配上失败退出程序
     18         exit(-1);
     19     List.length = 0;
     20     return OK;
     21 }
     22 
     23 ElemType ListGet(ListLink& List, int i)//得到顺序链表第i个位置元素
     24 {
     25     if (i<1 || i>List.length)
     26         return false;
     27     return List.elem[i - 1];
     28 }
     29 
     30 bool ListInsert(ListLink& List, int i, ElemType data)//顺序链表在第i个位置插入元素data
     31 {
     32     if (i<1 || i>List.length)
     33         return false;
     34     if (List.length >= Max)
     35         return false;
     36     for (int j = List.length ; j >= i; --j)
     37         List.elem[j] = List.elem[j-1];
     38     List.elem[i- 1] = data;
     39     ++List.length;
     40     return true;
     41 }
     42 
     43 bool ListInsertHead(ListLink& List, ElemType data)//在头部插入一个元素
     44 {
     45     if (List.length >= Max)
     46         return false;
     47     for (int j = List.length-1; j >=0; --j)
     48     List.elem[j + 1] = List.elem[j];
     49     List.elem[0] = data;
     50     ++List.length;
     51     return true;
     52 }
     53 
     54 bool ListInsertEnd(ListLink& List, ElemType data)//在尾部插入一个元素
     55 {
     56     if (List.length >= Max)
     57         return false;
     58     List.elem[List.length] = data;
     59     ++List.length;
     60     return true;
     61 }
     62 
     63 bool ListDeleteIndex(ListLink& List, int i)//按位置删除元素
     64 {
     65     if (i<1 || i>List.length)
     66         return false;
     67     for (int j = i; j < List.length; ++j)
     68         List.elem[j - 1] = List.elem[j];
     69     --List.length;
     70     return true;
     71 }
     72 
     73 bool ListDeleteElem(ListLink& List, ElemType data)//按值删除元素
     74 {
     75     for (int j = 0; j < List.length; ++j)
     76     {
     77         if (List.elem[j] == data)
     78         {
     79             for (int t = j; t < List.length-1; ++t)
     80                 List.elem[t] = List.elem[t+1];
     81             --List.length;
     82             return true;
     83         }
     84     }
     85     return false;
     86 }
     87 
     88 void ListClear(ListLink& List)//删除顺序链表全部元素
     89 {
     90     if (List.length)
     91     {
     92         for (int i = List.length-1; i >=0 ; --i)
     93         {
     94             List.elem[i-1] = List.elem[i];
     95             List.length--;
     96         }
     97     }
     98 }
     99 
    100 int ListLength(ListLink& List)//获取顺序链表长度
    101 {
    102     return List.length;
    103 }
    104 
    105 bool ListFull(ListLink& List)//顺序链表是否为满
    106 {
    107     return List.length == Max;
    108 
    109 }
    110 
    111 bool ListEmpty(ListLink& List)//顺序链表是否为空
    112 {
    113     return List.length == 0;
    114 
    115 }
    116 
    117 void ListShow(ListLink& List)
    118 {
    119     for (int i = 0; i < List.length; ++i)
    120         cout << List.elem[i] << "  ";
    121 }
    122 
    123 int main() {
    124     ListLink lis;
    125     ListInit(lis);
    126     cout << ListEmpty(lis) << endl;
    127     ListInsertHead(lis, 4);
    128     ListInsertHead(lis, 3);
    129     ListInsertHead(lis, 2);
    130     ListInsertEnd(lis, 7);
    131     ListInsertEnd(lis, 8);
    132     ListInsertEnd(lis, 9);
    133     ListInsert(lis, 4, 5);
    134     ListInsert(lis, 5, 6);
    135     ListShow(lis);
    136     cout << endl;
    137     ListDeleteIndex(lis, 3);
    138     ListShow(lis);
    139     cout << endl;
    140     ListDeleteElem(lis, 8);
    141     ListShow(lis);
    142     cout << endl;
    143     cout << ListLength(lis) << endl;
    144     ListClear(lis);
    145     cout << ListEmpty(lis) << endl;
    146     return 0;
    147 
    148 }

  • 相关阅读:
    Window上编译最新版libCef(Branch 2704)(转载)
    在hue 使用oozie sqoop 从mysql 导入hive 失败
    hive 支持更新
    基于Hadoop生态圈的数据仓库实践 —— 环境搭建(三)笔记
    修改CENTOS7的网卡名(将网卡ens33修改为我们在centos6中常见的eth0)
    config network name
    Java服务部署规范(内部使用)
    MongoDB干货系列1-定期巡检之Mtools
    mongodb validation--像关系型数据库表去使用mongodb
    ntp 服务导致kudu集群不可用
  • 原文地址:https://www.cnblogs.com/dhhu007/p/13192672.html
Copyright © 2020-2023  润新知