zoukankan      html  css  js  c++  java
  • 线性表的顺序存储

     1 //List.h
     2 
     3 
     4 #ifndef LIST_H
     5 #define LIST_H
     6 #define LIST_INIT_SIZE 100  //线性表存储空间的初始分配量
     7 #define LISTINCREMENT 10    //线性表存储空间的分配增量
     8 
     9 #define OK 1
    10 #define Error 0
    11 typedef int Status;
    12 typedef int ElemType;
    13 
    14 typedef struct {
    15     ElemType *elem;//存储空间基地址
    16     int length;    //当前长度
    17     int listsize;  //当前分配的存储容量(以sizeof(Elemtype)为单位)
    18 }SqList;
    19 
    20 Status InitList(SqList &L);
    21 Status ListInsert(SqList &L, int i, ElemType e);
    22 Status ListDelete(SqList &L, int i, ElemType &e);
    23 void ListTraverse(SqList &L);
    24 #endif
    //List.cpp
    
    #include"List.h"
    #include<cstdlib>
    #include<iostream>
    Status InitList(SqList &L)
    //构造一个空的线性表
    {
        L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
        if (!L.elem)
            exit(OVERFLOW);
        L.length = 0;
        L.listsize = LIST_INIT_SIZE;
        return OK;
    }
    
    Status ListInsert(SqList &L, int i, ElemType e)
    //在顺序线性表L中第i个位置之前插入新的元素e
    {
        if (i<1 || i>L.length + 1)
            return Error;
        if (L.length >= L.listsize)
        {
            ElemType * newbase;
            newbase= (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
            if (!newbase)
                exit(OVERFLOW);
            L.elem = newbase;//新基地址
            L.listsize += LISTINCREMENT;
        }
        ElemType* q;
        ElemType* p;
        q = &(L.elem[i - 1]);
        for (p = &(L.elem[L.length - 1]); p >= q; --p)
            *(p + 1) = *p;
        *q = e;
        ++L.length;
        return OK;
    }
    
    Status ListDelete(SqList &L, int i, ElemType &e)
    {
        if (i<1 || i>L.length)
            return Error;
        ElemType* p;
        ElemType* q;
        p = &(L.elem[i - 1]);  //p为被删除元素的位置
        e = *p;                //被删除元素的值赋给e
        q = L.elem + L.length - 1;
        for (++p; p <= q; ++p)
            *(p - 1) = *p;
        --L.length;
        return OK;
    }
    
    void ListTraverse(SqList &L)
    {
        for (int i = 0; i < L.length; i++)
        {
            std::cout << L.elem[i];
        }
        std::cout << std::endl;
    }

    主函数实现

    //main.cpp
    
    #include"List.h"
    #include<iostream>
    using namespace std;
    
    int main()
    {
        SqList L;
        InitList(L);
        ElemType n1 = 1;
        ElemType n2 = 2;
        ElemType n3 = 3;
        ElemType n4 = 4;
        ElemType n5 = 5;
        ElemType n6 = 6;
        ElemType n7 = 7;
        ElemType n8 = 8;
        ElemType n9 = 9;
        ElemType n0 = 0;
        ElemType temp = 0;
    
        ListInsert(L, 1, n7);
        ListInsert(L, 2, n7);
        ListInsert(L, 3, n7);
        ListInsert(L, 4, n4);
        ListInsert(L, 5, n6);
        ListInsert(L, 6, n5);
        ListInsert(L, 7, n8);
        ListInsert(L, 8, n3);
        ListTraverse(L);
    
        ListDelete(L, 5, temp);
        cout << temp;
        cout << endl;
    
        ListTraverse(L);
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    NFS共享储存
    Rsync服务
    企业集群架构概述
    定时任务
    开机启动流程
    进程管理(二)
    linux系统管理-进程管理
    RAID&LVM有关磁盘的故障
    gdisk分区及swap分区
    操作系统与磁盘管理
  • 原文地址:https://www.cnblogs.com/sgawscd/p/10176046.html
Copyright © 2011-2022 走看看