zoukankan      html  css  js  c++  java
  • 线性表--线性存储

      1 #include <iostream>
      2 using namespace std;
      3 //定义线性表结构体
      4 #define SIZE 100
      5 typedef struct SqList {
      6     int *elem;//线性表基地址
      7     int length;//当前长度
      8     int listsize;//当前分配存储容量
      9 }*List;
     10 //初始化
     11 void InitList(SqList &L) {
     12     //分配存储空间
     13     L.elem = (int *)malloc(SIZE * sizeof(int));
     14     if (!L.elem) {
     15         cout << "抱歉,存储空间分配失败!" << endl;
     16         return;
     17     }
     18     //设置表长
     19     L.length = 0;
     20     //设置表的存储容量
     21     L.listsize = SIZE;
     22 }
     23 //插入元素
     24 void InsertList(SqList &L, int a, int j) {
     25     //判断插入位置是否符合条件
     26     if (j<1 || j>L.length + 1) {
     27         cout << "抱歉,要插入的位置不符合条件,插入操作失败!" << endl;
     28         return;
     29     }
     30     //移动后半部分数据
     31     int *q, *p;
     32     q = &(L.elem[j-1]);//q为要插入的位置
     33     for (p = &(L.elem[L.length]); p > q; p--) *p = *(p - 1);
     34     *q = a;
     35     L.length++;
     36     cout << "数据插入成功" << endl;
     37 }
     38 //删除元素
     39 void DeleteList(SqList &L, int j,int &e) {
     40     if (j<1 || j>L.length) {
     41         cout << "所输入元素位置不满足条件,无法进行删除操作" << endl;
     42         return;
     43     }
     44     int *p;
     45     e = L.elem[j - 1];
     46     for (p = &(L.elem[j - 1]); p < &(L.elem[L.length - 1]); p++) *p = *(p + 1);
     47     L.length--;
     48     cout << "数据删除成功" << endl;
     49 }
     50 //销毁线性表
     51 void DestoryList(SqList &L) {
     52     //清空操作
     53     int i;
     54     for (i = 0; i < L.length - 1; i++) L.elem[i] = 0;
     55     L.length = 0;
     56 }
     57 //判断是否为空
     58 bool IsEmpty(SqList &L) {
     59     if (L.length == 0) return true;
     60     else return false;
     61 }
     62 //求线性表长度
     63 int ListLength(SqList &L) {
     64     return L.length;
     65 }
     66 //返回第i个元素的值
     67 int GetElem(SqList &L, int i) {
     68     //判断i是否符合条件
     69     if (i<1 || i>L.length) {
     70         cout << "抱歉,要查找的元素位置不符合条件,查找失败" << endl;
     71         return -1;
     72     }
     73     else return L.elem[i - 1];
     74 }
     75 //查找元素
     76 int FindElem(SqList &L, int e) {
     77     int i;
     78     for (i = 0; i < L.length; i++) {
     79         if (e == L.elem[i]) {
     80             return i;
     81         }
     82     }
     83     return -1;//此元素不存在
     84 }
     85 //返回前驱
     86 int PriorElem(SqList &L, int cur_e) {
     87     //判断是否存在
     88     int i;
     89     i = FindElem(L, cur_e);
     90     if (i == -1) {
     91         cout << "抱歉,该元素不存在!" << endl;
     92         return 0;
     93     }
     94     else if (i == 0) {
     95         cout << "该元素没有前驱元素" << endl;
     96         return 0;
     97     }
     98     else {
     99         int m;
    100         m=GetElem(L, i);
    101         return m;
    102     }
    103 }
    104 //返回后继
    105 int NextElem(SqList &L, int cur_e) {
    106     //判断是否存在
    107     int i;
    108     i = FindElem(L, cur_e);
    109     if (i == -1) {
    110         cout << "抱歉,该元素不存在!" << endl;
    111         return 0;
    112     }
    113     else if (i == (L.length-1)) {
    114         cout << "该元素没有后继元素" << endl;
    115         return 0;
    116     }
    117     else {
    118         int m;
    119         m=GetElem(L, i);
    120         return m;
    121     }
    122 }
    123 int main() {
    124     SqList list;
    125     int n;
    126     int i;
    127     InitList(list);
    128     for (i = 1; i < 11; i++) InsertList(list, i, i);
    129     int m;
    130     DeleteList(list, 5,m);
    131     cout << "m=" << m << endl;
    132     m = ListLength(list);
    133     cout <<  "线性表长度为:" << m << endl;
    134     m = GetElem(list, 7);
    135     cout << "第7个元素的值为" << m << endl;
    136     if (IsEmpty(list) == true) cout << "线性表为空" << endl;
    137     else cout << "线性表不为空" << endl;
    138     return 0;
    139 
    140 }

    本代码并未采用定义类的方式来封装函数,只是单纯定义函数然后调用,本代码目的主要是能够掌握各函数的定义,因此并未定义类

  • 相关阅读:
    【Nginx】url 带有 “https://” 双斜杠特殊处理
    【layui】tepmlet 格式化 table 数据
    于二零二零年:终章
    【Golang】练习-Web 处理 form 表单请求失败不刷新页面并保存输入的数据
    实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例)
    面向对象的进阶(item系列,__new__,__hash__,__eq__)
    面向对象阶段复习
    计算器实例
    反射
    静态方法staticmethod和类方法classmethod
  • 原文地址:https://www.cnblogs.com/2019-12-10-18ykx/p/12956222.html
Copyright © 2011-2022 走看看